Skip to main content

cgp_dispatch/providers/field_matchers/
extract_first_field.rs

1use cgp_core::prelude::*;
2use cgp_handler::{AsyncComputer, AsyncComputerComponent, Computer, ComputerComponent};
3
4pub struct ExtractFirstFieldAndHandle<Tag, Provider = UseContext>(pub PhantomData<(Tag, Provider)>);
5
6#[cgp_provider]
7impl<Context, Code, Input, Args, Tag, Value, Provider, Output, Remainder>
8    Computer<Context, Code, (Input, Args)> for ExtractFirstFieldAndHandle<Tag, Provider>
9where
10    Input: ExtractField<Tag, Value = Value, Remainder = Remainder>,
11    Provider: Computer<Context, Code, (Field<Tag, Value>, Args), Output = Output>,
12{
13    type Output = Result<Output, (Remainder, Args)>;
14
15    fn compute(
16        context: &Context,
17        tag: PhantomData<Code>,
18        (input, args): (Input, Args),
19    ) -> Result<Output, (Remainder, Args)> {
20        let res = input.extract_field(PhantomData::<Tag>);
21        match res {
22            Ok(value) => {
23                let output = Provider::compute(context, tag, (value.into(), args));
24                Ok(output)
25            }
26            Err(remainder) => Err((remainder, args)),
27        }
28    }
29}
30
31#[cgp_provider]
32impl<Context, Code, Input, Args, Tag, Value, Provider, Output, Remainder>
33    AsyncComputer<Context, Code, (Input, Args)> for ExtractFirstFieldAndHandle<Tag, Provider>
34where
35    Input: ExtractField<Tag, Value = Value, Remainder = Remainder>,
36    Provider: AsyncComputer<Context, Code, (Field<Tag, Value>, Args), Output = Output>,
37{
38    type Output = Result<Output, (Remainder, Args)>;
39
40    async fn compute_async(
41        context: &Context,
42        tag: PhantomData<Code>,
43        (input, args): (Input, Args),
44    ) -> Result<Output, (Remainder, Args)> {
45        let value = input.extract_field(PhantomData::<Tag>);
46
47        match value {
48            Ok(value) => {
49                let output = Provider::compute_async(context, tag, (value.into(), args)).await;
50                Ok(output)
51            }
52            Err(remainder) => Err((remainder, args)),
53        }
54    }
55}