7 unstable releases (3 breaking)

0.4.1 Mar 28, 2024
0.4.0 Mar 26, 2024
0.3.2 Mar 14, 2024
0.2.0 Feb 23, 2024
0.1.0 Jan 25, 2024

#505 in Encoding

Download history 609/week @ 2024-08-14 383/week @ 2024-08-21 510/week @ 2024-08-28 538/week @ 2024-09-04 698/week @ 2024-09-11 677/week @ 2024-09-18 453/week @ 2024-09-25 421/week @ 2024-10-02 282/week @ 2024-10-09 528/week @ 2024-10-16 510/week @ 2024-10-23 467/week @ 2024-10-30 570/week @ 2024-11-06 808/week @ 2024-11-13 1219/week @ 2024-11-20 1276/week @ 2024-11-27

3,945 downloads per month
Used in 135 crates (11 directly)

MIT/Apache

92KB
2K SLoC

IPLD core

Crates.io Documentation

This crate provides core types for interoperating with IPLD. Codecs are not part of this crate, they are independent, but rely on ipld-core.

The code is based on libipld-core. The major difference is that Serde is used a lot more for better interoperability with the rest of the Rust ecosystem.

Usage

Codec independent code

One of the main features of IPLD is that your Codec is independent of the data you are encoding. Hence it's common that you want to have your code to be independent of a specific code, but rather be generic.

Here's a full example of a function that can encode data with both serde_ipld_dagcbor or serde_ipld_dagjson:

use std::str;

use ipld_core::codec::Codec;
use serde::{Deserialize, Serialize};
use serde_ipld_dagcbor::codec::DagCborCodec;
use serde_ipld_dagjson::codec::DagJsonCodec;

#[derive(Deserialize, Serialize)]
struct Tree {
    height: u8,
    age: u8,
}

fn encode_generic<C, T>(value: &T) -> Result<Vec<u8>, C::Error>
where
    C: Codec<T>,
{
    C::encode_to_vec(value)
}

fn main() {
    let tree = Tree {
        height: 12,
        age: 91,
    };

    let cbor_encoded = encode_generic::<DagCborCodec, _>(&tree);
    #[allow(clippy::format_collect)]
    let cbor_hex = cbor_encoded
        .unwrap()
        .iter()
        .map(|byte| format!("{:02x}", byte))
        .collect::<String>();
    // CBOR encoded: https://cbor.nemo157.com/#value=a2666865696768740c63616765185b
    println!("CBOR encoded: https://cbor.nemo157.com/#value={}", cbor_hex);
    let json_encoded = encode_generic::<DagJsonCodec, _>(&tree).unwrap();
    // JSON encoded: {"height":12,"age":91}
    println!("JSON encoded: {}", str::from_utf8(&json_encoded).unwrap());
}

If you are only interested in the links (CIDs) of an encoded IPLD object, then you can extract them them directly with [Codec::links()]:

use ipld_core::{codec::{Codec, Links}, ipld, cid::Cid};
use serde_ipld_dagjson::codec::DagJsonCodec;

fn main() {
    let cid = Cid::try_from("bafkreibme22gw2h7y2h7tg2fhqotaqjucnbc24deqo72b6mkl2egezxhvy").unwrap();
    let data = ipld!({"some": {"nested": cid}, "or": [cid, cid], "more": true});

    let mut encoded = Vec::new();
    DagJsonCodec::encode(&mut encoded, &data).unwrap();

    let links = DagJsonCodec::links(&encoded).unwrap().collect::<Vec<_>>();
    // Extracted links: [Cid(bafkreibme22gw2h7y2h7tg2fhqotaqjucnbc24deqo72b6mkl2egezxhvy), Cid(bafkreibme22gw2h7y2h7tg2fhqotaqjucnbc24deqo72b6mkl2egezxhvy), Cid(bafkreibme22gw2h7y2h7tg2fhqotaqjucnbc24deqo72b6mkl2egezxhvy)]
    println!("Extracted links: {:?}", links);
}

Feature flags

  • std (enabled by default): Makes the error implement std::error::Error and the Codec trait available.
  • codec (enabled by default): 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.
  • serde: Enables support for Serde serialization into/deserialization from the Ipld enum.
  • arb: Enables support for property based testing.

License

Licensed under either of

at your option.

Dependencies

~1.8–2.5MB
~51K SLoC