Skip to content

Commit

Permalink
Added never type
Browse files Browse the repository at this point in the history
  • Loading branch information
Haaxor1689 committed Nov 26, 2023
1 parent 0392892 commit 6496a84
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
49 changes: 43 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ import { n } from '@haaxor1689/nil';

// Declare object schema with given shape
const User = n.object({
age: n.uint16(),
rank: n.uint16(),
active: n.int32()
});

Expand All @@ -96,7 +96,7 @@ type User = n.output<typeof User>;

// Equivalent to
type User = {
age: number;
rank: number;
active: boolean;
};
```
Expand Down Expand Up @@ -152,6 +152,8 @@ n.array(n.int16(), 'fill');
The `.bytes()` option can be used to interpret a given length in bytes instead of the count of elements.

```ts
import { n } from '@haaxor1689/nil';

// Size will be 256 bytes
n.buffer(256).bytes();
n.string(256).bytes();
Expand All @@ -163,6 +165,8 @@ n.array(n.int8, 256).bytes();
You can load **C** enum values as a string literal union. Only default numbered **C** enums are supported now.

```ts
import { n } from '@haaxor1689/nil';

// Declare enum schema with given options
const Level = n.enum(n.int8(), ['LOW', 'MEDIUM', 'HIGH']);

Expand All @@ -188,12 +192,37 @@ enum Level {
You can access the tuple used to create a given enum with `.options`.

```ts
import { n } from '@haaxor1689/nil';

// Declare enum schema with given options
const Level = n.enum(n.int8(), ['LOW', 'MEDIUM', 'HIGH']);

Level.options; // ["LOW", "MEDIUM", "HIGH"]
```

## Never

If you need a placeholder that represents 0 bytes in the binary data, you can use the never type for that:

```ts
import { n } from '@haaxor1689/nil';

// Declare object schema with given shape
const User = n.object({
empty: n.never(), // represents 0 bytes in buffer
active: n.int32()
});

// Extract the output type
type User = n.output<typeof User>;

// Equivalent to
type User = {
empty: never;
active: boolean;
};
```

## Schema methods

All Nil schemas contain these methods.
Expand All @@ -210,15 +239,17 @@ All Nil schemas contain these methods.
You can provide custom transformation functions for your schemas that will change the output both when parsing from the raw buffer and creating a buffer from the JS object.

```ts
import { n } from '@haaxor1689/nil';

// Define transform that handles calculating `itemCount`
const MySchema = n
.object({
itemCount: n.int16(),
items: n.array(n.int8(), ['itemCount'])
})
.transform(
v => v.items, // Keep only raw items
v => ({ itemCount: v.length, items: v }) // Calculate itemCount
v => v.items, // keep only raw items
v => ({ itemCount: v.length, items: v }) // calculate itemCount
);

// Inferred output type is `number[]`
Expand All @@ -231,6 +262,8 @@ MySchema.toBuffer([1, 2, 3, 4]);
You can also access the current context when creating transformations to reference other attributes from the parent type (if any). The easiest way to do this is by using the `resolvePath` helper function.

```ts
import { n } from '@haaxor1689/nil';

const MySchema = n.object({
hasAlpha: n.bool(),
data: n.array(n.int8(), 'fill').transform(
Expand All @@ -248,13 +281,17 @@ const MySchema = n.object({

### `.fromBuffer`

`.fromBuffer(data: Uint8Array): Output`
```ts
.fromBuffer(data: Uint8Array): Output
```

Tries to parse given buffer into output type of used schema. Throws errors on failure.

### `.toBuffer`

`.toBuffer(value: Output): Uint8Array`
```ts
.toBuffer(value: Output): Uint8Array
```

Tries to serialize a given object into a buffer.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@haaxor1689/nil",
"version": "0.1.0",
"version": "0.1.1",
"description": "TypeScript-first binary data parsing library with static type inference",
"author": "Maroš Beťko <[email protected]>",
"repository": {
Expand Down
18 changes: 17 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,20 @@ export class NilEnum<
}
}

export class NilNever extends NilType<never> {
size() {
return 0;
}

_decode() {
return undefined as never;
}

_encode() {
// Do nothing
}
}

const bool = () => new NilBool({});
const int8 = () => new NilNumber({ bytes: 1, signed: true });
const uint8 = () => new NilNumber({ bytes: 1 });
Expand All @@ -622,6 +636,7 @@ const enum_ = <
type: T,
options: O
): NilEnum<T, O> => new NilEnum({ type, options });
const never = () => new NilNever({});

export {
bool,
Expand All @@ -639,5 +654,6 @@ export {
array,
object,
buffer,
enum_ as enum
enum_ as enum,
never
};

0 comments on commit 6496a84

Please sign in to comment.