finchers_ext/result/
mod.rs

1//! Extensions for `Result`
2
3mod and_then;
4mod err_into;
5mod map_err;
6mod map_ok;
7mod or_else;
8mod unwrap_ok;
9
10pub use self::and_then::AndThen;
11pub use self::err_into::ErrInto;
12pub use self::map_err::MapErr;
13pub use self::map_ok::MapOk;
14pub use self::or_else::OrElse;
15pub use self::unwrap_ok::UnwrapOk;
16
17use finchers_core::endpoint::assert_output;
18use finchers_core::{Endpoint, Error, IsResult};
19
20/// A set of extension methods which is available when the output value is a `Result`.
21pub trait EndpointResultExt<A, B>: Endpoint<Output = Result<A, B>> + Sized {
22    /// Annotate that the successful type of associated type `Output` is equal to `T`.
23    #[inline(always)]
24    fn as_ok<T>(self) -> Self
25    where
26        Self::Output: IsResult<Ok = T>,
27    {
28        self
29    }
30
31    /// Annotate that the error type of associated type `Output` is equal to `E`.
32    #[inline(always)]
33    fn as_err<E>(self) -> Self
34    where
35        Self::Output: IsResult<Err = E>,
36    {
37        self
38    }
39
40    /// Create an endpoint which will map the successful value to a new type with given function.
41    fn map_ok<F, U>(self, f: F) -> MapOk<Self, F>
42    where
43        F: FnOnce(A) -> U + Clone + Send + Sync,
44    {
45        assert_output::<_, Result<U, B>>(self::map_ok::new(self, f))
46    }
47
48    /// Create an endpoint which will map the error value to a new type with given function.
49    fn map_err<F, U>(self, f: F) -> MapErr<Self, F>
50    where
51        F: FnOnce(B) -> U + Clone + Send + Sync,
52    {
53        assert_output::<_, Result<A, U>>(self::map_err::new(self, f))
54    }
55
56    /// Create an endpoint which will convert the error value to a new type.
57    fn err_into<U>(self) -> ErrInto<Self, U>
58    where
59        B: Into<U>,
60    {
61        assert_output::<_, Result<A, U>>(self::err_into::new(self))
62    }
63
64    /// Create an endpoint which will map the successful value to a `Result` with given function.
65    fn and_then<F, U>(self, f: F) -> AndThen<Self, F>
66    where
67        F: FnOnce(A) -> Result<U, B> + Clone + Send + Sync,
68    {
69        assert_output::<_, Result<U, B>>(self::and_then::new(self, f))
70    }
71
72    /// Create an endpoint which will map the error value to a `Result` with given function.
73    fn or_else<F, U>(self, f: F) -> OrElse<Self, F>
74    where
75        F: FnOnce(B) -> Result<A, U> + Clone + Send + Sync,
76    {
77        assert_output::<_, Result<A, U>>(self::or_else::new(self, f))
78    }
79
80    /// Create an endpoint which will return the content of an `Ok`.
81    ///
82    /// The term "unwrap" does not means that an unwinding will occur if the value is an `Err`.
83    /// Instead, the error value will be converted to an `Error` and handled the state internally.
84    fn unwrap_ok(self) -> UnwrapOk<Self>
85    where
86        B: Into<Error>,
87    {
88        assert_output::<_, A>(self::unwrap_ok::new(self))
89    }
90}
91
92impl<E, A, B> EndpointResultExt<A, B> for E
93where
94    E: Endpoint<Output = Result<A, B>>,
95{
96}