-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FixedSizeTypeNode and SizePrefixTypeNode (#191)
* Add FixedSizeTypeNode and SizePrefixTypeNode * Add changeset
- Loading branch information
1 parent
937742d
commit 6021aa9
Showing
10 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@metaplex-foundation/kinobi": minor | ||
--- | ||
|
||
Add FixedSizeTypeNode and SizePrefixTypeNode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { TypeNode } from './TypeNode'; | ||
|
||
export type FixedSizeTypeNode = { | ||
readonly kind: 'fixedSizeTypeNode'; | ||
|
||
// Children. | ||
readonly type: TypeNode; | ||
|
||
// Data. | ||
readonly size: number; | ||
}; | ||
|
||
export function fixedSizeTypeNode( | ||
type: TypeNode, | ||
size: number | ||
): FixedSizeTypeNode { | ||
return { kind: 'fixedSizeTypeNode', type, size }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { NumberTypeNode } from './NumberTypeNode'; | ||
import { TypeNode } from './TypeNode'; | ||
|
||
export type SizePrefixTypeNode = { | ||
readonly kind: 'sizePrefixTypeNode'; | ||
|
||
// Children. | ||
readonly type: TypeNode; | ||
readonly prefix: NumberTypeNode; | ||
}; | ||
|
||
export function sizePrefixTypeNode( | ||
type: TypeNode, | ||
prefix: NumberTypeNode | ||
): SizePrefixTypeNode { | ||
return { kind: 'sizePrefixTypeNode', type, prefix }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import test from 'ava'; | ||
import { | ||
fixedSizeTypeNode, | ||
remainderSizeNode, | ||
stringTypeNode, | ||
} from '../../../../src'; | ||
import { | ||
deleteNodesVisitorMacro, | ||
getDebugStringVisitorMacro, | ||
identityVisitorMacro, | ||
mergeVisitorMacro, | ||
} from '../_setup'; | ||
|
||
const node = fixedSizeTypeNode( | ||
stringTypeNode({ size: remainderSizeNode() }), | ||
42 | ||
); | ||
|
||
test(mergeVisitorMacro, node, 3); | ||
test(identityVisitorMacro, node); | ||
test(deleteNodesVisitorMacro, node, '[stringTypeNode]', null); | ||
test(deleteNodesVisitorMacro, node, '[fixedSizeTypeNode]', null); | ||
test( | ||
getDebugStringVisitorMacro, | ||
node, | ||
` | ||
fixedSizeTypeNode [42] | ||
| stringTypeNode [utf8] | ||
| | remainderSizeNode | ||
` | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import test from 'ava'; | ||
import { | ||
numberTypeNode, | ||
remainderSizeNode, | ||
sizePrefixTypeNode, | ||
stringTypeNode, | ||
} from '../../../../src'; | ||
import { | ||
deleteNodesVisitorMacro, | ||
getDebugStringVisitorMacro, | ||
identityVisitorMacro, | ||
mergeVisitorMacro, | ||
} from '../_setup'; | ||
|
||
const node = sizePrefixTypeNode( | ||
stringTypeNode({ size: remainderSizeNode() }), | ||
numberTypeNode('u32') | ||
); | ||
|
||
test(mergeVisitorMacro, node, 4); | ||
test(identityVisitorMacro, node); | ||
test(deleteNodesVisitorMacro, node, '[sizePrefixTypeNode]', null); | ||
test(deleteNodesVisitorMacro, node, '[stringTypeNode]', null); | ||
test(deleteNodesVisitorMacro, node, '[numberTypeNode]', null); | ||
test( | ||
getDebugStringVisitorMacro, | ||
node, | ||
` | ||
sizePrefixTypeNode | ||
| numberTypeNode [u32] | ||
| stringTypeNode [utf8] | ||
| | remainderSizeNode | ||
` | ||
); |