warp/filter/
or.rs

1use std::future::Future;
2use std::pin::Pin;
3use std::task::{Context, Poll};
4
5use futures_util::{ready, TryFuture};
6use pin_project::pin_project;
7
8use super::{Filter, FilterBase, Internal};
9use crate::generic::Either;
10use crate::reject::CombineRejection;
11use crate::route;
12
13type Combined<E1, E2> = <E1 as CombineRejection<E2>>::Combined;
14
15#[derive(Clone, Copy, Debug)]
16pub struct Or<T, U> {
17    pub(super) first: T,
18    pub(super) second: U,
19}
20
21impl<T, U> FilterBase for Or<T, U>
22where
23    T: Filter,
24    U: Filter + Clone + Send,
25    U::Error: CombineRejection<T::Error>,
26{
27    type Extract = (Either<T::Extract, U::Extract>,);
28    //type Error = <U::Error as CombineRejection<T::Error>>::Combined;
29    type Error = Combined<U::Error, T::Error>;
30    type Future = EitherFuture<T, U>;
31
32    fn filter(&self, _: Internal) -> Self::Future {
33        let idx = route::with(|route| route.matched_path_index());
34        EitherFuture {
35            state: State::First(self.first.filter(Internal), self.second.clone()),
36            original_path_index: PathIndex(idx),
37        }
38    }
39}
40
41#[allow(missing_debug_implementations)]
42#[pin_project]
43pub struct EitherFuture<T: Filter, U: Filter> {
44    #[pin]
45    state: State<T, U>,
46    original_path_index: PathIndex,
47}
48
49#[pin_project(project = StateProj)]
50enum State<T: Filter, U: Filter> {
51    First(#[pin] T::Future, U),
52    Second(Option<T::Error>, #[pin] U::Future),
53    Done,
54}
55
56#[derive(Copy, Clone)]
57struct PathIndex(usize);
58
59impl PathIndex {
60    fn reset_path(&self) {
61        route::with(|route| route.reset_matched_path_index(self.0));
62    }
63}
64
65impl<T, U> Future for EitherFuture<T, U>
66where
67    T: Filter,
68    U: Filter,
69    U::Error: CombineRejection<T::Error>,
70{
71    type Output = Result<(Either<T::Extract, U::Extract>,), Combined<U::Error, T::Error>>;
72
73    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
74        loop {
75            let pin = self.as_mut().project();
76            let (err1, fut2) = match pin.state.project() {
77                StateProj::First(first, second) => match ready!(first.try_poll(cx)) {
78                    Ok(ex1) => {
79                        return Poll::Ready(Ok((Either::A(ex1),)));
80                    }
81                    Err(e) => {
82                        pin.original_path_index.reset_path();
83                        (e, second.filter(Internal))
84                    }
85                },
86                StateProj::Second(err1, second) => {
87                    let ex2 = match ready!(second.try_poll(cx)) {
88                        Ok(ex2) => Ok((Either::B(ex2),)),
89                        Err(e) => {
90                            pin.original_path_index.reset_path();
91                            let err1 = err1.take().expect("polled after complete");
92                            Err(e.combine(err1))
93                        }
94                    };
95                    self.set(EitherFuture {
96                        state: State::Done,
97                        ..*self
98                    });
99                    return Poll::Ready(ex2);
100                }
101                StateProj::Done => panic!("polled after complete"),
102            };
103
104            self.set(EitherFuture {
105                state: State::Second(Some(err1), fut2),
106                ..*self
107            });
108        }
109    }
110}