viz_core/handler/
or_else.rs

1use crate::{Error, Handler, Result};
2
3/// Calls `op` if the output is `Err`, otherwise returns the `Ok` value of the output.
4#[derive(Clone, Debug)]
5pub struct OrElse<H, F> {
6    h: H,
7    f: F,
8}
9
10impl<H, F> OrElse<H, F> {
11    /// Creates an [`OrElse`] 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> Handler<I> for OrElse<H, F>
20where
21    I: Send + 'static,
22    H: Handler<I, Output = Result<O>>,
23    F: Handler<Error, Output = H::Output>,
24    O: Send,
25{
26    type Output = F::Output;
27
28    async fn call(&self, i: I) -> Self::Output {
29        match self.h.call(i).await {
30            Ok(o) => Ok(o),
31            Err(e) => self.f.call(e).await,
32        }
33    }
34}