#codec #deserialize #serialization #utilities #serde-derive

codee

Easy and flexible way of encoding and decoding data into either strings or bytes

4 releases

0.2.0 Aug 23, 2024
0.1.2 Jul 8, 2024
0.1.1 Jul 7, 2024
0.1.0 Jul 7, 2024

#674 in Encoding

Download history 387/week @ 2024-07-04 105/week @ 2024-07-11 80/week @ 2024-07-18 900/week @ 2024-07-25 1983/week @ 2024-08-01 1564/week @ 2024-08-08 2035/week @ 2024-08-15 2720/week @ 2024-08-22 2810/week @ 2024-08-29

9,546 downloads per month
Used in 9 crates (2 directly)

MIT/Apache

38KB
620 lines

Codee

Crates.io Docs MIT/Apache 2.0 Build Status

Easy and flexible way of encoding and decoding data into either strings or bytes.

This crate provides generic traits for Encoders and Decoders as well as several implementations for commonly used (de)serializer crates.

This makes it easily possible to abstract away the serialization and deserialization independent of the concrete crate used. You can write a function like this:

use codee::{CodecError, Decoder, Encoder};

fn store_value<T, Codec>(value: T) -> Result<(), CodecError<<Codec as Encoder<T>>::Error, <Codec as Decoder<T>>::Error>>
where
    Codec: Encoder<T, Encoded = String> + Decoder<T, Encoded = str>,
{
    let encoded = Codec::encode(&value).map_err(CodecError::Encode)?;
    let decoded = Codec::decode(&encoded).map_err(CodecError::Decode)?;

    Ok(())
}

// Then we can use it like this:

use codee::string::{JsonSerdeCodec, FromToStringCodec};

#[derive(serde::Serialize, serde::Deserialize)]
struct MyStruct {
    field: usize,
}

store_value::<i32, FromToStringCodec>(42);
store_value::<MyStruct, JsonSerdeCodec>(MyStruct { field: 42 });

Dependencies

~0.3–1.6MB
~34K SLoC