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.

Some(T)

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

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Deserialize this value from the given Serde deserializer. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.