Function libipld_core::serde::from_ipld

source ·
pub fn from_ipld<T>(value: Ipld) -> Result<T, SerdeError>
Expand description

Deserialize instances of crate::ipld::Ipld.

§Example

use std::collections::BTreeMap;

use serde::Deserialize;
use libipld_core::ipld::Ipld;
use libipld_core::serde::from_ipld;

#[derive(Deserialize)]
struct Person {
    name: String,
    age: u8,
    hobbies: Vec<String>,
    is_cool: bool,
}

let ipld = Ipld::Map({
    BTreeMap::from([
        ("name".into(), Ipld::String("Hello World!".into())),
        ("age".into(), Ipld::Integer(52)),
        (
            "hobbies".into(),
            Ipld::List(vec![
                Ipld::String("geography".into()),
                Ipld::String("programming".into()),
            ]),
        ),
        ("is_cool".into(), Ipld::Bool(true)),
    ])
});

let person = from_ipld(ipld);
assert!(matches!(person, Ok(Person { .. })));