Skip to main content

to_value

Function to_value 

Source
pub fn to_value<T>(value: &T) -> Result<Value, SerError>
where T: Serialize,
Expand description

Convert any Serialize type into a Value.

Structs become Struct, vectors become List, strings/numbers/bools map to their corresponding Value variants.

§Errors

Returns an error if the type contains unsupported serde data (e.g. bytes).

§Examples

use md_tmpl_core::Value;
use serde::Serialize;

#[derive(Serialize)]
struct Agent {
    name: String,
    score: i64,
}

let agent = Agent {
    name: "Alice".into(),
    score: 95,
};
let val = md_tmpl_core::to_value(&agent).unwrap();
assert_eq!(val.get_field("name").unwrap().to_string(), "Alice");
assert_eq!(val.get_field("score").unwrap().to_string(), "95");