Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
refactor(experimental): codecs: refactor message out of codec asser…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
buffalojoec authored and steveluscher committed Feb 29, 2024
1 parent 2ae6db6 commit c091204
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 30 deletions.
22 changes: 4 additions & 18 deletions packages/codecs-core/src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,27 +168,22 @@ export function isFixedSize(codec: { fixedSize: number } | { maxSize?: number })

export function assertIsFixedSize<TFrom, TSize extends number>(
encoder: FixedSizeEncoder<TFrom, TSize> | VariableSizeEncoder<TFrom>,
message?: string,
): asserts encoder is FixedSizeEncoder<TFrom, TSize>;
export function assertIsFixedSize<TTo, TSize extends number>(
decoder: FixedSizeDecoder<TTo, TSize> | VariableSizeDecoder<TTo>,
message?: string,
): asserts decoder is FixedSizeDecoder<TTo, TSize>;
export function assertIsFixedSize<TFrom, TTo extends TFrom, TSize extends number>(
codec: FixedSizeCodec<TFrom, TTo, TSize> | VariableSizeCodec<TFrom, TTo>,
message?: string,
): asserts codec is FixedSizeCodec<TFrom, TTo, TSize>;
export function assertIsFixedSize<TSize extends number>(
codec: { fixedSize: TSize } | { maxSize?: number },
message?: string,
): asserts codec is { fixedSize: TSize };
export function assertIsFixedSize(
codec: { fixedSize: number } | { maxSize?: number },
message?: string,
): asserts codec is { fixedSize: number } {
if (!isFixedSize(codec)) {
// TODO: Coded error.
throw new Error(message ?? 'Expected a fixed-size codec, got a variable-size one.');
throw new Error('Expected a fixed-size codec, got a variable-size one.');
}
}

Expand All @@ -202,28 +197,19 @@ export function isVariableSize(codec: { fixedSize: number } | { maxSize?: number
return !isFixedSize(codec);
}

export function assertIsVariableSize<T>(
encoder: Encoder<T>,
message?: string,
): asserts encoder is VariableSizeEncoder<T>;
export function assertIsVariableSize<T>(
decoder: Decoder<T>,
message?: string,
): asserts decoder is VariableSizeDecoder<T>;
export function assertIsVariableSize<T>(encoder: Encoder<T>): asserts encoder is VariableSizeEncoder<T>;
export function assertIsVariableSize<T>(decoder: Decoder<T>): asserts decoder is VariableSizeDecoder<T>;
export function assertIsVariableSize<TFrom, TTo extends TFrom>(
codec: Codec<TFrom, TTo>,
message?: string,
): asserts codec is VariableSizeCodec<TFrom, TTo>;
export function assertIsVariableSize(
codec: { fixedSize: number } | { maxSize?: number },
message?: string,
): asserts codec is { maxSize?: number };
export function assertIsVariableSize(
codec: { fixedSize: number } | { maxSize?: number },
message?: string,
): asserts codec is { maxSize?: number } {
if (!isVariableSize(codec)) {
// TODO: Coded error.
throw new Error(message ?? 'Expected a variable-size codec, got a fixed-size one.');
throw new Error('Expected a variable-size codec, got a fixed-size one.');
}
}
14 changes: 12 additions & 2 deletions packages/codecs-core/src/reverse-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import { combineCodec } from './combine-codec';
export function reverseEncoder<TFrom, TSize extends number>(
encoder: FixedSizeEncoder<TFrom, TSize>,
): FixedSizeEncoder<TFrom, TSize> {
assertIsFixedSize(encoder, 'Cannot reverse a codec of variable size.');
try {
assertIsFixedSize(encoder);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Cannot reverse a codec of variable size.');
}
return createEncoder({
...encoder,
write: (value: TFrom, bytes, offset) => {
Expand All @@ -32,7 +37,12 @@ export function reverseEncoder<TFrom, TSize extends number>(
export function reverseDecoder<TTo, TSize extends number>(
decoder: FixedSizeDecoder<TTo, TSize>,
): FixedSizeDecoder<TTo, TSize> {
assertIsFixedSize(decoder, 'Cannot reverse a codec of variable size.');
try {
assertIsFixedSize(decoder);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Cannot reverse a codec of variable size.');
}
return createDecoder({
...decoder,
read: (bytes, offset) => {
Expand Down
14 changes: 12 additions & 2 deletions packages/codecs-data-structures/src/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export function getBooleanEncoder<TSize extends number>(
export function getBooleanEncoder(config: BooleanCodecConfig<NumberEncoder>): Encoder<boolean>;
export function getBooleanEncoder(config: BooleanCodecConfig<NumberEncoder> = {}): Encoder<boolean> {
const size = config.size ?? getU8Encoder();
assertIsFixedSize(size, 'Codec [bool] requires a fixed size.');
try {
assertIsFixedSize(size);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Codec [bool] requires a fixed size.');
}
return mapEncoder(size, (value: boolean) => (value ? 1 : 0));
}

Expand All @@ -58,7 +63,12 @@ export function getBooleanDecoder<TSize extends number>(
export function getBooleanDecoder(config: BooleanCodecConfig<NumberDecoder>): Decoder<boolean>;
export function getBooleanDecoder(config: BooleanCodecConfig<NumberDecoder> = {}): Decoder<boolean> {
const size = config.size ?? getU8Decoder();
assertIsFixedSize(size, 'Codec [bool] requires a fixed size.');
try {
assertIsFixedSize(size);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Codec [bool] requires a fixed size.');
}
return mapDecoder(size, (value: number | bigint): boolean => Number(value) === 1);
}

Expand Down
28 changes: 24 additions & 4 deletions packages/codecs-data-structures/src/nullable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,18 @@ export function getNullableEncoder<TFrom>(

const isZeroSizeItem = isFixedSize(item) && isFixedSize(prefix) && item.fixedSize === 0;
if (fixed || isZeroSizeItem) {
assertIsFixedSize(item, 'Fixed nullables can only be used with fixed-size codecs.');
assertIsFixedSize(prefix, 'Fixed nullables can only be used with fixed-size prefix.');
try {
assertIsFixedSize(item);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Fixed nullables can only be used with fixed-size codecs.');
}
try {
assertIsFixedSize(prefix);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Fixed nullables can only be used with fixed-size prefix.');
}
const fixedSize = prefix.fixedSize + item.fixedSize;
return createEncoder({
fixedSize,
Expand Down Expand Up @@ -131,8 +141,18 @@ export function getNullableDecoder<TTo>(
let fixedSize: number | null = null;
const isZeroSizeItem = isFixedSize(item) && isFixedSize(prefix) && item.fixedSize === 0;
if (fixed || isZeroSizeItem) {
assertIsFixedSize(item, 'Fixed nullables can only be used with fixed-size codecs.');
assertIsFixedSize(prefix, 'Fixed nullables can only be used with fixed-size prefix.');
try {
assertIsFixedSize(item);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Fixed nullables can only be used with fixed-size codecs.');
}
try {
assertIsFixedSize(prefix);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Fixed nullables can only be used with fixed-size prefix.');
}
fixedSize = prefix.fixedSize + item.fixedSize;
}

Expand Down
28 changes: 24 additions & 4 deletions packages/options/src/option-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,18 @@ export function getOptionEncoder<TFrom>(

const isZeroSizeItem = isFixedSize(item) && isFixedSize(prefix) && item.fixedSize === 0;
if (fixed || isZeroSizeItem) {
assertIsFixedSize(item, 'Fixed options can only be used with fixed-size codecs.');
assertIsFixedSize(prefix, 'Fixed options can only be used with fixed-size prefix.');
try {
assertIsFixedSize(item);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Fixed options can only be used with fixed-size codecs.');
}
try {
assertIsFixedSize(prefix);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Fixed options can only be used with fixed-size prefix.');
}
const fixedSize = prefix.fixedSize + item.fixedSize;
return createEncoder({
fixedSize,
Expand Down Expand Up @@ -139,8 +149,18 @@ export function getOptionDecoder<TTo>(
let fixedSize: number | null = null;
const isZeroSizeItem = isFixedSize(item) && isFixedSize(prefix) && item.fixedSize === 0;
if (fixed || isZeroSizeItem) {
assertIsFixedSize(item, 'Fixed options can only be used with fixed-size codecs.');
assertIsFixedSize(prefix, 'Fixed options can only be used with fixed-size prefix.');
try {
assertIsFixedSize(item);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Fixed options can only be used with fixed-size codecs.');
}
try {
assertIsFixedSize(prefix);
} catch (e) {
// TODO: Coded error, also proper catch handling
throw new Error('Fixed options can only be used with fixed-size prefix.');
}
fixedSize = prefix.fixedSize + item.fixedSize;
}

Expand Down

0 comments on commit c091204

Please sign in to comment.