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
2 changes: 2 additions & 0 deletions crates/cgp-error/src/traits/can_raise_error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cgp_component::*;
use cgp_field::types::*;
use cgp_macro::cgp_component;

use crate::traits::has_error_type::HasErrorType;
Expand All @@ -11,6 +12,7 @@ use crate::traits::has_error_type::HasErrorType;
provider: ErrorRaiser,
derive_delegate: UseDelegate<SourceError>,
}]
#[use_namespace(cgp.core.error)]
pub trait CanRaiseError<SourceError>: HasErrorType {
fn raise_error(error: SourceError) -> Self::Error;
}
9 changes: 1 addition & 8 deletions crates/cgp-field/src/types/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,4 @@ where
}
}

impl<Tag, Value> Eq for Field<Tag, Value>
where
Value: Eq,
{
fn assert_receiver_is_total_eq(&self) {
self.value.assert_receiver_is_total_eq()
}
}
impl<Tag, Value> Eq for Field<Tag, Value> where Value: Eq {}
64 changes: 55 additions & 9 deletions crates/cgp-macro-lib/src/parse/delegate_components.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use core::iter;

use proc_macro2::TokenStream;
use proc_macro2::{Span, TokenStream};
use quote::{ToTokens, TokenStreamExt, quote};
use syn::parse::discouraged::Speculative;
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::token::{At, Bracket, Colon, Comma, Gt, Lt, Pound, RArrow};
use syn::{Attribute, Error, Generics, Ident, Token, Type, braced, bracketed, parse_quote};
use syn::token::{At, Bracket, Colon, Comma, Gt, Lt, Pound, RArrow, Semi};
use syn::{Attribute, Error, Generics, Ident, Token, Type, braced, bracketed, parse_quote, parse2};

use crate::parse::{ComponentPaths, ImplGenerics, SimpleType, TypeGenerics};

Expand Down Expand Up @@ -99,22 +99,68 @@ impl Parse for DelegateComponents {

let target_type: Type = input.parse()?;

let delegate_entries = {
let content;
braced!(content in input);
Punctuated::parse_terminated(&content)?
};
let content;
braced!(content in input);

let meta_entries = parse_meta_delegate_entries(&content, &target_type)?;

let delegate_entries: Punctuated<DelegateEntry<Type>, Comma> =
Punctuated::parse_terminated(&content)?;

let entries = meta_entries.into_iter().chain(delegate_entries).collect();

Ok(Self {
attributes,
new_struct,
target_type,
target_generics,
entries: delegate_entries,
entries,
})
}
}

pub fn parse_meta_delegate_entries(
input: ParseStream,
target_type: &Type,
) -> syn::Result<Vec<DelegateEntry<Type>>> {
let mut entries = Vec::new();

while input.peek(Ident) {
let fork = input.fork();
let keyword: Ident = fork.parse()?;

if keyword == "open" {
input.advance_to(&fork);

let components: Punctuated<Type, Comma> = Punctuated::parse_separated_nonempty(input)?;
let _: Semi = input.parse()?;

for component in components {
let value = DelegateValue::Type(parse2(
quote!(RedirectLookup<#target_type, PathCons<#component, PathNil>>),
)?);

let key = DelegateKey {
ty: component,
generics: Default::default(),
};

let entry = DelegateEntry {
keys: Punctuated::from_iter([key]),
mode: DelegateMode::Provider(Colon(Span::call_site())),
value,
};

entries.push(entry)
}
} else {
break;
}
}

Ok(entries)
}

impl Parse for DelegateEntry<Type> {
fn parse(input: ParseStream) -> syn::Result<Self> {
let components = if input.peek(Bracket) {
Expand Down
10 changes: 9 additions & 1 deletion crates/cgp-tests/src/namespaces/extend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ where
type Provider = Provider;
}

impl<Components> ExtendedNamespace<Components> for ErrorRaiserComponent {
impl<Components, Error> ExtendedNamespace<Components>
for PathCons<
Symbol!("cgp"),
PathCons<
Symbol!("core"),
PathCons<Symbol!("error"), PathCons<ErrorRaiserComponent, PathCons<Error, PathNil>>>,
>,
>
{
type Provider = RedirectLookup<
Components,
PathCons<Symbol!("app"), PathCons<ErrorRaiserComponent, PathNil>>,
Expand Down
3 changes: 2 additions & 1 deletion crates/cgp-tests/tests/namespace_tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod experiments;
pub mod extend_namespace;
pub mod open;
pub mod redirect;
pub mod use_namespace;
23 changes: 23 additions & 0 deletions crates/cgp-tests/tests/namespace_tests/open.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use cgp::core::error::{ErrorRaiserComponent, ErrorTypeProviderComponent};
use cgp::extra::error::ReturnError;
use cgp::prelude::*;

pub struct App;

delegate_components! {
App {
open ErrorRaiserComponent;

ErrorTypeProviderComponent:
UseType<String>,
@ErrorRaiserComponent.String:
ReturnError,
}
}

check_components! {
App {
ErrorRaiserComponent:
String,
}
}
11 changes: 10 additions & 1 deletion crates/cgp-tests/tests/namespace_tests/use_namespace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cgp::core::error::ErrorTypeProviderComponent;
use cgp::core::error::{ErrorRaiserComponent, ErrorTypeProviderComponent};
use cgp::extra::error::ReturnError;
use cgp::prelude::*;

pub struct MyComponents;
Expand All @@ -16,5 +17,13 @@ delegate_components! {
App {
@cgp.core.error.ErrorTypeProviderComponent:
UseType<String>,
@cgp.core.error.ErrorRaiserComponent.String:
ReturnError,
}
}

check_components! {
App {
ErrorRaiserComponent: String,
}
}
Loading