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
48 changes: 0 additions & 48 deletions crates/cgp-macro-lib/src/delegate_components/attributes.rs

This file was deleted.

49 changes: 0 additions & 49 deletions crates/cgp-macro-lib/src/delegate_components/derive_namespace.rs

This file was deleted.

4 changes: 0 additions & 4 deletions crates/cgp-macro-lib/src/delegate_components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
mod attributes;
mod define_struct;
mod derive_namespace;
mod impl_delegate;
mod merge_generics;

pub use attributes::*;
pub use define_struct::*;
pub use derive_namespace::*;
pub use impl_delegate::*;
14 changes: 1 addition & 13 deletions crates/cgp-macro-lib/src/entrypoints/delegate_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use proc_macro2::TokenStream;
use quote::ToTokens;
use syn::parse2;

use crate::delegate_components::{
DelegateNamespaceAttribute, define_struct, derive_namespace_delegate, impl_delegate_components,
parse_delegate_attributes,
};
use crate::delegate_components::{define_struct, impl_delegate_components};
use crate::parse::{DelegateComponents, SimpleType, TypeGenerics};

pub fn delegate_components(body: TokenStream) -> syn::Result<TokenStream> {
Expand All @@ -26,15 +23,6 @@ pub fn delegate_components(body: TokenStream) -> syn::Result<TokenStream> {
output.extend(component_struct.to_token_stream());
}

let attributes = parse_delegate_attributes(spec.attributes)?;

if let Some(DelegateNamespaceAttribute { namespace }) = attributes.use_namespace {
let namespace_impl =
derive_namespace_delegate(namespace, target_type, &target_generics.generics)?;

output.extend(namespace_impl);
}

let impl_items = impl_delegate_components(target_type, target_generics, &spec.entries)?;

output.extend(impl_items);
Expand Down
46 changes: 36 additions & 10 deletions crates/cgp-macro-lib/src/parse/delegate_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ 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, Semi};
use syn::{Attribute, Error, Generics, Ident, Token, Type, braced, bracketed, parse_quote, parse2};
use syn::token::{At, Bracket, Colon, Comma, Gt, Lt, RArrow, Semi};
use syn::{Error, Generics, Ident, Token, Type, braced, bracketed, parse_quote, parse2};

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

pub struct DelegateComponents {
pub attributes: Vec<Attribute>,
pub new_struct: bool,
pub target_type: Type,
pub target_generics: ImplGenerics,
Expand Down Expand Up @@ -73,12 +72,6 @@ impl DelegateValue {

impl Parse for DelegateComponents {
fn parse(input: ParseStream) -> syn::Result<Self> {
let attributes = if input.peek(Pound) {
input.call(Attribute::parse_outer)?
} else {
Vec::new()
};

let target_generics = if input.peek(Lt) {
input.parse()?
} else {
Expand Down Expand Up @@ -110,7 +103,6 @@ impl Parse for DelegateComponents {
let entries = meta_entries.into_iter().chain(delegate_entries).collect();

Ok(Self {
attributes,
new_struct,
target_type,
target_generics,
Expand Down Expand Up @@ -153,6 +145,40 @@ pub fn parse_meta_delegate_entries(

entries.push(entry)
}
} else if keyword == "namespace" {
input.advance_to(&fork);

let ident: Ident = input.parse()?;
let _: Semi = input.parse()?;

let namespace_ident = if ident == "default" {
Ident::new("DefaultNamespace", ident.span())
} else {
ident
};

let delegate_key: Type = parse2(quote! {
__Component__
})?;

let generics: ImplGenerics = parse2(quote! {
<__Component__: #namespace_ident< #target_type >>
})?;

let delegate_value: Type = parse2(quote! {
< __Component__ as #namespace_ident< #target_type >>::Provider
})?;

let entry = DelegateEntry {
keys: Punctuated::from_iter([DelegateKey {
ty: delegate_key,
generics,
}]),
mode: DelegateMode::Provider(Colon(Span::call_site())),
value: DelegateValue::Type(delegate_value),
};

entries.push(entry)
} else {
break;
}
Expand Down
15 changes: 15 additions & 0 deletions crates/cgp-macro-lib/src/parse/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub fn path_type_as_ident(path_type: &Type) -> Option<Ident> {
let path_str = path_ident.to_string();
if let Some(path_char) = path_str.chars().next()
&& path_char.is_ascii_lowercase()
&& !is_primitive_type(&path_str)
{
return Some(path_ident);
}
Expand Down Expand Up @@ -166,3 +167,17 @@ impl Parse for PathType {
}
}
}

pub fn is_primitive_type(ident: &str) -> bool {
if (ident.starts_with("i") || ident.starts_with("u") || ident.starts_with("f"))
&& ident[1..].chars().all(|c| c.is_numeric())
{
return true;
}

if ["char", "bool", "usize", "isize", "str"].contains(&ident) {
return true;
}

false
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ impl BarProvider {
pub struct App;

delegate_components! {
#[use_namespace(MyNamespace)]
App {
namespace MyNamespace;

@MyFooComponent:
DummyFoo,
@MyBarComponent:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use cgp_tests::namespaces::ExtendedNamespace;
pub struct App;

delegate_components! {
#[use_namespace(ExtendedNamespace)]
App {
namespace ExtendedNamespace;

@app.ErrorTypeProviderComponent:
UseType<String>,
@app.{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ impl BarProvider {
pub struct App;

delegate_components! {
#[use_namespace(MyNamespace)]
App {
namespace MyNamespace;

@MyApp.MyFooComponent:
DummyFoo,
@MyApp.MyBarComponent:
Expand All @@ -64,8 +65,9 @@ check_components! {
pub struct OtherApp;

delegate_components! {
#[use_namespace(OtherNamespace)]
OtherApp {
namespace OtherNamespace;

@my_app.MyFooComponent:
DummyFoo,
@my_app.MyBarComponent:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ impl BarProvider {
pub struct App;

delegate_components! {
#[use_namespace(MyNamespace)]
App {
namespace MyNamespace;

@my_app.MyFooComponent:
DummyFoo,
@my_app.MyBarComponent:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ impl BarProvider {
pub struct App;

delegate_components! {
#[use_namespace(MyNamespace)]
App {
namespace MyNamespace;

@MyApp.MyFooComponent:
DummyFoo,
@MyApp.MyBarComponent:
Expand Down
41 changes: 33 additions & 8 deletions crates/cgp-tests/tests/namespace_tests/open.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
use cgp::core::error::{ErrorRaiserComponent, ErrorTypeProviderComponent};
use cgp::extra::error::ReturnError;
use cgp::prelude::*;

pub struct App;

#[cgp_component(FooProvider)]
pub trait Foo<T> {
fn foo(&self, value: &T);
}

#[cgp_component(BarProvider)]
pub trait Bar<T> {
fn bar(&self, value: &T);
}

#[cgp_impl(new DummyFoo)]
impl<T> FooProvider<T> {
fn foo(&self, _value: &T) {}
}

#[cgp_impl(new DummyBar)]
impl<T> BarProvider<T> {
fn bar(&self, _value: &T) {}
}

delegate_components! {
App {
open ErrorRaiserComponent;
open FooProviderComponent, BarProviderComponent;

ErrorTypeProviderComponent:
UseType<String>,
@ErrorRaiserComponent.String:
ReturnError,
@FooProviderComponent.String:
DummyFoo,
@BarProviderComponent.{u32, u64, bool, usize, isize}:
DummyBar,
}
}

check_components! {
App {
ErrorRaiserComponent:
FooProviderComponent:
String,
BarProviderComponent: [
u32,
u64,
bool,
usize,
isize,
],
}
}
3 changes: 2 additions & 1 deletion crates/cgp-tests/tests/namespace_tests/redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ impl FooProvider {
pub struct App;

delegate_components! {
#[use_namespace]
App {
namespace default;

// @bar: TestProvider,

@bar.baz: TestProvider,
Expand Down
3 changes: 2 additions & 1 deletion crates/cgp-tests/tests/namespace_tests/use_namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ pub trait Foo {
pub struct App;

delegate_components! {
#[use_namespace]
App {
namespace default;

@cgp.core.error.ErrorTypeProviderComponent:
UseType<String>,
@cgp.core.error.ErrorRaiserComponent.String:
Expand Down
Loading