finchers_ext/result/
mod.rs1mod 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
20pub trait EndpointResultExt<A, B>: Endpoint<Output = Result<A, B>> + Sized {
22 #[inline(always)]
24 fn as_ok<T>(self) -> Self
25 where
26 Self::Output: IsResult<Ok = T>,
27 {
28 self
29 }
30
31 #[inline(always)]
33 fn as_err<E>(self) -> Self
34 where
35 Self::Output: IsResult<Err = E>,
36 {
37 self
38 }
39
40 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 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 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 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 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 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}