Skip to main content

cgp_dispatch/providers/with_handlers/
match_first_with_handlers.rs

1use core::marker::PhantomData;
2
3use cgp_core::prelude::*;
4use cgp_handler::{AsyncComputer, AsyncComputerComponent, Computer, ComputerComponent};
5
6use crate::DispatchMatchers;
7
8pub struct MatchFirstWithHandlers<Handlers>(pub PhantomData<Handlers>);
9
10#[cgp_provider]
11impl<Context, Code, Input, Args, Output, Remainder, Handlers> Computer<Context, Code, (Input, Args)>
12    for MatchFirstWithHandlers<Handlers>
13where
14    Input: HasExtractor,
15    DispatchMatchers<Handlers>: Computer<
16            Context,
17            Code,
18            (Input::Extractor, Args),
19            Output = Result<Output, (Remainder, Args)>,
20        >,
21    Remainder: FinalizeExtract,
22{
23    type Output = Output;
24
25    fn compute(context: &Context, code: PhantomData<Code>, (input, args): (Input, Args)) -> Output {
26        let res = DispatchMatchers::compute(context, code, (input.to_extractor(), args));
27        match res {
28            Ok(output) => output,
29            Err((remainder, _)) => remainder.finalize_extract(),
30        }
31    }
32}
33
34#[cgp_provider]
35impl<Context, Code, Input, Args, Output, Remainder, Handlers>
36    AsyncComputer<Context, Code, (Input, Args)> for MatchFirstWithHandlers<Handlers>
37where
38    Input: HasExtractor,
39    DispatchMatchers<Handlers>: AsyncComputer<
40            Context,
41            Code,
42            (Input::Extractor, Args),
43            Output = Result<Output, (Remainder, Args)>,
44        >,
45    Remainder: FinalizeExtract,
46{
47    type Output = Output;
48
49    async fn compute_async(
50        context: &Context,
51        code: PhantomData<Code>,
52        (input, args): (Input, Args),
53    ) -> Output {
54        let res =
55            DispatchMatchers::compute_async(context, code, (input.to_extractor(), args)).await;
56
57        match res {
58            Ok(output) => output,
59            Err((remainder, _)) => remainder.finalize_extract(),
60        }
61    }
62}