#codec #serialization #deserialize #data-encoding #utilities

codee

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

5 unstable releases

0.3.0 Jan 10, 2025
0.2.0 Aug 23, 2024
0.1.2 Jul 8, 2024
0.1.1 Jul 7, 2024
0.1.0 Jul 7, 2024

#860 in Encoding

Download history 4285/week @ 2024-10-30 4512/week @ 2024-11-06 3298/week @ 2024-11-13 3136/week @ 2024-11-20 5448/week @ 2024-11-27 5157/week @ 2024-12-04 5923/week @ 2024-12-11 5265/week @ 2024-12-18 5050/week @ 2024-12-25 5634/week @ 2025-01-01 9451/week @ 2025-01-08 8150/week @ 2025-01-15 7616/week @ 2025-01-22 9594/week @ 2025-01-29 10623/week @ 2025-02-05 8490/week @ 2025-02-12

37,499 downloads per month
Used in 78 crates (6 directly)

MIT/Apache

38KB
621 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.2–1.6MB
~34K SLoC