Crate gadget_blueprint_serde

Crate gadget_blueprint_serde 

Source
Expand description

Compatibility crate for using the Serde serialization frame with data from tangle_subxt

This crate provides two primary functions:

§Examples

use gadget_blueprint_serde::{new_bounded_string, BoundedVec, Field};
use serde::{Deserialize, Serialize};

#[derive(PartialEq, Debug, Serialize, Deserialize)]
struct Person {
    name: String,
    age: u8,
}

let person = Person {
    name: String::from("John"),
    age: 40,
};

let expected = Field::Struct(
    new_bounded_string("Person"),
    Box::new(BoundedVec(vec![
        (
            new_bounded_string("name"),
            Field::String(new_bounded_string("John")),
        ),
        (new_bounded_string("age"), Field::Uint8(40)),
    ])),
);

// Convert our `Serialize` type to a `tangle_subxt::Field`
let field = gadget_blueprint_serde::to_field(&person).unwrap();
assert_eq!(expected, field);

// Convert our `tangle_subxt::Field` back to a `Person`
let person_deserialized: Person = gadget_blueprint_serde::from_field(field).unwrap();
assert_eq!(person, person_deserialized);

Modules§

error

Structs§

BoundedVec
ByteBuf
Wrapper around Vec<u8> to serialize and deserialize efficiently.

Enums§

Field

Functions§

from_field
Derive an instance of type D from a Field
new_bounded_string
to_field
Derive a Field from an instance of type S