Enum yaml_peg::serialize::Optional [−][src]
pub enum Optional<T> {
Bool(bool),
Some(T),
}
This is supported on crate feature
serde
only.Expand description
A data type that can support listed items, or inline it if there is single item.
use serde::Deserialize;
use yaml_peg::{node, serialize::Optional};
#[derive(Deserialize)]
struct Content {
img: Optional<Img>,
}
#[derive(Deserialize)]
struct Img {
src: String,
}
impl Default for Img {
fn default() -> Self {
Self { src: "~/img/.desktop.png".to_string() }
}
}
let n_disabled = node!({"img" => false});
let n_default = node!({"img" => true});
let n_enabled = node!({"img" => node!({"src" => "img/1.png"})});
let disabled = Content::deserialize(n_disabled).unwrap();
let default = Content::deserialize(n_default).unwrap();
let enabled = Content::deserialize(n_enabled).unwrap();
let mut doc = String::new();
disabled.img.ok(|img| doc += &img.src);
doc += ":";
default.img.ok(|img| doc += &img.src);
doc += ":";
enabled.img.ok(|img| doc += &img.src);
assert_eq!(":~/img/.desktop.png:img/1.png", doc);
Variants
Bool(bool)
Boolean value, false means disable, true means default.
Tuple Fields of Bool
0: bool
The custom option.
Implementations
Do things with the provided functions, only if the value is enable.
If the value is Bool(true)
, use the default value instead.
Same as Self::ok
, but provide default value directly.
Do things with the provided functions, the functions can return the value in both cases. (enabled / disabled)
If the value is Bool(true)
, use the default value instead.
Same as Self::ok_or
, but provide default value directly.
Trait Implementations
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more