Skip to main content

deserialize

Function deserialize 

Source
pub fn deserialize<T: DeserializeOwned>(
    node: &Node,
    format: &'static dyn Format,
) -> Result<T, Refusal>
Available on crate features json or toml or yaml only.
Expand description

Reads a T from node, a tree format read, resolving each scalar by format’s rules.

The mapping and sequence rules are the same for every format. A struct or a map needs a mapping, and a sequence, a tuple or a tuple struct needs a sequence. An enum is a scalar naming a unit variant, or a mapping of one entry from the variant to its content. An Option is None for a scalar that resolves as null and Some for any other node.

§Errors

A Refusal naming the fault and the bytes it sits at: the node the fault arose in, or the alias that replayed it with Refusal::defined_at naming where its value is written. A missing field sits on the key whose value is the mapping that lacks it, or at the start of the text for the root mapping.

use ctl_input::Limits;
use ctl_input::de::deserialize;
use ctl_input::format::{Format, Reading, YAML};
use serde::Deserialize;

#[derive(Debug, Deserialize, PartialEq)]
struct Config {
    port: u16,
}

let tree = YAML.read("port: '8080'\n", &Limits::NONE, Reading::Strict)?;
let config: Config = deserialize(&tree, &YAML)?;
assert_eq!(config, Config { port: 8080 });