Skip to content

Commit 607607f

Browse files
committed
Create a bunch of examples.
1 parent 278354e commit 607607f

File tree

3 files changed

+136
-2
lines changed

3 files changed

+136
-2
lines changed

README.md

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
[![Version](https://img.shields.io/crates/v/byte-transcoder)](https://crates.io/crates/byte-transcoder)
44
[![Docs](https://docs.rs/byte-transcoder/badge.svg)](https://docs.rs/byte-transcoder)
5-
[![License](https://img.shields.io/crates/l/byte-transcoder)](https://crates.io/crates/byte-transcoder)
65

76
[![JSR](https://jsr.io/badges/@todd/byte-transcoder)](https://jsr.io/@todd/byte-transcoder)
87
[![JSR Score](https://jsr.io/badges/@todd/byte-transcoder/score)](https://jsr.io/@todd/byte-transcoder)
@@ -23,7 +22,62 @@ Typescript only. Helps write `number`s as specific primitives (e.g. `u8`/`u16`/`
2322

2423
## Examples
2524

26-
Read `tests/` to learn how to use!
25+
Read `examples/`, `tests/`, and `src-ts/**/*.test.ts` for more examples!
26+
27+
### ByteReader
28+
29+
#### Typescript
30+
31+
```typescript
32+
type Payload = { gameId: string; joinCode: string };
33+
34+
const bytes = new Uint8Array([
35+
// <your binary data>
36+
]);
37+
const byteReader = new ByteReader(bytes);
38+
39+
const payload: Payload = {
40+
gameId: byteReader.readUuid(),
41+
joinCode: byteReader.readString()
42+
};
43+
```
44+
45+
#### Rust
46+
47+
```rust
48+
struct Payload {
49+
game_id: Uuid,
50+
join_code: String,
51+
}
52+
53+
let bytes: Vec<u8> = vec![
54+
// <your binary data>
55+
];
56+
let mut byte_reader = ByteReader::new(bytes);
57+
58+
let payload = Payload {
59+
game_id: byte_reader.read_uuid()?,
60+
join_code: byte_reader.read_string()?,
61+
}
62+
```
63+
64+
### ByteWriter
65+
66+
#### Typescript
67+
68+
```typescript
69+
type Payload = { gameId: string; joinCode: string };
70+
const payload: Payload = {
71+
gameId: "24399a6c-c4a9-4053-9b2d-4199107fb567",
72+
joinCode: "12345"
73+
};
74+
75+
const byteWriter = new ByteWriter();
76+
byteWriter.writeUuid(payload.gameId);
77+
byteWriter.writeString(payload.joinCode);
78+
79+
const bytes: Uint8Array = byteWriter.getBytes();
80+
```
2781

2882
## Developers
2983

examples/transcode.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use byte_transcoder::{reader::ByteReader, reader_error::ByteReaderResult};
2+
use uuid::Uuid;
3+
4+
#[allow(dead_code)]
5+
#[derive(Debug)]
6+
pub struct Payload {
7+
game_id: Uuid,
8+
join_code: String,
9+
lobby: Vec<Account>,
10+
}
11+
12+
#[derive(Debug)]
13+
pub struct Account {
14+
pub account_id: Uuid,
15+
pub username: String,
16+
}
17+
18+
fn main() -> ByteReaderResult<()> {
19+
#[rustfmt::skip]
20+
let bytes: Vec<u8> = vec![
21+
// Game ID (UUIDv4)
22+
26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
23+
24+
// Join Code (String)
25+
4, 84, 111, 100, 100,
26+
27+
// Lobby size (u8)
28+
2,
29+
30+
// Account 1
31+
// (UUIDv4)
32+
26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
33+
// (String)
34+
4, 84, 111, 100, 100,
35+
36+
// Account 2
37+
// (UUIDv4)
38+
26, 159, 68, 107, 159, 116, 65, 60, 136, 44, 246, 216, 52, 76, 64, 30,
39+
// (String)
40+
4, 84, 111, 100, 100,
41+
];
42+
43+
let mut byte_reader: ByteReader = ByteReader::new(&bytes);
44+
45+
let game_id: Uuid = byte_reader.read_uuid()?;
46+
let join_code: String = byte_reader.read_string()?;
47+
48+
let mut lobby: Vec<Account> = Vec::new();
49+
let lobby_len: u8 = byte_reader.read_u8()?;
50+
for _ in 0..lobby_len {
51+
lobby.push(Account {
52+
account_id: byte_reader.read_uuid()?,
53+
username: byte_reader.read_string()?,
54+
});
55+
}
56+
57+
println!(
58+
"{:?}",
59+
Payload {
60+
game_id,
61+
join_code,
62+
lobby,
63+
}
64+
);
65+
Ok(())
66+
}

tests/test_examples.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::process::{Command, Output};
2+
3+
fn run_example(name: &str) {
4+
let output: Output = Command::new("cargo")
5+
.args(["run", "--example", name])
6+
.output()
7+
.expect("Failed to execute example");
8+
assert!(output.status.success());
9+
}
10+
11+
#[test]
12+
fn test_transcode() {
13+
run_example("transcode");
14+
}

0 commit comments

Comments
 (0)