Skip to main content

cgp_dispatch/providers/with_handlers/
match_first_with_handlers_ref.rs

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