Crates.io | field_block |
lib.rs | field_block |
version | 0.3.0 |
source | src |
created_at | 2022-09-30 10:59:07.476375 |
updated_at | 2022-10-02 14:54:08.624378 |
description | A definition language for buffer parsing |
homepage | |
repository | https://github.com/Banyc/field_block |
max_upload_size | |
id | 677231 |
size | 24,184 |
A definition language for buffer parsing.
Adding this crate to a project:
cargo add field_block
Defining fields for a buffer:
fn get_block() -> Block<Name> {
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:
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:
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.