Skip to content

Commit

Permalink
ready to go readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr Martian committed Jan 8, 2025
1 parent 46f8462 commit ad67400
Showing 1 changed file with 84 additions and 2 deletions.
86 changes: 84 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<img src="https://img.shields.io/crates/v/pbf.svg?logo=rust&logoColor=white" alt="crate">
</a>
<a href="https://bundlejs.com/?q=pbf-ts&treeshake=%5B*%5D">
<img src="https://deno.bundlejs.com/badge?q=pbf-ts&treeshake=[*]" alt="bundle">
<img src="https://img.shields.io/bundlejs/size/pbf-ts" alt="bundle">
</a>
<a href="https://www.npmjs.com/package/pbf-ts">
<img src="https://img.shields.io/npm/dm/pbf-ts.svg" alt="downloads">
Expand Down Expand Up @@ -59,6 +59,88 @@ yarn add pbf-ts
deno install pbf-ts
```

### Typescript Examples

```ts
import { readFileSync } from 'fs';
import { Pbf } from 'pbf-ts';

// Reading:
const pbf = new Pbf(readFileSync(path));

// Writing:
const pbf = new Pbf();
pbf.writeVarintField(1, 1);
// ...
const result = pbf.commit();
```

More complex example:

```ts
/** Building a class to test with. */
class Test {
a = 0;
b = 0;
c = 0;
/**
* @param pbf - the Protobuf object to read from
* @param end - the position to stop at
*/
constructor(pbf: Protobuf, end = 0) {
pbf.readFields(Test.read, this, end);
}
/**
* @param t - the test object to write.
* @param pbf - the Protobuf object to write to.
*/
static writeMessage(t: Test, pbf: Protobuf): void {
pbf.writeVarintField(1, t.a);
pbf.writeFloatField(2, t.b);
pbf.writeSVarintField(3, t.c);
}

/**
* @param tag - the tag to read.
* @param test - the test to modify
* @param pbf - the Protobuf object to read from
*/
static read(tag: number, test: Test, pbf: Protobuf): void {
if (tag === 1) test.a = pbf.readVarint();
else if (tag === 2) test.b = pbf.readFloat();
else if (tag === 3) test.c = pbf.readSVarint();
else throw new Error(`Unexpected tag: ${tag}`);
}

/**
* @returns - a new test object
*/
static newTest(): Test {
return { a: 1, b: 2.2, c: -3 } as Test;
}

/**
* @returns - a new default test object
*/
static newTestDefault(): Test {
return { a: 0, b: 0, c: 0 } as Test;
}
}

// Writing the message
const pbf = new Protobuf();
const t = Test.newTest();
pbf.writeMessage(5, Test.writeMessage, t);
const data = pbf.commit();
expect(data).toEqual(new Uint8Array([42, 9, 8, 1, 21, 205, 204, 12, 64, 24, 5]));

// Reading the message
const pbf2 = new Protobuf(data);
expect(pbf2.readTag()).toEqual({ tag: 5, type: Protobuf.Bytes });
const t2 = new Test(pbf2, pbf2.readVarint() + pbf2.pos);
expect(t2).toEqual({ a: 1, b: 2.200000047683716, c: -3 } as Test);
```

### Rust

Install the package:
Expand All @@ -75,7 +157,7 @@ or add the following to your `Cargo.toml`:
pbf = "0.3"
```

## Examples
### Rust Examples

```rust
use pbf::{ProtoRead, ProtoWrite, Protobuf, Field, Type};
Expand Down

0 comments on commit ad67400

Please sign in to comment.