stefans_utils/bool/
map.rs

1use crate::map::Map;
2use core::fmt;
3
4impl Map for bool {
5    fn ok(self) -> Result<Self, Self> {
6        if self { Ok(true) } else { Err(false) }
7    }
8
9    fn ok_or<E>(self, err: E) -> Result<Self, E> {
10        if self { Ok(true) } else { Err(err) }
11    }
12
13    fn map<T>(self, f: impl FnOnce() -> T) -> Option<T> {
14        if self { Some(f()) } else { None }
15    }
16
17    fn and_then<T>(self, f: impl FnOnce() -> Option<T>) -> Option<T> {
18        if self { f() } else { None }
19    }
20
21    fn unwrap(self) -> Self {
22        self.expect("Expected 'true', got 'false'")
23    }
24
25    fn expect(self, message: impl fmt::Display) -> Self {
26        if self { self } else { panic!("{message}") }
27    }
28}