pub fn to_ipld<T>(value: T) -> Result<Ipld, SerdeError>where
T: Serialize,
Expand description
Serialize into instances of crate::ipld::Ipld
.
All Rust types can be serialized to crate::ipld::Ipld
, here is a list of how they are
converted:
- bool ->
Ipld::Bool
- i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, usize ->
Ipld::Integer
- f32, f64 ->
Ipld::Float
- char, String ->
Ipld::String
- slices ->
Ipld::List
- struct
- struct ->
Ipld::Map
- newtype struct -> the value the struct wraps
- tuple struct ->
Ipld::List
- unit struct -> cannot be serialized, it errors
- struct ->
- enum:
- unit variant ->
Ipld::String
of the variant name - newtype variant -> single element
Ipld::Map
, key: variant name, value: the one the newtype wraps - tuple variant -> single element
Ipld::Map
, key: variant name, value:Ipld::List
- struct variant -> single element
Ipld::Map
, key: variant name, value:Ipld::Map
- unit variant ->
- unit (
()
) -> cannot be serialized, it errors
There are also common compound types that are supported:
std::option::Option
-> eitheIpld::Null
or the value- [
serde_bytes::ByteBuf
] ->Ipld::Bytes
- lists (like e.g.
std::vec::Vec
) ->Ipld::List
- maps (like e.g.
std::collections::BTreeMap
) ->Ipld::Map
cid::Cid
->Ipld::Link
§Example
use serde::Serialize;
use libipld_core::ipld::Ipld;
use libipld_core::serde::to_ipld;
#[derive(Serialize)]
struct Person {
name: String,
age: u8,
hobbies: Vec<String>,
is_cool: bool,
}
let person = Person {
name: "Hello World!".into(),
age: 52,
hobbies: vec!["geography".into(), "programming".into()],
is_cool: true,
};
let ipld = to_ipld(person);
assert!(matches!(ipld, Ok(Ipld::Map(_))));