1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use std::error::Error;

use crate::base::Warn;

mod sealed {
    pub trait Option {}
    pub trait Result<T, E> {}

    impl<T> Option for std::option::Option<T> {}
    impl<T, E> Result<T, E> for std::result::Result<T, E> {}
}

/// Integration between [`Option`] and [`Warn`]
///
/// Note that this trait is sealed, so it is implemented for [`Option<T>`] and cannot
/// be implemented for anything else.
pub trait OptionExt: sealed::Option {
    /// Yield error `error` into sink `warn` if the option contains `None`. The option
    /// is returned unchanged.
    fn or_warn_with<E: Error>(self, error: E, warn: &mut impl Warn<E>) -> Self;
}

/// Integration between [`Result`] and [`Warn`]
///
/// Note that this trait is sealed, so it is implemented for [`Result<T, E>`] and cannot
/// be implemented for anything else.
pub trait ResultExt<T, E: Error>: sealed::Result<T, E> {
    /// Pass the error from result to the sink
    ///
    /// If the result contains a value, then this value is returned. Otherwise, the error
    /// is passed to `warn`, and `None` is returned. The error also can be converted to a
    /// corresponding type via [`From`] trait before passing into `warn`.
    fn or_warn<D: From<E> + Error>(self, warn: &mut impl Warn<D>) -> Option<T>;

    /// Same as [`ResultExt::or_warn()`], but `func` is applied to error before passing
    /// into `warn`
    fn or_warn_map<D: Error>(self, func: impl FnOnce(E) -> D, warn: &mut impl Warn<D>)
        -> Option<T>;
}

impl<T> OptionExt for Option<T> {
    #[inline]
    fn or_warn_with<E: Error>(self, error: E, warn: &mut impl Warn<E>) -> Self {
        if self.is_none() {
            warn.warn(error);
        }
        self
    }
}

impl<T, E: Error> ResultExt<T, E> for Result<T, E> {
    #[inline]
    fn or_warn<D: From<E> + Error>(self, warn: &mut impl Warn<D>) -> Option<T> {
        self.or_warn_map(From::from, warn)
    }

    #[inline]
    fn or_warn_map<D: Error>(
        self,
        func: impl FnOnce(E) -> D,
        warn: &mut impl Warn<D>,
    ) -> Option<T> {
        match self {
            Ok(val) => Some(val),
            Err(err) => {
                warn.warn(func(err));
                None
            }
        }
    }
}