rcodec
This Rust library provides combinators for purely functional, declarative encoding and decoding of binary data. Its design is largely derived from that of the scodec library for Scala.
Usage
Add the following dependencies to your Cargo.toml
:
[]
= "1.0.0"
= "1.0.0"
Then, in your crate:
use *;
use byte_vector;
use *;
Examples
The codec module provides a number of predefined codecs. In the following example, we use the uint32
codec to encode a u32
value to a ByteVector
representation, and then decode the ByteVector
back to its u32
representation:
let codec = uint32;
let v0 = 258u32;
let bv = codec.encode.unwrap;
assert_eq;
let v1 = codec.decode.unwrap.value;
assert_eq;
Automatic binding to structs when encoding/decoding is supported via the pl-hlist crate:
let codec = struct_codec!;
let s0 = TestStruct ;
let bv = codec.encode.unwrap;
assert_eq;
let s1 = codec.decode.unwrap.value;
assert_eq;
Here's an example of a more complex codec for a fictitious binary packet format, which uses a number of the built-in combinators:
let magic = byte_vector!;
let header_codec = struct_codec!;
let packet_codec = struct_codec!;
More examples of specific codecs can be found in the tests for src/codec.rs
as well as in tests/lib.rs
.
License
rcodec
is distributed under an MIT license. See LICENSE for more details.