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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
mod and_then;
mod err_into;
mod map_err;
mod map_ok;
mod or_else;
mod unwrap_ok;
pub use self::and_then::AndThen;
pub use self::err_into::ErrInto;
pub use self::map_err::MapErr;
pub use self::map_ok::MapOk;
pub use self::or_else::OrElse;
pub use self::unwrap_ok::UnwrapOk;
use finchers_core::endpoint::assert_output;
use finchers_core::{Endpoint, Error, IsResult};
pub trait EndpointResultExt<A, B>: Endpoint<Output = Result<A, B>> + Sized {
#[inline(always)]
fn as_ok<T>(self) -> Self
where
Self::Output: IsResult<Ok = T>,
{
self
}
#[inline(always)]
fn as_err<E>(self) -> Self
where
Self::Output: IsResult<Err = E>,
{
self
}
fn map_ok<F, U>(self, f: F) -> MapOk<Self, F>
where
F: FnOnce(A) -> U + Clone + Send + Sync,
{
assert_output::<_, Result<U, B>>(self::map_ok::new(self, f))
}
fn map_err<F, U>(self, f: F) -> MapErr<Self, F>
where
F: FnOnce(B) -> U + Clone + Send + Sync,
{
assert_output::<_, Result<A, U>>(self::map_err::new(self, f))
}
fn err_into<U>(self) -> ErrInto<Self, U>
where
B: Into<U>,
{
assert_output::<_, Result<A, U>>(self::err_into::new(self))
}
fn and_then<F, U>(self, f: F) -> AndThen<Self, F>
where
F: FnOnce(A) -> Result<U, B> + Clone + Send + Sync,
{
assert_output::<_, Result<U, B>>(self::and_then::new(self, f))
}
fn or_else<F, U>(self, f: F) -> OrElse<Self, F>
where
F: FnOnce(B) -> Result<A, U> + Clone + Send + Sync,
{
assert_output::<_, Result<A, U>>(self::or_else::new(self, f))
}
fn unwrap_ok(self) -> UnwrapOk<Self>
where
B: Into<Error>,
{
assert_output::<_, A>(self::unwrap_ok::new(self))
}
}
impl<E, A, B> EndpointResultExt<A, B> for E
where
E: Endpoint<Output = Result<A, B>>,
{
}