Skip to main content

from_value

Function from_value 

Source
pub fn from_value<'de, T>(value: &'de Value) -> Result<T, DeError>
where T: Deserialize<'de>,
Expand description

Convert a Value back into any Deserialize type.

This is the inverse of to_value. Enums are supported:

  • Value::Str("Variant") deserializes as a unit variant.
  • Value::Struct({"__kind__": "Variant", ...}) deserializes as a struct variant.

§Errors

Returns an error if the value shape doesn’t match the target type.

§Examples

use md_tmpl_core::Value;
use serde::Deserialize;

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

let val = Value::new_struct([
    ("name", Value::Str("Alice".into())),
    ("score", Value::Int(95)),
]);
let agent: Agent = md_tmpl_core::from_value(&val).unwrap();
assert_eq!(
    agent,
    Agent {
        name: "Alice".into(),
        score: 95
    }
);