Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/banyc/field_block
A definition language for buffer parsing. No code gen. Restrict business code to outside of the codec.
https://github.com/banyc/field_block
codec network parsing runtime zero-copy
Last synced: about 2 hours ago
JSON representation
A definition language for buffer parsing. No code gen. Restrict business code to outside of the codec.
- Host: GitHub
- URL: https://github.com/banyc/field_block
- Owner: Banyc
- License: mit
- Created: 2022-09-28T14:39:25.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2022-10-02T14:54:03.000Z (about 2 years ago)
- Last Synced: 2024-10-21T01:37:03.296Z (29 days ago)
- Topics: codec, network, parsing, runtime, zero-copy
- Language: Rust
- Homepage:
- Size: 47.9 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Field Block
A definition language for buffer parsing.
## Usage
Adding this crate to a project:
```bash
cargo add field_block
```Defining fields for a buffer:
```rust
fn get_block() -> Block {
let mut block = Block::new();
block.add_field(
Name::FixedVarInt, //
Def::VarInt(U64::Fixed(0xdeadbeef)),
);
block.add_field(
Name::VarInt, //
Def::VarInt(U64::Var),
);
block.add_field(
Name::BytesFixedLen, //
Def::Bytes(Len::Fixed(1)),
);
block.add_field(
Name::BytesVarLen, //
Def::Bytes(Len::Var),
);
block.add_field(
Name::FixedBytes, //
Def::FixedBytes(vec![0xba, 0xad, 0xf0, 0x0d]),
);
block
}#[derive(Debug, PartialEq, Eq, Hash, Clone)]
enum Name {
FixedVarInt,
VarInt,
BytesFixedLen,
BytesVarLen,
FixedBytes,
}impl FieldName for Name {}
```Encoding a buffer:
```rust
let block = get_block();let mut values = HashMap::new();
values.insert(Name::VarInt, Val::VarInt(0x1234));
let vec = vec![1];
values.insert(Name::BytesFixedLen, Val::Bytes(&vec));
let vec = vec![1, 2, 3];
values.insert(Name::BytesVarLen, Val::Bytes(&vec));let mut vec = vec![0; 1024];
let end = block.to_bytes(&values, &mut vec).unwrap();
```Decoding a buffer:
```rust
let block = get_block();let vec = vec![0 | 0xc0, 0, 0, 0, 0xde, 0xad, 0xbe, 0xef, 0x12 | 0x40, 0x34, 1, 3, 1, 2, 3, 0xba, 0xad, 0xf0, 0x0d];
let mut values = HashMap::new();
let end = block.to_values(&vec, &mut values).unwrap();
let ValInfo { value, pos } = values.get(&Name::VarInt).unwrap();
let value = value.varint().unwrap();println!("Field VarInt has a value {} at pos {}", value, pos);
```See unit tests for examples.