pub fn is_present_option<T: Presentable>(value: &Option<T>) -> bool {
match value {
None => false,
Some(v) => v.is_present(),
}
}
pub trait Presentable {
fn is_present(&self) -> bool;
}
impl Presentable for String {
fn is_present(&self) -> bool {
!self.trim().is_empty()
}
}
impl Presentable for bool {
fn is_present(&self) -> bool {
true
}
}
impl<T> Presentable for Vec<T> {
fn is_present(&self) -> bool {
!self.is_empty()
}
}
macro_rules! impl_presentable_numeric {
($($t:ty),*) => {
$(
impl Presentable for $t {
fn is_present(&self) -> bool {
true
}
}
)*
};
}
impl_presentable_numeric!(i8, i16, i32, i64, u8, u16, u32, u64, f32, f64);
pub fn is_accepted(value: &str) -> bool {
matches!(value.to_lowercase().as_str(), "yes" | "on" | "1" | "true")
}
pub fn is_accepted_bool(value: bool) -> bool {
value
}
pub fn is_declined(value: &str) -> bool {
matches!(value.to_lowercase().as_str(), "no" | "off" | "0" | "false")
}
pub fn is_declined_bool(value: bool) -> bool {
!value
}
pub fn is_filled(value: &str) -> bool {
!value.trim().is_empty()
}