use crate::DefaultValueConstructor;
use crate::Value;
pub fn value_false() -> Value<bool> {
Value::default()
}
pub fn value_true() -> Value<bool> {
default_value(|| true)
}
pub fn default_string(s: impl ToString + Clone + 'static) -> impl Fn() -> Value<String> {
move || {
let s_clone = s.clone();
default_value(|| s_clone.to_string())
}
}
macro_rules! make_default_fn {
($name:ident, $ty:ty, $doc:expr) => {
#[doc = $doc]
pub fn $name(int: $ty) -> impl Fn() -> Value<$ty> {
move || default_value(|| int)
}
};
}
make_default_fn!(
default_u8,
u8,
"Returns a closure that creates a [`Value<u8>`] with the given default."
);
make_default_fn!(
default_u16,
u16,
"Returns a closure that creates a [`Value<u16>`] with the given default."
);
make_default_fn!(
default_u32,
u32,
"Returns a closure that creates a [`Value<u32>`] with the given default."
);
make_default_fn!(
default_u64,
u64,
"Returns a closure that creates a [`Value<u64>`] with the given default."
);
make_default_fn!(
default_i8,
i8,
"Returns a closure that creates a [`Value<i8>`] with the given default."
);
make_default_fn!(
default_i16,
i16,
"Returns a closure that creates a [`Value<i16>`] with the given default."
);
make_default_fn!(
default_i32,
i32,
"Returns a closure that creates a [`Value<i32>`] with the given default."
);
make_default_fn!(
default_i64,
i64,
"Returns a closure that creates a [`Value<i64>`] with the given default."
);
pub fn default_value<F, T>(f: F) -> Value<T>
where
F: Fn() -> T,
{
Value {
value: f(),
default: DefaultValueConstructor::Custom(f()),
}
}