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
sourceimpl<T> Optional<T>
impl<T> Optional<T>
sourcepub fn ok(&self, ok: impl FnMut(&T)) where
T: Default,
pub fn ok(&self, ok: impl FnMut(&T)) where
T: Default,
Do things with the provided functions, only if the value is enable.
If the value is Bool(true)
, use the default value instead.
sourcepub fn ok_instead(&self, ok: impl FnMut(&T), default: &T)
pub fn ok_instead(&self, ok: impl FnMut(&T), default: &T)
Same as Self::ok
, but provide default value directly.
sourcepub fn ok_or<Ok, Or, R>(&self, ok: Ok, or: Or) -> R where
T: Default,
Ok: FnMut(&T) -> R,
Or: FnMut() -> R,
pub fn ok_or<Ok, Or, R>(&self, ok: Ok, or: Or) -> R where
T: Default,
Ok: FnMut(&T) -> R,
Or: FnMut() -> R,
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.
sourcepub fn ok_or_instead<Ok, Or, R>(&self, ok: Ok, default: &T, or: Or) -> R where
Ok: FnMut(&T) -> R,
Or: FnMut() -> R,
pub fn ok_or_instead<Ok, Or, R>(&self, ok: Ok, default: &T, or: Or) -> R where
Ok: FnMut(&T) -> R,
Or: FnMut() -> R,
Same as Self::ok_or
, but provide default value directly.
Trait Implementations
sourceimpl<'de, T> Deserialize<'de> for Optional<T> where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Optional<T> where
T: Deserialize<'de>,
sourcefn 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
impl<T> StructuralPartialEq for Optional<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for Optional<T> where
T: RefUnwindSafe,
impl<T> Send for Optional<T> where
T: Send,
impl<T> Sync for Optional<T> where
T: Sync,
impl<T> Unpin for Optional<T> where
T: Unpin,
impl<T> UnwindSafe for Optional<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more