#serialization #serde #cbor #serialize-deserialize

no-std serde_ipld_dagcbor

IPLD DAG-CBOR support for Serde

13 unstable releases (5 breaking)

0.6.1 Apr 24, 2024
0.6.0 Mar 26, 2024
0.5.0 Mar 18, 2024
0.4.2 Sep 12, 2023
0.1.2 Mar 21, 2022

#99 in Encoding

Download history 2214/week @ 2024-07-21 2119/week @ 2024-07-28 3193/week @ 2024-08-04 2294/week @ 2024-08-11 2705/week @ 2024-08-18 2353/week @ 2024-08-25 2759/week @ 2024-09-01 2781/week @ 2024-09-08 2832/week @ 2024-09-15 2871/week @ 2024-09-22 2053/week @ 2024-09-29 2048/week @ 2024-10-06 2256/week @ 2024-10-13 1743/week @ 2024-10-20 1792/week @ 2024-10-27 1692/week @ 2024-11-03

7,591 downloads per month
Used in 147 crates (22 directly)

MIT/Apache

63KB
1.5K SLoC

Serde IPLD DAG-CBOR

Crates.io Documentation

This is a Serde implementation for DAG-CBOR. It can be use in conjunction with ipld-core.

The underlying library for CBOR encoding/decoding is cbor4ii and the Serde implementation is also heavily based on their code.

This crate started as a fork of serde_cbor, thanks everyone involved there.

Usage

Storing and loading Rust types is easy and requires only minimal modifications to the program code.

use serde_derive::{Deserialize, Serialize};
use std::error::Error;
use std::fs::File;
use std::io::BufReader;

// Types annotated with `Serialize` can be stored as DAG-CBOR.
// To be able to load them again add `Deserialize`.
#[derive(Debug, Serialize, Deserialize)]
struct Mascot {
    name: String,
    species: String,
    year_of_birth: u32,
}

fn main() -> Result<(), Box<dyn Error>> {
    let ferris = Mascot {
        name: "Ferris".to_owned(),
        species: "crab".to_owned(),
        year_of_birth: 2015,
    };

    let ferris_file = File::create("examples/ferris.cbor")?;
    // Write Ferris to the given file.
    // Instead of a file you can use any type that implements `io::Write`
    // like a HTTP body, database connection etc.
    serde_ipld_dagcbor::to_writer(ferris_file, &ferris)?;

    let tux_file = File::open("examples/tux.cbor")?;
    let tux_reader = BufReader::new(tux_file);
    // Load Tux from a file.
    // Serde IPLD DAG-CBOR performs roundtrip serialization meaning that
    // the data will not change in any way.
    let tux: Mascot = serde_ipld_dagcbor::from_reader(tux_reader)?;

    println!("{:?}", tux);
    // prints: Mascot { name: "Tux", species: "penguin", year_of_birth: 1996 }

    Ok(())
}

Features

codec

The codec feature is enabled by default, it provides the Codec trait, which enables encoding and decoding independent of the IPLD Codec. The minimum supported Rust version (MSRV) can significantly be reduced to 1.64 by disabling this feature.

no-cid-as-bytes

Sometimes it is desired that a CID is not accidentally deserialized into bytes. This can happen because the intermediate serde data model does not retain enough information to be able to differentiate between a bytes container and a CID container when there is a conflicting choice to be made, as in the case of some enum cases. The no-cid-as-bytes feature can be enabled in order to error at runtime in such cases.

The problem with that feature is, that it breaks Serde's derive attributes for internally tagged enums (#[serde(tag = "sometag")]) and untagged enums (#serde(untagged)). If this feature is enabled and you still need similar functionality, you could implement a deserializer manually. Examples of how to do that are in the enum example.

License

Licensed under either of

at your option.

Dependencies

~2.5MB
~57K SLoC