Skip to content

Commit

Permalink
impr: Add a default bin export. (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
iwoplaza authored Sep 15, 2024
1 parent 9eb48a3 commit 6a2c1ee
Show file tree
Hide file tree
Showing 39 changed files with 283 additions and 295 deletions.
6 changes: 3 additions & 3 deletions apps/examples/binaryMesh/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'node:fs/promises';
import { BufferReader, BufferWriter } from 'typed-binary';
import bin from 'typed-binary';
import { Mesh } from './schemas';

const DEFAULT_MESH: Mesh = {
Expand All @@ -17,7 +17,7 @@ const DEFAULT_MESH: Mesh = {
async function loadMesh(): Promise<Mesh> {
try {
const buffer = await fs.readFile('./binaryMesh/mesh.bin');
const reader = new BufferReader(buffer);
const reader = new bin.BufferReader(buffer);

return Mesh.read(reader);
} catch (e) {
Expand All @@ -28,7 +28,7 @@ async function loadMesh(): Promise<Mesh> {
async function saveMesh(mesh: Mesh): Promise<void> {
try {
const buffer = Buffer.alloc(Mesh.measure(mesh).size);
const writer = new BufferWriter(buffer);
const writer = new bin.BufferWriter(buffer);

Mesh.write(writer, mesh);

Expand Down
21 changes: 10 additions & 11 deletions apps/examples/binaryMesh/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import type { Parsed } from 'typed-binary';
import { arrayOf, dynamicArrayOf, i32, object } from 'typed-binary';
import bin from 'typed-binary';

export const Vertex = object({
x: i32,
y: i32,
z: i32,
export const Vertex = bin.object({
x: bin.i32,
y: bin.i32,
z: bin.i32,
});

export const Polygon = object({
vertices: arrayOf(Vertex, 3),
export const Polygon = bin.object({
vertices: bin.arrayOf(Vertex, 3),
});

export const Mesh = object({
faces: dynamicArrayOf(Polygon),
export const Mesh = bin.object({
faces: bin.dynamicArrayOf(Polygon),
});

// Helpful for the top-most level element
export type Mesh = Parsed<typeof Mesh>;
export type Mesh = bin.Parsed<typeof Mesh>;
6 changes: 3 additions & 3 deletions apps/examples/customSchema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
// Run with `npm run example:customSchema`
//

import { type Parsed, object } from 'typed-binary';
import bin from 'typed-binary';
import { writeAndRead } from '../__util';
import { radians } from './radians';

/*
* ROTATION
*/

type Rotation = Parsed<typeof Rotation>;
const Rotation = object({
type Rotation = bin.Parsed<typeof Rotation>;
const Rotation = bin.object({
roll: radians,
pitch: radians,
yaw: radians,
Expand Down
12 changes: 6 additions & 6 deletions apps/examples/dog/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
// Run with `npm run example:dog`
//

import { type Parsed, f32, i32, object, string, tupleOf } from 'typed-binary';
import bin from 'typed-binary';

// Describing the Dog schema
const Dog = object({
name: string,
position: tupleOf([f32, f32, f32]),
age: i32,
const Dog = bin.object({
name: bin.string,
position: bin.tupleOf([bin.f32, bin.f32, bin.f32]),
age: bin.i32,
});

// Creating a type-alias for ease-of-use.
type Dog = Parsed<typeof Dog>;
type Dog = bin.Parsed<typeof Dog>;

// Creating a 'Dog' object.
const dog: Dog = {
Expand Down
28 changes: 10 additions & 18 deletions apps/examples/genericEnumTypes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,35 @@
// Run with `npm run example:genericEnumTypes`
//

import {
BufferReader,
BufferWriter,
bool,
genericEnum,
i32,
object,
string,
} from 'typed-binary';
import bin from 'typed-binary';

enum AnimalType {
DOG = 0,
CAT = 1,
}

// Generic (enum) object schema
const Animal = genericEnum(
const Animal = bin.genericEnum(
{
nickname: string,
age: i32,
nickname: bin.string,
age: bin.i32,
},
{
[AnimalType.DOG]: object({
[AnimalType.DOG]: bin.object({
// Animal can be a dog
breed: string,
breed: bin.string,
}),
[AnimalType.CAT]: object({
[AnimalType.CAT]: bin.object({
// Animal can be a cat
striped: bool,
striped: bin.bool,
}),
},
);

// A buffer to serialize into/out of
const buffer = Buffer.alloc(16);
const writer = new BufferWriter(buffer);
const reader = new BufferReader(buffer);
const writer = new bin.BufferWriter(buffer);
const reader = new bin.BufferReader(buffer);

// Writing an Animal
Animal.write(writer, {
Expand Down
28 changes: 10 additions & 18 deletions apps/examples/genericTypes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,30 @@
// Run with `npm run example:genericTypes`
//

import {
BufferReader,
BufferWriter,
bool,
generic,
i32,
object,
string,
} from 'typed-binary';
import bin from 'typed-binary';

// Generic object schema
const Animal = generic(
const Animal = bin.generic(
{
nickname: string,
age: i32,
nickname: bin.string,
age: bin.i32,
},
{
dog: object({
dog: bin.object({
// Animal can be a dog
breed: string,
breed: bin.string,
}),
cat: object({
cat: bin.object({
// Animal can be a cat
striped: bool,
striped: bin.bool,
}),
},
);

// A buffer to serialize into/out of
const buffer = Buffer.alloc(16);
const writer = new BufferWriter(buffer);
const reader = new BufferReader(buffer);
const writer = new bin.BufferWriter(buffer);
const reader = new bin.BufferReader(buffer);

// Writing an Animal
Animal.write(writer, {
Expand Down
10 changes: 5 additions & 5 deletions apps/examples/inferredShowcase/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { type Parsed, i32, object, string } from 'typed-binary';
import bin from 'typed-binary';

// Describing the Dog schema.
const Dog = object({
const Dog = bin.object({
/** The name of the doggy. */
name: string,
name: bin.string,
/** The dog's age in dog years. */
age: i32,
age: bin.i32,
});

// Creating a type-alias for ease-of-use.
type Dog = Parsed<typeof Dog>;
type Dog = bin.Parsed<typeof Dog>;

// Creating a 'Dog' object.
const dog: Dog = {
Expand Down
45 changes: 22 additions & 23 deletions apps/examples/recursiveTypes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,28 @@
// Run with `npm run example:recursiveTypes`
//

import type { Parsed } from 'typed-binary';
import { generic, i32, keyed, object, optional, string } from 'typed-binary';
import bin from 'typed-binary';

type Expression = Parsed<typeof Expression>;
const Expression = keyed('expression', (Expression) =>
generic(
type Expression = bin.Parsed<typeof Expression>;
const Expression = bin.keyed('expression', (Expression) =>
bin.generic(
{},
{
multiply: object({
multiply: bin.object({
a: Expression,
b: Expression,
}),
negate: object({
negate: bin.object({
inner: Expression,
}),
int_literal: object({
value: i32,
int_literal: bin.object({
value: bin.i32,
}),
},
),
);

const expr: Parsed<typeof Expression> = {
const expr: bin.Parsed<typeof Expression> = {
type: 'multiply',
a: {
type: 'negate',
Expand All @@ -39,21 +38,21 @@ const expr: Parsed<typeof Expression> = {
},
};

type Instruction = Parsed<typeof Instruction>;
const Instruction = object({
target_variable: string,
expression: optional(Expression),
type Instruction = bin.Parsed<typeof Instruction>;
const Instruction = bin.object({
target_variable: bin.string,
expression: bin.optional(Expression),
});

type Complex = Parsed<typeof Complex>;
const Complex = keyed('complex' as const, (Complex) =>
object({
label: string,
inner: optional(Complex),
cycle: keyed('cycle' as const, (Cycle) =>
object({
value: string,
next: optional(Cycle),
type Complex = bin.Parsed<typeof Complex>;
const Complex = bin.keyed('complex' as const, (Complex) =>
bin.object({
label: bin.string,
inner: bin.optional(Complex),
cycle: bin.keyed('cycle' as const, (Cycle) =>
bin.object({
value: bin.string,
next: bin.optional(Cycle),
}),
),
}),
Expand Down
10 changes: 5 additions & 5 deletions apps/examples/stateMachine/connection.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { byte, f32, i32, object } from 'typed-binary';
import bin from 'typed-binary';
import { TriggerCondition } from './triggerCondition';

export const ConnectionTemplate = object({
targetNodeIndex: i32,
export const ConnectionTemplate = bin.object({
targetNodeIndex: bin.i32,
/**
* The duration of the transition in Minecraft ticks
*/
transitionDuration: f32,
transitionEasing: byte,
transitionDuration: bin.f32,
transitionEasing: bin.byte,
triggerCondition: TriggerCondition,
});

Expand Down
8 changes: 4 additions & 4 deletions apps/examples/stateMachine/graph.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dynamicArrayOf, i32, object } from 'typed-binary';
import bin from 'typed-binary';
import { NodeTemplate } from './node';

export const Graph = object({
entryNode: i32,
nodes: dynamicArrayOf(NodeTemplate),
export const Graph = bin.object({
entryNode: bin.i32,
nodes: bin.dynamicArrayOf(NodeTemplate),
});
14 changes: 7 additions & 7 deletions apps/examples/stateMachine/node.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { bool, dynamicArrayOf, f32, i32, object, string } from 'typed-binary';
import bin from 'typed-binary';
import { ConnectionTemplate } from './connection';

export const NodeTemplate = object({
animationKey: string,
startFrame: i32,
playbackSpeed: f32,
looping: bool,
connections: dynamicArrayOf(ConnectionTemplate),
export const NodeTemplate = bin.object({
animationKey: bin.string,
startFrame: bin.i32,
playbackSpeed: bin.f32,
looping: bin.bool,
connections: bin.dynamicArrayOf(ConnectionTemplate),
});
33 changes: 17 additions & 16 deletions apps/examples/stateMachine/triggerCondition.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { byte, generic, keyed, object } from 'typed-binary';
import type { Parsed } from 'typed-binary';
import bin from 'typed-binary';

type TriggerCondition = Parsed<typeof TriggerCondition>;
export const TriggerCondition = keyed('trigger-condition', (TriggerCondition) =>
generic(
{},
{
'core:state': object({
state: byte,
}),
'core:animation_finished': object({}),
'core:not': object({
condition: TriggerCondition,
}),
},
),
type TriggerCondition = bin.Parsed<typeof TriggerCondition>;
export const TriggerCondition = bin.keyed(
'trigger-condition',
(TriggerCondition) =>
bin.generic(
{},
{
'core:state': bin.object({
state: bin.byte,
}),
'core:animation_finished': bin.object({}),
'core:not': bin.object({
condition: TriggerCondition,
}),
},
),
);

export enum MobState {
Expand Down
Loading

0 comments on commit 6a2c1ee

Please sign in to comment.