Merge pull request #1697 from axodotdev/booloroptext

(config-migration) Add BoolOr<T> and Option<BoolOr<T>> helper functions.
This commit is contained in:
Ellen Marie Dash 2025-01-15 11:42:50 -05:00 committed by GitHub
commit 4ac5a5bc42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -171,3 +171,40 @@ pub enum BoolOr<T> {
/// They gave a more interesting value
Val(T),
}
impl<T> BoolOr<T> {
/// Returns true if it is `BoolOr::Bool(true)`, or if it contains a value.
pub fn is_not_false(&self) -> bool {
match self {
BoolOr::Bool(b) => *b,
BoolOr::Val(_v) => true,
}
}
}
/// Extension trait to provide `is_none_or_false` and `is_some_and_not_false`.
pub trait BoolOrOptExt
where
Self: Sized,
{
/// Given an `Option<BoolOr<T>>`, returns `true` if the value is
/// `None` or `Some(BoolOr::Bool(false))`.
fn is_none_or_false(&self) -> bool;
/// Given an `Option<BoolOr<T>>`, returns `true` if the value is
/// a `Some(BoolOr::Val(...))` or `Some(BoolOr::Bool(true))`.
fn is_some_and_not_false(&self) -> bool;
}
impl<T> BoolOrOptExt for Option<BoolOr<T>> {
fn is_none_or_false(&self) -> bool {
!self.is_some_and_not_false()
}
fn is_some_and_not_false(&self) -> bool {
if let Some(item) = self {
item.is_not_false()
} else {
false
}
}
}