finchers_ext/option/
mod.rs

1//! Extensions for `Option`
2
3mod map_some;
4mod ok_or_else;
5
6pub use self::map_some::MapSome;
7pub use self::ok_or_else::OkOrElse;
8
9use finchers_core::Endpoint;
10use finchers_core::endpoint::assert_output;
11
12/// A set of extension methods which is available when the output is value is an `Option`.
13pub trait EndpointOptionExt<T>: Endpoint<Output = Option<T>> + Sized {
14    /// Create an endpoint which will map the value to a new type with given function.
15    fn map_some<F, U>(self, f: F) -> MapSome<Self, F>
16    where
17        F: FnOnce(T) -> U + Clone + Send + Sync,
18    {
19        assert_output::<_, Option<U>>(self::map_some::new(self, f))
20    }
21
22    /// Create an endpoint which will transform the returned value into a `Result`.
23    fn ok_or_else<F, U>(self, f: F) -> OkOrElse<Self, F>
24    where
25        F: FnOnce() -> U + Clone + Send + Sync,
26    {
27        assert_output::<_, Result<T, U>>(self::ok_or_else::new(self, f))
28    }
29}
30
31impl<E, T> EndpointOptionExt<T> for E
32where
33    E: Endpoint<Output = Option<T>>,
34{
35}