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
//! Extensions for `Option`

mod map_some;
mod ok_or_else;

pub use self::map_some::MapSome;
pub use self::ok_or_else::OkOrElse;

use finchers_core::Endpoint;
use finchers_core::endpoint::assert_output;

/// A set of extension methods which is available when the output is value is an `Option`.
pub trait EndpointOptionExt<T>: Endpoint<Output = Option<T>> + Sized {
    /// Create an endpoint which will map the value to a new type with given function.
    fn map_some<F, U>(self, f: F) -> MapSome<Self, F>
    where
        F: FnOnce(T) -> U + Clone + Send + Sync,
    {
        assert_output::<_, Option<U>>(self::map_some::new(self, f))
    }

    /// Create an endpoint which will transform the returned value into a `Result`.
    fn ok_or_else<F, U>(self, f: F) -> OkOrElse<Self, F>
    where
        F: FnOnce() -> U + Clone + Send + Sync,
    {
        assert_output::<_, Result<T, U>>(self::ok_or_else::new(self, f))
    }
}

impl<E, T> EndpointOptionExt<T> for E
where
    E: Endpoint<Output = Option<T>>,
{
}