Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 89 additions & 2 deletions crates/cgp-dispatch/src/providers/dispatch_fields.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use cgp_core::prelude::*;
use cgp_handler::{Computer, ComputerComponent, Handler, HandlerComponent};
use cgp_handler::{
Computer, ComputerComponent, HandleFieldValue, Handler, HandlerComponent, TryComputer,
TryComputerComponent,
};

use crate::{DispatchHandlers, ExtractFieldAndHandle};
use crate::{DispatchHandlers, DispatchHandlersRef, ExtractFieldAndHandle};

pub struct DispatchFields<Provider = UseContext>(pub PhantomData<Provider>);

pub struct DispatchFieldsRef<Provider = UseContext>(pub PhantomData<Provider>);

pub type DispatchFieldValues = DispatchFields<HandleFieldValue<UseContext>>;

pub type DispatchFieldValueRefs = DispatchFieldsRef<HandleFieldValue<UseContext>>;

#[cgp_provider]
impl<Context, Code, Input, Output, Fields, Provider> Computer<Context, Code, Input>
for DispatchFields<Provider>
Expand All @@ -20,6 +29,26 @@ where
}
}

#[cgp_provider]
impl<Context, Code, Input, Output, Fields, Provider> TryComputer<Context, Code, Input>
for DispatchFields<Provider>
where
Context: HasErrorType,
Input: HasFields<Fields = Fields>,
Fields: FieldsToExtractFieldHandlers<Provider>,
DispatchHandlers<Fields::Handlers>: TryComputer<Context, Code, Input, Output = Output>,
{
type Output = Output;

fn try_compute(
context: &Context,
code: PhantomData<Code>,
input: Input,
) -> Result<Output, Context::Error> {
DispatchHandlers::try_compute(context, code, input)
}
}

#[cgp_provider]
impl<Context, Code: Send, Input: Send, Output: Send, Fields, Provider> Handler<Context, Code, Input>
for DispatchFields<Provider>
Expand All @@ -40,6 +69,64 @@ where
}
}

#[cgp_provider]
impl<Context, Code, Input, Output, Provider> Computer<Context, Code, &Input>
for DispatchFieldsRef<Provider>
where
Input: HasFieldsRef,
for<'b> Input::FieldsRef<'b>: FieldsToExtractFieldHandlers<Provider>,
for<'b> DispatchHandlersRef<<Input::FieldsRef<'b> as FieldsToExtractFieldHandlers<Provider>>::Handlers>:
Computer<Context, Code, &'b Input, Output = Output>,
{
type Output = Output;

fn compute(context: &Context, code: PhantomData<Code>, input: &Input) -> Output {
DispatchHandlersRef::compute(context, code, input)
}
}

#[cgp_provider]
impl<Context, Code, Input, Output, Provider> TryComputer<Context, Code, &Input>
for DispatchFieldsRef<Provider>
where
Context: HasErrorType,
Input: HasFieldsRef,
for<'b> Input::FieldsRef<'b>: FieldsToExtractFieldHandlers<Provider>,
for<'b> DispatchHandlersRef<<Input::FieldsRef<'b> as FieldsToExtractFieldHandlers<Provider>>::Handlers>:
TryComputer<Context, Code, &'b Input, Output = Output>,
{
type Output = Output;

fn try_compute(
context: &Context,
code: PhantomData<Code>,
input: &Input,
) -> Result<Output, Context::Error> {
DispatchHandlersRef::try_compute(context, code, input)
}
}

#[cgp_provider]
impl<Context, Code: Send, Input, Output: Send, Provider> Handler<Context, Code, &Input>
for DispatchFieldsRef<Provider>
where
Context: HasAsyncErrorType,
Input: Send + Sync + HasFieldsRef,
for<'b> Input::FieldsRef<'b>: FieldsToExtractFieldHandlers<Provider>,
for<'b> DispatchHandlersRef<<Input::FieldsRef<'b> as FieldsToExtractFieldHandlers<Provider>>::Handlers>:
Handler<Context, Code, &'b Input, Output = Output>,
{
type Output = Output;

async fn handle(
context: &Context,
code: PhantomData<Code>,
input: &Input,
) -> Result<Output, Context::Error> {
DispatchHandlersRef::handle(context, code, input).await
}
}

trait FieldsToExtractFieldHandlers<Provider> {
type Handlers;
}
Expand Down
85 changes: 85 additions & 0 deletions crates/cgp-dispatch/src/providers/dispatch_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use cgp_handler::{
};

pub struct DispatchHandlers<Handlers>(pub PhantomData<Handlers>);
pub struct DispatchHandlersRef<Handlers>(pub PhantomData<Handlers>);

#[cgp_provider]
impl<Context, Code, Input, Output, Handlers> Computer<Context, Code, Input>
Expand Down Expand Up @@ -77,6 +78,90 @@ where
}
}

#[cgp_provider]
impl<Context, Code, Input, Output, Handlers> TryComputer<Context, Code, &Input>
for DispatchHandlersRef<Handlers>
where
Context: HasErrorType,
Input: HasExtractorRef,
Handlers: for<'b> TryDispatchComputer<
Context,
Code,
Input::ExtractorRef<'b>,
Output = Output,
Remainder: FinalizeExtract,
>,
{
type Output = Output;

fn try_compute(
_context: &Context,
code: PhantomData<Code>,
input: &Input,
) -> Result<Output, Context::Error> {
let res = Handlers::try_compute(_context, code, input.extractor_ref())?;

match res {
Ok(output) => Ok(output),
Err(remainder) => remainder.finalize_extract(),
}
}
}
#[cgp_provider]
impl<Context, Code, Input, Output, Handlers> Computer<Context, Code, &Input>
for DispatchHandlersRef<Handlers>
where
Input: HasExtractorRef,
Handlers: for<'b> DispatchComputer<
Context,
Code,
Input::ExtractorRef<'b>,
Output = Output,
Remainder: FinalizeExtract,
>,
{
type Output = Output;

fn compute(_context: &Context, code: PhantomData<Code>, input: &Input) -> Output {
let res = Handlers::compute(_context, code, input.extractor_ref());

match res {
Ok(output) => output,
Err(remainder) => remainder.finalize_extract(),
}
}
}

#[cgp_provider]
impl<Context, Code: Send, Input, Output: Send, Handlers> Handler<Context, Code, &Input>
for DispatchHandlersRef<Handlers>
where
Context: HasAsyncErrorType,
Input: HasExtractorRef + Send + Sync,
Handlers: for<'b> DispatchHandler<
Context,
Code,
Input::ExtractorRef<'b>,
Output = Output,
Remainder: FinalizeExtract,
>,
{
type Output = Output;

async fn handle(
_context: &Context,
code: PhantomData<Code>,
input: &Input,
) -> Result<Output, Context::Error> {
let res = Handlers::handle(_context, code, input.extractor_ref()).await?;

match res {
Ok(output) => Ok(output),
Err(remainder) => Err(remainder.finalize_extract()),
}
}
}

trait DispatchComputer<Context, Code, Input> {
type Output;

Expand Down
2 changes: 1 addition & 1 deletion crates/cgp-handler/src/providers/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
Computer, ComputerComponent, Handler, HandlerComponent, TryComputer, TryComputerComponent,
};

pub struct HandleFieldValue<Provider>(pub PhantomData<Provider>);
pub struct HandleFieldValue<Provider = UseContext>(pub PhantomData<Provider>);

#[cgp_provider]
impl<Context, Code, Tag, Input, Output, Provider> Computer<Context, Code, Field<Tag, Input>>
Expand Down
Loading