virto-network / virto-sdk

Virto toolset to build simple to use decentralized applications
GNU General Public License v3.0
21 stars 5 forks source link

`scales::Value` data retrieval methods #53

Open olanod opened 6 months ago

olanod commented 6 months ago

scales::Value is a lightweight container that simply holds references to a type registry and the scale encoded data, it allows using serde to for example build a serde_json::Value from the underlying encoded data to make it easy for users to interpret it. However in constrained contexts where its not desirable to have the extra serde_json or similar dependency and allocate a new object having some utility functions on Value to access fields and values can come quite handy.

// e.g. struct that that gets encoded to scale
struct SomeStruct<'a> {
    field_a: u8,
    field_b: &'a str,
}

let value = Value::new(&encoded_some_struct, ty_id, &registry);
// e.g. some way to access a field
let f_a = value.field("field_a")?.as_u8()?;
// e.g. a method that returns an iterator of (field, value)
let f_b = value.fields().filter_map(|(f, _v)| f.eq("field_b").then_some(v) )?.as_str();