viz_core/handler/
map_err.rs

1use crate::{Error, Handler, Result};
2
3/// Maps the `Err` value of the output if after the handler called.
4#[derive(Clone, Debug)]
5pub struct MapErr<H, F> {
6    h: H,
7    f: F,
8}
9
10impl<H, F> MapErr<H, F> {
11    /// Creates a [`MapErr`] handler.
12    #[inline]
13    pub const fn new(h: H, f: F) -> Self {
14        Self { h, f }
15    }
16}
17
18#[crate::async_trait]
19impl<H, F, I, O, E> Handler<I> for MapErr<H, F>
20where
21    I: Send + 'static,
22    H: Handler<I, Output = Result<O, E>>,
23    F: FnOnce(E) -> Error + Send + Sync + Copy + 'static,
24{
25    type Output = Result<O>;
26
27    async fn call(&self, i: I) -> Self::Output {
28        self.h.call(i).await.map_err(self.f)
29    }
30}