Skip to main content

cgp_handler/providers/
try_promote.rs

1use cgp_core::prelude::*;
2
3use crate::{
4    AsyncComputer, AsyncComputerComponent, Computer, ComputerComponent, Handler, HandlerComponent,
5    TryComputer, TryComputerComponent,
6};
7
8pub struct TryPromote<Provider>(pub PhantomData<Provider>);
9
10#[cgp_provider]
11impl<Context, Code, Input, Output, Provider> TryComputer<Context, Code, Input>
12    for TryPromote<Provider>
13where
14    Context: HasErrorType,
15    Provider: Computer<Context, Code, Input, Output = Result<Output, Context::Error>>,
16{
17    type Output = Output;
18
19    fn try_compute(
20        context: &Context,
21        tag: PhantomData<Code>,
22        input: Input,
23    ) -> Result<Output, Context::Error> {
24        Provider::compute(context, tag, input)
25    }
26}
27
28#[cgp_provider]
29impl<Context, Code, Input, Provider, Output> Computer<Context, Code, Input> for TryPromote<Provider>
30where
31    Context: HasErrorType,
32    Provider: TryComputer<Context, Code, Input, Output = Output>,
33{
34    type Output = Result<Output, Context::Error>;
35
36    fn compute(
37        context: &Context,
38        tag: PhantomData<Code>,
39        input: Input,
40    ) -> Result<Output, Context::Error> {
41        Provider::try_compute(context, tag, input)
42    }
43}
44
45#[cgp_provider]
46impl<Context, Code, Input, Output, Provider> Handler<Context, Code, Input> for TryPromote<Provider>
47where
48    Context: HasErrorType,
49    Provider: AsyncComputer<Context, Code, Input, Output = Result<Output, Context::Error>>,
50{
51    type Output = Output;
52
53    async fn handle(
54        context: &Context,
55        tag: PhantomData<Code>,
56        input: Input,
57    ) -> Result<Output, Context::Error> {
58        Provider::compute_async(context, tag, input).await
59    }
60}
61
62#[cgp_provider]
63impl<Context, Code, Input, Provider, Output> AsyncComputer<Context, Code, Input>
64    for TryPromote<Provider>
65where
66    Context: HasErrorType,
67    Provider: Handler<Context, Code, Input, Output = Output>,
68{
69    type Output = Result<Output, Context::Error>;
70
71    async fn compute_async(
72        context: &Context,
73        tag: PhantomData<Code>,
74        input: Input,
75    ) -> Result<Output, Context::Error> {
76        Provider::handle(context, tag, input).await
77    }
78}