Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions apps/examples/genericEnumTypes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ const Animal = bin.genericEnum(
age: bin.i32,
},
{
[AnimalType.DOG]: bin.object({
[AnimalType.DOG]: {
// Animal can be a dog
breed: bin.string,
}),
[AnimalType.CAT]: bin.object({
},
[AnimalType.CAT]: {
// Animal can be a cat
striped: bin.bool,
}),
},
},
);

Expand Down
41 changes: 1 addition & 40 deletions packages/typed-binary/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,6 @@
import * as bin from './main-api.ts';
export * from './main-api.ts';
export { bin };
export default bin;

export { getSystemEndianness } from './util.ts';
export { MaxValue, SubTypeKey, Schema } from './structure/types.ts';
export {
BoolSchema,
Float16Schema,
Float32Schema,
Int16Schema,
Int32Schema,
Int8Schema,
StringSchema,
Uint16Schema,
Uint32Schema,
Uint8Schema,
/** @deprecated Use Uint8Schema instead. */
Uint8Schema as ByteSchema,
} from './structure/baseTypes.ts';
export { ArraySchema } from './structure/array.ts';
export { CharsSchema } from './structure/chars.ts';
export { DynamicArraySchema } from './structure/dynamicArray.ts';
export { KeyedSchema } from './structure/keyed.ts';
export { ObjectSchema, GenericObjectSchema } from './structure/object.ts';
export { OptionalSchema } from './structure/optional.ts';
export { TupleSchema } from './structure/tuple.ts';
export { TypedArraySchema } from './structure/typedArray.ts';

export type { AnyObjectSchema } from './structure/object.ts';
export type {
Unwrap,
UnwrapRecord,
UnwrapArray,
IKeyedSchema,
Ref,
IRefResolver,
ISchema,
AnyKeyedSchema,
AnySchema,
AnySchemaWithProperties,
ISchemaWithProperties,
} from './structure/types.ts';
export type { ParseUnwrapped } from './utilityTypes.ts';
export { SubTypeKey } from './structure/types.ts';
38 changes: 30 additions & 8 deletions packages/typed-binary/src/main-api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
export { arrayOf } from './structure/array.ts';
export { bool, byte, i8, u8, i16, u16, i32, u32, f16, f32, string } from './structure/baseTypes.ts';
export { chars } from './structure/chars.ts';
export { array, type Array } from './structure/array.ts';
export {
bool,
i8,
u8,
i16,
u16,
i32,
u32,
f16,
f32,
string,
type Bool,
type Int8,
type Uint8,
type Int16,
type Uint16,
type Int32,
type Uint32,
type Float16,
type Float32,
type String,
} from './structure/baseTypes.ts';
export { chars, type Chars } from './structure/chars.ts';
export { concat } from './structure/concat.ts';
export { dynamicArrayOf } from './structure/dynamicArray.ts';
export { keyed } from './structure/keyed.ts';
export { object, generic, genericEnum } from './structure/object.ts';
export { dynamicArray, type DynamicArray } from './structure/dynamicArray.ts';
export { struct, type Struct } from './structure/struct.ts';
export { generic, genericEnum, type GenericObjectSchema } from './structure/genericObject.ts';
export { optional } from './structure/optional.ts';
export { tupleOf } from './structure/tuple.ts';
export { tuple, type Tuple } from './structure/tuple.ts';
export {
f32Array,
f64Array,
Expand All @@ -17,6 +38,7 @@ export {
u32Array,
u8Array,
u8ClampedArray,
type TypedArraySchema,
} from './structure/typedArray.ts';
export { MaxValue } from './structure/types.ts';

Expand All @@ -26,4 +48,4 @@ export { Measurer } from './io/measurer.ts';
export { UnresolvedReferenceError, ValidationError } from './error.ts';

export type { Endianness, IMeasurer, ISerialInput, ISerialOutput } from './io/types.ts';
export type { Parsed } from './utilityTypes.ts';
export type { ExtractIn, ExtractOut, Schema } from './structure/types.ts';
46 changes: 22 additions & 24 deletions packages/typed-binary/src/structure/array.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import { ValidationError } from '../error.ts';
import { Measurer } from '../io/measurer.ts';
import type { IMeasurer, ISerialInput, ISerialOutput } from '../io/types.ts';
import type { ParseUnwrapped } from '../utilityTypes.ts';
import { type AnySchema, type IRefResolver, MaxValue, Schema, type Unwrap } from './types.ts';
import { type ExtractIn, type ExtractOut, MaxValue, type Schema } from './types.ts';

export class ArraySchema<TElement extends AnySchema> extends Schema<Unwrap<TElement>[]> {
private elementSchema: TElement;

constructor(
private readonly _unstableElementSchema: TElement,
public readonly length: number,
) {
super();
export interface Array<TElement> extends Schema<
readonly ExtractIn<TElement>[],
ExtractOut<TElement>[]
> {
readonly elementSchema: TElement;
readonly length: number;
}

// In case this array isn't part of a keyed chain,
// let's assume the inner type is stable.
this.elementSchema = _unstableElementSchema;
}
class ArraySchema<TElement extends Schema> implements Array<TElement> {
readonly elementSchema: TElement;
readonly length: number;

override resolveReferences(ctx: IRefResolver): void {
this.elementSchema = ctx.resolve(this._unstableElementSchema);
constructor(elementSchema: TElement, length: number) {
this.elementSchema = elementSchema;
this.length = length;
}

override write(output: ISerialOutput, values: ParseUnwrapped<TElement>[]): void {
write(output: ISerialOutput, values: readonly ExtractIn<TElement>[]): void {
if (values.length !== this.length) {
throw new ValidationError(`Expected array of length ${this.length}, got ${values.length}`);
}
Expand All @@ -32,11 +30,11 @@ export class ArraySchema<TElement extends AnySchema> extends Schema<Unwrap<TElem
}
}

override read(input: ISerialInput): ParseUnwrapped<TElement>[] {
const array: ParseUnwrapped<TElement>[] = [];
read(input: ISerialInput): ExtractOut<TElement>[] {
const array: ExtractOut<TElement>[] = [];

for (let i = 0; i < this.length; ++i) {
array.push(this.elementSchema.read(input) as ParseUnwrapped<TElement>);
array.push(this.elementSchema.read(input));
}

return array;
Expand All @@ -54,8 +52,8 @@ export class ArraySchema<TElement extends AnySchema> extends Schema<Unwrap<TElem
return this.elementSchema.measure(MaxValue).size * this.length;
}

override measure(
values: ParseUnwrapped<TElement>[] | MaxValue,
measure(
values: readonly ExtractIn<TElement>[] | MaxValue,
measurer: IMeasurer = new Measurer(),
): IMeasurer {
for (let i = 0; i < this.length; ++i) {
Expand All @@ -66,8 +64,8 @@ export class ArraySchema<TElement extends AnySchema> extends Schema<Unwrap<TElem
}
}

// @__NO_SIDE_EFFECTS__
export function arrayOf<TSchema extends AnySchema>(
/*#__NO_SIDE_EFFECTS__*/
export function array<TSchema extends Schema>(
elementSchema: TSchema,
length: number,
): ArraySchema<TSchema> {
Expand Down
71 changes: 46 additions & 25 deletions packages/typed-binary/src/structure/baseTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { MaxValue, Schema } from './types.ts';
// BOOL
////

export class BoolSchema extends Schema<boolean> {
export interface Bool extends Schema<boolean> {}

class BoolSchema implements Bool {
/**
* The maximum number of bytes this schema can take up.
*
Expand All @@ -27,13 +29,18 @@ export class BoolSchema extends Schema<boolean> {
}
}

export const bool: BoolSchema = new BoolSchema();
export const bool: Bool = new BoolSchema();

////
// STRING
////

export class StringSchema extends Schema<string> {
export interface String extends Schema<string, string> {}

class StringSchema implements String {
declare readonly $in: string;
declare readonly $out: string;

private static _cachedEncoder: TextEncoder | undefined;

private static get _encoder() {
Expand Down Expand Up @@ -61,13 +68,15 @@ export class StringSchema extends Schema<string> {
}
}

export const string: StringSchema = new StringSchema();
export const string: String = new StringSchema();

////
// i8
////

export class Int8Schema extends Schema<number> {
export interface Int8 extends Schema<number> {}

class Int8Schema implements Int8 {
/**
* The maximum number of bytes this schema can take up.
*
Expand All @@ -88,13 +97,15 @@ export class Int8Schema extends Schema<number> {
}
}

export const i8: Int8Schema = new Int8Schema();
export const i8: Int8 = new Int8Schema();

////
// u8
////

export class Uint8Schema extends Schema<number> {
export interface Uint8 extends Schema<number> {}

class Uint8Schema implements Uint8 {
/**
* The maximum number of bytes this schema can take up.
*
Expand All @@ -115,18 +126,15 @@ export class Uint8Schema extends Schema<number> {
}
}

export const u8: Uint8Schema = new Uint8Schema();

/**
* Alias for `bin.u8`
*/
export const byte: Uint8Schema = u8;
export const u8: Uint8 = new Uint8Schema();

////
// i16
////

export class Int16Schema extends Schema<number> {
export interface Int16 extends Schema<number> {}

class Int16Schema implements Int16 {
/**
* The maximum number of bytes this schema can take up.
*
Expand All @@ -147,13 +155,15 @@ export class Int16Schema extends Schema<number> {
}
}

export const i16: Int16Schema = new Int16Schema();
export const i16: Int16 = new Int16Schema();

////
// u16
////

export class Uint16Schema extends Schema<number> {
export interface Uint16 extends Schema<number> {}

class Uint16Schema implements Uint16 {
/**
* The maximum number of bytes this schema can take up.
*
Expand All @@ -174,13 +184,18 @@ export class Uint16Schema extends Schema<number> {
}
}

export const u16: Uint16Schema = new Uint16Schema();
export const u16: Uint16 = new Uint16Schema();

////
// i32
////

export class Int32Schema extends Schema<number> {
export interface Int32 extends Schema<number, number> {}

class Int32Schema implements Int32 {
declare readonly $in: number;
declare readonly $out: number;

/**
* The maximum number of bytes this schema can take up.
*
Expand All @@ -201,13 +216,15 @@ export class Int32Schema extends Schema<number> {
}
}

export const i32: Int32Schema = new Int32Schema();
export const i32: Int32 = new Int32Schema();

////
// u32
////

export class Uint32Schema extends Schema<number> {
export interface Uint32 extends Schema<number> {}

class Uint32Schema implements Uint32 {
/**
* The maximum number of bytes this schema can take up.
*
Expand All @@ -228,13 +245,15 @@ export class Uint32Schema extends Schema<number> {
}
}

export const u32: Uint32Schema = new Uint32Schema();
export const u32: Uint32 = new Uint32Schema();

////
// f16
////

export class Float16Schema extends Schema<number> {
export interface Float16 extends Schema<number> {}

class Float16Schema implements Float16 {
/**
* The maximum number of bytes this schema can take up.
*
Expand All @@ -255,13 +274,15 @@ export class Float16Schema extends Schema<number> {
}
}

export const f16: Float16Schema = new Float16Schema();
export const f16: Float16 = new Float16Schema();

////
// f32
////

export class Float32Schema extends Schema<number> {
export interface Float32 extends Schema<number> {}

class Float32Schema implements Float32 {
/**
* The maximum number of bytes this schema can take up.
*
Expand All @@ -282,4 +303,4 @@ export class Float32Schema extends Schema<number> {
}
}

export const f32: Float32Schema = new Float32Schema();
export const f32: Float32 = new Float32Schema();
Loading