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
8 changes: 3 additions & 5 deletions crates/core/cgp-component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ mod namespaces;
mod providers;
mod traits;

pub use namespaces::{DefaultImpls1, DefaultImpls2, DefaultNamespace};
pub use providers::{
RedirectLookup, UseContext, UseDefault, UseDelegate, UseFields, WithContext, WithProvider,
};
pub use traits::{CanUseComponent, DelegateComponent, IsProviderFor};
pub use namespaces::*;
pub use providers::*;
pub use traits::*;
8 changes: 3 additions & 5 deletions crates/core/cgp-component/src/providers/use_delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use core::marker::PhantomData;
that overlaps on the generic parameters.

The implementation of `UseDelegate` can be automatically generated through
the `derive_delegate` entry in `#[cgp_component]`. It is also possible to
the `#[derive_delegate]` attribute in `#[cgp_component]`. It is also possible to
implement the dispatcher pattern on types other than `UseDelegate`, especially
when there are multiple generic parameters that could be dispatched differently.
We mainly use `UseDelegate` as the default dispatcher, so that users don't need
Expand All @@ -31,10 +31,8 @@ use core::marker::PhantomData;
Given the following component definition:

```rust,ignore
#[cgp_component {
provider: ErrorRaiser,
derive_delegate: UseDelegate<SourceError>,
}]
#[cgp_component(ErrorRaiser)]
#[derive_delegate(UseDelegate<SourceError>)]
pub trait CanRaiseError<SourceError>: HasErrorType {
fn raise_error(error: SourceError) -> Self::Error;
}
Expand Down
6 changes: 3 additions & 3 deletions crates/core/cgp-component/src/traits/delegate_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
```
*/
#[diagnostic::on_unimplemented(
message = "{Self} does not contain any DelegateComponent entry for {Name}",
note = "You might want to implement the provider trait for {Name} on {Self}"
message = "{Self} does not contain any DelegateComponent entry for {Key}",
note = "You might want to implement the provider trait for {Key} on {Self}"
)]
pub trait DelegateComponent<Name: ?Sized> {
pub trait DelegateComponent<Key: ?Sized> {
type Delegate;
}
6 changes: 2 additions & 4 deletions crates/core/cgp-error/src/traits/can_raise_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ use crate::traits::has_error_type::HasErrorType;
The `CanRaiseError` trait is used to raise any concrete error type into
an abstract error provided by [`HasErrorType`].
*/
#[cgp_component {
provider: ErrorRaiser,
derive_delegate: UseDelegate<SourceError>,
}]
#[cgp_component(ErrorRaiser)]
#[prefix(@cgp.core.error in DefaultNamespace)]
#[derive_delegate(UseDelegate<SourceError>)]
pub trait CanRaiseError<SourceError>: HasErrorType {
fn raise_error(error: SourceError) -> Self::Error;
}
6 changes: 2 additions & 4 deletions crates/core/cgp-error/src/traits/can_wrap_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ use cgp_macro::cgp_component;

use crate::traits::HasErrorType;

#[cgp_component {
provider: ErrorWrapper,
derive_delegate: UseDelegate<Detail>,
}]
#[cgp_component(ErrorWrapper)]
#[prefix(@cgp.core.error in DefaultNamespace)]
#[derive_delegate(UseDelegate<Detail>)]
pub trait CanWrapError<Detail>: HasErrorType {
fn wrap_error(error: Self::Error, detail: Detail) -> Self::Error;
}
6 changes: 2 additions & 4 deletions crates/core/cgp-type/src/traits/has_type.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use cgp::macro_prelude::*;
use cgp_macro::cgp_component;

#[cgp_component {
provider: TypeProvider,
derive_delegate: UseDelegate<Tag>,
}]
#[cgp_component(TypeProvider)]
#[derive_delegate(UseDelegate<Tag>)]
pub trait HasType<Tag> {
type Type;
}
Expand Down
22 changes: 8 additions & 14 deletions crates/extra/cgp-handler/src/components/async_computer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,21 @@ use cgp::prelude::*;
use crate::UseInputDelegate;

#[async_trait]
#[cgp_component {
provider: AsyncComputer,
derive_delegate: [
UseDelegate<Code>,
UseInputDelegate<Input>,
],
}]
#[cgp_component(AsyncComputer)]
#[prefix(@cgp.extra.handler in DefaultNamespace)]
#[derive_delegate(UseDelegate<Code>)]
#[derive_delegate(UseInputDelegate<Input>)]
pub trait CanComputeAsync<Code, Input> {
type Output;

async fn compute_async(&self, _code: PhantomData<Code>, input: Input) -> Self::Output;
}

#[async_trait]
#[cgp_component {
provider: AsyncComputerRef,
derive_delegate: [
UseDelegate<Code>,
UseInputDelegate<Input>,
],
}]
#[cgp_component(AsyncComputerRef)]
#[prefix(@cgp.extra.handler in DefaultNamespace)]
#[derive_delegate(UseDelegate<Code>)]
#[derive_delegate(UseInputDelegate<Input>)]
pub trait CanComputeAsyncRef<Code, Input> {
type Output;

Expand Down
22 changes: 8 additions & 14 deletions crates/extra/cgp-handler/src/components/computer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,20 @@ use cgp::prelude::*;

use crate::UseInputDelegate;

#[cgp_component {
provider: Computer,
derive_delegate: [
UseDelegate<Code>,
UseInputDelegate<Input>,
],
}]
#[cgp_component(Computer)]
#[prefix(@cgp.extra.handler in DefaultNamespace)]
#[derive_delegate(UseDelegate<Code>)]
#[derive_delegate(UseInputDelegate<Input>)]
pub trait CanCompute<Code, Input> {
type Output;

fn compute(&self, _code: PhantomData<Code>, input: Input) -> Self::Output;
}

#[cgp_component {
provider: ComputerRef,
derive_delegate: [
UseDelegate<Code>,
UseInputDelegate<Input>,
],
}]
#[cgp_component(ComputerRef)]
#[prefix(@cgp.extra.handler in DefaultNamespace)]
#[derive_delegate(UseDelegate<Code>)]
#[derive_delegate(UseInputDelegate<Input>)]
pub trait CanComputeRef<Code, Input> {
type Output;

Expand Down
22 changes: 8 additions & 14 deletions crates/extra/cgp-handler/src/components/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ use cgp::prelude::*;
use crate::UseInputDelegate;

#[async_trait]
#[cgp_component {
provider: Handler,
derive_delegate: [
UseDelegate<Code>,
UseInputDelegate<Input>,
],
}]
#[cgp_component(Handler)]
#[prefix(@cgp.extra.handler in DefaultNamespace)]
#[derive_delegate(UseDelegate<Code>)]
#[derive_delegate(UseInputDelegate<Input>)]
pub trait CanHandle<Code, Input>: HasErrorType {
type Output;

Expand All @@ -24,13 +21,10 @@ pub trait CanHandle<Code, Input>: HasErrorType {
}

#[async_trait]
#[cgp_component {
provider: HandlerRef,
derive_delegate: [
UseDelegate<Code>,
UseInputDelegate<Input>,
],
}]
#[cgp_component(HandlerRef)]
#[prefix(@cgp.extra.handler in DefaultNamespace)]
#[derive_delegate(UseDelegate<Code>)]
#[derive_delegate(UseInputDelegate<Input>)]
pub trait CanHandleRef<Code, Input>: HasErrorType {
type Output;

Expand Down
7 changes: 3 additions & 4 deletions crates/extra/cgp-handler/src/components/produce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use core::marker::PhantomData;
use cgp::component::UseDelegate;
use cgp::prelude::*;

#[cgp_component {
provider: Producer,
derive_delegate: UseDelegate<Code>,
}]
#[cgp_component(Producer)]
#[prefix(@cgp.extra.handler in DefaultNamespace)]
#[derive_delegate(UseDelegate<Code>)]
pub trait CanProduce<Code> {
type Output;

Expand Down
22 changes: 8 additions & 14 deletions crates/extra/cgp-handler/src/components/try_compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ use cgp::prelude::*;

use crate::UseInputDelegate;

#[cgp_component {
provider: TryComputer,
derive_delegate: [
UseDelegate<Code>,
UseInputDelegate<Input>,
],
}]
#[cgp_component(TryComputer)]
#[prefix(@cgp.extra.handler in DefaultNamespace)]
#[derive_delegate(UseDelegate<Code>)]
#[derive_delegate(UseInputDelegate<Input>)]
pub trait CanTryCompute<Code, Input>: HasErrorType {
type Output;

Expand All @@ -22,13 +19,10 @@ pub trait CanTryCompute<Code, Input>: HasErrorType {
) -> Result<Self::Output, Self::Error>;
}

#[cgp_component {
provider: TryComputerRef,
derive_delegate: [
UseDelegate<Code>,
UseInputDelegate<Input>,
],
}]
#[cgp_component(TryComputerRef)]
#[prefix(@cgp.extra.handler in DefaultNamespace)]
#[derive_delegate(UseDelegate<Code>)]
#[derive_delegate(UseInputDelegate<Input>)]
pub trait CanTryComputeRef<Code, Input>: HasErrorType {
type Output;

Expand Down
12 changes: 4 additions & 8 deletions crates/extra/cgp-run/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@ use core::marker::PhantomData;

use cgp::prelude::*;

#[cgp_component {
provider: Runner,
derive_delegate: UseDelegate<Code>,
}]
#[cgp_component(Runner)]
#[async_trait]
#[derive_delegate(UseDelegate<Code>)]
pub trait CanRun<Code>: HasErrorType {
async fn run(&self, _code: PhantomData<Code>) -> Result<(), Self::Error>;
}

#[cgp_component {
provider: SendRunner,
derive_delegate: UseDelegate<Code>,
}]
#[cgp_component(SendRunner)]
#[async_trait]
#[derive_delegate(UseDelegate<Code>)]
pub trait CanSendRun<Code>: HasErrorType {
fn send_run(
&self,
Expand Down
2 changes: 2 additions & 0 deletions crates/macros/cgp-macro-core/src/exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export_constructs! {
RedirectLookup,
DelegateComponent,
IsProviderFor,
CanUseComponent,
HasField,
HasFieldMut,
UseContext,
Life,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use syn::punctuated::Punctuated;
use syn::token::Comma;
use syn::{GenericParam, Generics, Type};

use crate::exports::Life;
use crate::parse_internal;
use crate::types::generics::TypeGenerics;

Expand All @@ -18,7 +19,7 @@ pub fn parse_is_provider_params(generics: &Generics) -> syn::Result<Punctuated<T
}
GenericParam::Lifetime(life_param) => {
let life = &life_param.lifetime;
parse_internal! { Life<#life> }
parse_internal! { #Life<#life> }
}
GenericParam::Const(_) => {
unimplemented!("const generic parameters are not yet supported in CGP traits")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ use syn::punctuated::Punctuated;
use syn::token::Comma;
use syn::{Attribute, ItemTrait, TypeParamBound};

use crate::types::attributes::{PrefixAttribute, UseTypeAttribute, UseTypeAttributes};
use crate::types::attributes::{
DeriveDelegateAttribute, DeriveDelegateAttributes, PrefixAttribute, UseTypeAttribute,
UseTypeAttributes,
};

#[derive(Default, Clone)]
pub struct CgpComponentAttributes {
pub extend: Vec<TypeParamBound>,
pub use_type: UseTypeAttributes,
pub prefixes: Vec<PrefixAttribute>,
pub derive_delegate_attributes: DeriveDelegateAttributes,
}

impl CgpComponentAttributes {
Expand Down Expand Up @@ -57,6 +61,13 @@ impl CgpComponentAttributes {
} else if ident == "prefix" {
let namespace_specs = attribute.parse_args_with(PrefixAttribute::parse)?;
parsed_attributes.prefixes.push(namespace_specs);
} else if ident == "derive_delegate" {
let derive_delegate_attribute =
attribute.parse_args_with(DeriveDelegateAttribute::parse)?;
parsed_attributes
.derive_delegate_attributes
.attributes
.push(derive_delegate_attribute);
} else {
attributes.push(attribute);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
use syn::bracketed;
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::token::{Bracket, Comma};

use crate::types::attributes::DeriveDelegateAttribute;

#[derive(Default, Clone)]
pub struct DeriveDelegateAttributes {
pub attributes: Vec<DeriveDelegateAttribute>,
}

impl Parse for DeriveDelegateAttributes {
fn parse(input: ParseStream) -> syn::Result<Self> {
if input.peek(Bracket) {
let body;
bracketed!(body in input);

let attributes = <Punctuated<DeriveDelegateAttribute, Comma>>::parse_terminated(&body)?;
Ok(Self {
attributes: Vec::from_iter(attributes),
})
} else {
let spec = input.parse()?;
Ok(Self {
attributes: vec![spec],
})
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use proc_macro2::Span;
use syn::parse::Parse;
use syn::{Error, Ident};

use crate::types::attributes::DeriveDelegateAttributes;
use crate::types::cgp_component::CgpComponentRawArgs;
use crate::types::ident::IdentWithTypeGenerics;

Expand All @@ -11,7 +10,6 @@ pub struct CgpComponentArgs {
pub context_ident: Ident,
pub provider_ident: Ident,
pub component_name: IdentWithTypeGenerics,
pub derive_delegate_attributes: DeriveDelegateAttributes,
}

impl Parse for CgpComponentArgs {
Expand Down Expand Up @@ -41,13 +39,10 @@ impl TryFrom<CgpComponentRawArgs> for CgpComponentArgs {
))
});

let derive_delegate_attributes = raw_args.derive_delegate_attributes.unwrap_or_default();

Ok(Self {
context_ident,
provider_ident,
component_name,
derive_delegate_attributes,
})
}
}
Loading
Loading