simple_parse 0.4.1

A declarative converter for Rust type to and from binary
Documentation

simple_parse

crates.io mio Lines of Code

A declarative converter for Rust type to and from binary.

It provides basic implementations for most standard Rust types and also provides a derive macro to automatically implement the trait on your own types !

For bit level control or more advanced features, take a look at deku.

Usage

use ::simple_parse::{SpRead, SpWrite};

#[derive(SpRead, SpWrite)]
pub struct SomeStruct {
    pub some_field: u8,
    #[sp(endian="big")]
    pub items: Vec<u32>,
}

let mut cursor: &[u8] = &[
    1,                      // some_field
    2,0,0,0,0,0,0,0,        // items.len()
    0xDE,0xAD,0xBE,0xEF,    // items[0]
    0xBA,0xDC,0x0F,0xFE     // items[1]
];

// Decode bytes into a struct
let mut my_struct = SomeStruct::from_reader(&mut cursor)?;

/// Modify the struct
my_struct.items.push(0xFFFFFFFF);

/// Encode modified struct into bytes
let mut dst_buf: Vec<u8> = Vec::new();
my_struct.to_writer(&mut dst_buf)?;
//dst_buf == [1,3,0,0,0,0,0,0,0,0xDE,0xAD,0xBE,0xEF,0xBA,0xDC,0x0F,0xFE,0xFF,0xFF,0xFF,0xFF]

For complete examples see : examples

Features

Feature Description
No-Copy parsing simple_parse is able to generate references into byte slices (see struct.rs)
count Annotating a dynamically sized field with count allows it's number of items to live somewhere else in the struct. The default is to simply prepend the number of items as a u64
endian Annotating structs/fields with endian gives control over how numbers will be parsed
Custom read/write Custom parsers can be writen for individual fields when simple_parse doesnt have the adequate default implementation