-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(experimental): add read write functions to option codecs
- Loading branch information
1 parent
b2f08a6
commit 38fe2c9
Showing
4 changed files
with
231 additions
and
125 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 |
---|---|---|
@@ -1,31 +1,34 @@ | ||
import { Codec } from '@solana/codecs-core'; | ||
import { Codec, createCodec } from "@solana/codecs-core"; | ||
|
||
export const b = (s: string) => base16.encode(s); | ||
|
||
export const base16: Codec<string> = { | ||
decode(bytes, offset = 0) { | ||
export const base16: Codec<string> = createCodec({ | ||
getSizeFromValue: (value: string) => Math.ceil(value.length / 2), | ||
read(bytes, offset) { | ||
const value = bytes.slice(offset).reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), ''); | ||
return [value, bytes.length]; | ||
}, | ||
description: 'base16', | ||
encode(value: string) { | ||
write(value: string, bytes, offset) { | ||
const matches = value.toLowerCase().match(/.{1,2}/g); | ||
return Uint8Array.from(matches ? matches.map((byte: string) => parseInt(byte, 16)) : []); | ||
const hexBytes = matches ? matches.map((byte: string) => parseInt(byte, 16)) : []; | ||
bytes.set(hexBytes, offset); | ||
return offset + hexBytes.length; | ||
}, | ||
fixedSize: null, | ||
maxSize: null, | ||
}; | ||
}); | ||
|
||
export const getMockCodec = ( | ||
config: { | ||
defaultValue?: string; | ||
description?: string; | ||
size?: number | null; | ||
} = {}, | ||
) => ({ | ||
decode: jest.fn().mockReturnValue([config.defaultValue ?? '', 0]), | ||
description: config.description ?? 'mock', | ||
encode: jest.fn().mockReturnValue(new Uint8Array()), | ||
fixedSize: config.size ?? null, | ||
maxSize: config.size ?? null, | ||
}); | ||
} = {} | ||
) => | ||
createCodec({ | ||
...(config.size != null ? { fixedSize: config.size } : { getSizeFromValue: jest.fn().mockReturnValue(0) }), | ||
read: jest.fn().mockReturnValue([config.defaultValue ?? '', 0]), | ||
write: jest.fn().mockReturnValue(0), | ||
}) as Codec<unknown> & { | ||
readonly read: jest.Mock; | ||
readonly getSizeFromValue: jest.Mock; | ||
readonly write: jest.Mock; | ||
}; |
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
46 changes: 46 additions & 0 deletions
46
packages/options/src/__typetests__/option-codec-typetest.ts
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,46 @@ | ||
import { | ||
FixedSizeCodec, | ||
FixedSizeDecoder, | ||
FixedSizeEncoder, | ||
VariableSizeCodec, | ||
VariableSizeDecoder, | ||
VariableSizeEncoder, | ||
} from '@solana/codecs-core'; | ||
|
||
import { Option, OptionOrNullable } from '../option'; | ||
import { getOptionCodec, getOptionDecoder, getOptionEncoder } from '../option-codec'; | ||
|
||
{ | ||
// [getOptionEncoder]: It knows if the encoder is fixed size or variable size. | ||
getOptionEncoder({} as FixedSizeEncoder<void, 0>) satisfies FixedSizeEncoder<OptionOrNullable<void>>; | ||
getOptionEncoder({} as FixedSizeEncoder<string>, { fixed: true }) satisfies FixedSizeEncoder< | ||
OptionOrNullable<string> | ||
>; | ||
getOptionEncoder({} as FixedSizeEncoder<string>) satisfies VariableSizeEncoder<OptionOrNullable<string>>; | ||
|
||
// @ts-expect-error It cannot be fixed when using a variable size item. | ||
getOptionEncoder({} as VariableSizeEncoder<string>, { fixed: true }); | ||
} | ||
|
||
{ | ||
// [getOptionDecoder]: It knows if the decoder is fixed size or variable size. | ||
getOptionDecoder({} as FixedSizeDecoder<void, 0>) satisfies FixedSizeDecoder<Option<void>>; | ||
getOptionDecoder({} as FixedSizeDecoder<string>, { fixed: true }) satisfies FixedSizeDecoder<Option<string>>; | ||
getOptionDecoder({} as FixedSizeDecoder<string>) satisfies VariableSizeDecoder<Option<string>>; | ||
|
||
// @ts-expect-error It cannot be fixed when using a variable size item. | ||
getOptionDecoder({} as VariableSizeDecoder<string>, { fixed: true }); | ||
} | ||
|
||
{ | ||
// [getOptionCodec]: It knows if the codec is fixed size or variable size. | ||
getOptionCodec({} as FixedSizeCodec<void, void, 0>) satisfies FixedSizeCodec<OptionOrNullable<void>, Option<void>>; | ||
getOptionCodec({} as FixedSizeCodec<string>, { fixed: true }) satisfies FixedSizeCodec< | ||
OptionOrNullable<string>, | ||
Option<string> | ||
>; | ||
getOptionCodec({} as FixedSizeCodec<string>) satisfies VariableSizeCodec<OptionOrNullable<string>, Option<string>>; | ||
|
||
// @ts-expect-error It cannot be fixed when using a variable size item. | ||
getOptionCodec({} as VariableSizeCodec<string>, { fixed: true }); | ||
} |
Oops, something went wrong.