6 releases

0.2.2 May 19, 2022
0.2.1 May 18, 2022
0.2.0 Apr 27, 2022
0.1.2 Mar 21, 2022

#122 in Encoding

Download history 2042/week @ 2022-08-15 2110/week @ 2022-08-22 2784/week @ 2022-08-29 2400/week @ 2022-09-05 1930/week @ 2022-09-12 1571/week @ 2022-09-19 1504/week @ 2022-09-26 1459/week @ 2022-10-03 2214/week @ 2022-10-10 2302/week @ 2022-10-17 1922/week @ 2022-10-24 2494/week @ 2022-10-31 4051/week @ 2022-11-07 5573/week @ 2022-11-14 4854/week @ 2022-11-21 4757/week @ 2022-11-28

19,401 downloads per month
Used in 42 crates (7 directly)

MIT/Apache

55KB
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 libipld.

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(())
}

License

Licensed under either of

at your option.

Dependencies

~0.8–1.5MB
~33K SLoC