1pub(crate) trait CheckExt<T: ?Sized> {
2 fn check<E>(&self, other: &T, error: E) -> Result<(), E>;
3
4 fn check_or<E>(&self, other: &T, error: impl FnOnce() -> E) -> Result<(), E>;
5}
6
7impl<T> CheckExt<T> for T
8where
9 T: PartialEq,
10{
11 fn check<E>(&self, other: &T, error: E) -> Result<(), E> {
12 self.eq(other).then_some(()).ok_or(error)
13 }
14
15 fn check_or<E>(&self, other: &T, error: impl FnOnce() -> E) -> Result<(), E> {
16 self.eq(other).then_some(()).ok_or_else(error)
17 }
18}
19
20impl CheckExt<str> for Option<String> {
21 fn check<E>(&self, other: &str, error: E) -> Result<(), E> {
22 self.as_deref().eq(&Some(other)).then_some(()).ok_or(error)
23 }
24
25 fn check_or<E>(&self, other: &str, error: impl FnOnce() -> E) -> Result<(), E> {
26 self.as_deref()
27 .eq(&Some(other))
28 .then_some(())
29 .ok_or_else(error)
30 }
31}