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
use {
    crate::{
        action::{
            ActionContext, //
            EndpointAction,
            Preflight,
            PreflightContext,
        },
        endpoint::{Endpoint, IsEndpoint},
        error::Error,
    },
    futures::Poll,
};

#[allow(missing_docs)]
#[derive(Debug, Copy, Clone)]
pub struct MapErr<E, F> {
    pub(super) endpoint: E,
    pub(super) f: F,
}

impl<E: IsEndpoint, F> IsEndpoint for MapErr<E, F> {}

impl<E, F, Bd, R> Endpoint<Bd> for MapErr<E, F>
where
    E: Endpoint<Bd>,
    F: Fn(Error) -> R + Clone,
    R: Into<Error>,
{
    type Output = E::Output;
    type Action = MapErrAction<E::Action, F>;

    fn action(&self) -> Self::Action {
        MapErrAction {
            action: self.endpoint.action(),
            f: self.f.clone(),
        }
    }
}

#[allow(missing_debug_implementations)]
pub struct MapErrAction<Act, F> {
    action: Act,
    f: F,
}

impl<Act, F, Bd, R> EndpointAction<Bd> for MapErrAction<Act, F>
where
    Act: EndpointAction<Bd>,
    F: Fn(Error) -> R,
    R: Into<Error>,
{
    type Output = Act::Output;

    fn preflight(
        &mut self,
        cx: &mut PreflightContext<'_>,
    ) -> Result<Preflight<Self::Output>, Error> {
        self.action
            .preflight(cx)
            .map_err(|err| (self.f)(err).into())
    }

    fn poll_action(&mut self, cx: &mut ActionContext<'_, Bd>) -> Poll<Self::Output, Error> {
        self.action
            .poll_action(cx)
            .map_err(|err| (self.f)(err).into())
    }
}