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