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: 5 additions & 3 deletions crates/cgp-macro-lib/src/check_components/derive.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use quote::quote;
use syn::punctuated::Punctuated;
use syn::token::Comma;
use syn::{ItemImpl, ItemTrait, Type, parse2};

use crate::check_components::override_span;
use crate::parse::{CheckComponents, CheckEntry};

pub fn derive_check_components(spec: &CheckComponents) -> syn::Result<(ItemTrait, Vec<ItemImpl>)> {
if let Some(providers) = &spec.check_provider {
return derive_check_provider(spec, providers);
if let Some(check_providers) = &spec.check_providers {
return derive_check_provider(spec, check_providers);
}

let mut item_impls = Vec::new();
Expand Down Expand Up @@ -49,7 +51,7 @@ pub fn derive_check_components(spec: &CheckComponents) -> syn::Result<(ItemTrait

pub fn derive_check_provider(
spec: &CheckComponents,
providers: &[Type],
providers: &Punctuated<Type, Comma>,
) -> syn::Result<(ItemTrait, Vec<ItemImpl>)> {
let mut item_impls = Vec::new();
let unit: Type = parse2(quote!(()))?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::check_components::derive_check_components;
use crate::delegate_components::impl_delegate_components;
use crate::parse::{
CheckComponents, CheckEntries, CheckEntry, DelegateAndCheckSpec, DelegateEntry, DelegateKey,
DelegateValue, ImplGenerics,
ImplGenerics,
};

pub fn delegate_and_check_components(body: TokenStream) -> syn::Result<TokenStream> {
Expand All @@ -19,13 +19,24 @@ pub fn delegate_and_check_components(body: TokenStream) -> syn::Result<TokenStre
.entries
.iter()
.flat_map(|entry| {
entry.keys.iter().map(|component_type| {
entry.keys.iter().flat_map(|key| {
let component_type = &key.component_type;
let span = component_type.span();

CheckEntry {
component_type: component_type.clone(),
component_params: None,
span,
match &key.check_params {
Some(check_params) => check_params
.iter()
.map(|generic| CheckEntry {
component_type: component_type.clone(),
component_params: Some(generic.clone()),
span,
})
.collect::<Vec<_>>(),
None => vec![CheckEntry {
component_type: component_type.clone(),
component_params: None,
span,
}],
}
})
})
Expand All @@ -38,27 +49,25 @@ pub fn delegate_and_check_components(body: TokenStream) -> syn::Result<TokenStre
let keys = entry
.keys
.into_iter()
.map(|ty| DelegateKey {
ty,
.map(|key| DelegateKey {
ty: key.component_type,
generics: ImplGenerics::default(),
})
.collect();

let value = DelegateValue::Type(entry.value);

DelegateEntry {
keys,
value,
value: entry.value,
mode: entry.mode,
}
})
.collect();

let mut out =
impl_delegate_components(&spec.provider_type, &spec.impl_generics, &delegate_entries)?;
impl_delegate_components(&spec.context_type, &spec.impl_generics, &delegate_entries)?;

let check_spec = CheckComponents {
check_provider: None,
check_providers: None,
impl_generics: spec.impl_generics,
trait_name: spec.trait_name,
context_type: spec.context_type,
Expand Down
77 changes: 47 additions & 30 deletions crates/cgp-macro-lib/src/parse/check_components.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use proc_macro2::Span;
use quote::ToTokens;
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::token::{Bracket, Colon, Comma, For, Lt, Pound, Where};
use syn::{Ident, Type, WhereClause, braced, bracketed, parenthesized};
use syn::token::{Bracket, Colon, Comma, Lt, Pound, Where};
use syn::{Attribute, Ident, Type, WhereClause, braced, bracketed, parse2};

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

pub struct CheckComponentsSpecs {
pub specs: Vec<CheckComponents>,
}

pub struct CheckComponents {
pub check_provider: Option<Vec<Type>>,
pub check_providers: Option<Punctuated<Type, Comma>>,
pub impl_generics: ImplGenerics,
pub trait_name: Ident,
pub context_type: Type,
Expand Down Expand Up @@ -49,29 +50,38 @@ impl Parse for CheckComponentsSpecs {

impl Parse for CheckComponents {
fn parse(input: ParseStream) -> syn::Result<Self> {
let check_provider = if input.peek(Pound) {
let _: Pound = input.parse()?;

let content;
bracketed!(content in input);

let command: Ident = content.parse()?;
if command != "check_providers" {
return Err(syn::Error::new(
command.span(),
"expected `check_providers` attribute",
));
let mut check_providers: Option<Punctuated<Type, Comma>> = None;
let mut m_check_trait_name: Option<Ident> = None;

if input.peek(Pound) {
let attributes = input.call(Attribute::parse_outer)?;

for attribute in attributes {
if attribute.path().is_ident("check_providers") {
let provider_types: Punctuated<Type, Comma> =
attribute.parse_args_with(Punctuated::parse_terminated)?;

check_providers
.get_or_insert_default()
.extend(provider_types);
} else if attribute.path().is_ident("check_trait") {
let check_trait_name: Ident = attribute.parse_args()?;

if m_check_trait_name.is_some() {
return Err(syn::Error::new(
attribute.span(),
"Multiple `#[check_trait]` attributes found. Expected at most one.",
));
}

m_check_trait_name = Some(check_trait_name);
} else {
return Err(syn::Error::new(
attribute.span(),
format!("Invalid attribute {}", attribute.to_token_stream()),
));
}
}

let raw_providers;
parenthesized!(raw_providers in content);

let provider_types: Punctuated<Type, Comma> =
Punctuated::parse_terminated(&raw_providers)?;

Some(provider_types.into_iter().collect())
} else {
None
};

let impl_generics = if input.peek(Lt) {
Expand All @@ -80,11 +90,18 @@ impl Parse for CheckComponents {
Default::default()
};

let trait_name = input.parse()?;
let context_type: Type = input.parse()?;

let _: For = input.parse()?;
let trait_name = if let Some(check_trait_name) = m_check_trait_name {
check_trait_name
} else {
let context_type: SimpleType = parse2(context_type.to_token_stream())?;

let context_type: Type = input.parse()?;
Ident::new(
&format!("__CanUse{}", context_type.name),
context_type.span(),
)
};

let where_clause = if input.peek(Where) {
input.parse()?
Expand All @@ -101,7 +118,7 @@ impl Parse for CheckComponents {
let entries: CheckEntries = content.parse()?;

Ok(Self {
check_provider,
check_providers,
impl_generics,
trait_name,
context_type,
Expand Down
112 changes: 96 additions & 16 deletions crates/cgp-macro-lib/src/parse/delegate_and_check_components.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
use core::iter;

use quote::ToTokens;
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::token::{Bracket, Comma, For, Lt, Semi};
use syn::{Ident, Type, braced, bracketed};
use syn::spanned::Spanned;
use syn::token::{Bracket, Comma, Lt, Pound};
use syn::{Attribute, Ident, Type, braced, bracketed, parse2};

use crate::parse::{DelegateMode, ImplGenerics};
use crate::parse::{DelegateMode, DelegateValue, ImplGenerics, SimpleType};

pub struct DelegateAndCheckSpec {
pub impl_generics: ImplGenerics,
pub trait_name: Ident,
pub context_type: Type,
pub provider_type: Type,
pub entries: Punctuated<DelegateAndCheckEntry, Comma>,
}

#[derive(Clone)]
pub struct DelegateAndCheckEntry {
pub keys: Punctuated<Type, Comma>,
pub keys: Punctuated<DelegateAndCheckKey, Comma>,
pub mode: DelegateMode,
pub value: Type,
pub value: DelegateValue,
}

#[derive(Clone)]
pub struct DelegateAndCheckKey {
pub component_type: Type,
pub check_params: Option<Punctuated<Type, Comma>>,
}

impl Parse for DelegateAndCheckSpec {
Expand All @@ -30,15 +37,20 @@ impl Parse for DelegateAndCheckSpec {
Default::default()
};

let trait_name = input.parse()?;

let _: For = input.parse()?;
let m_trait_name = parse_check_trait_name(input)?;

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

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

let provider_type = input.parse()?;
let trait_name = match m_trait_name {
Some(ident) => ident,
None => {
let context_type: SimpleType = parse2(context_type.to_token_stream())?;
Ident::new(
&format!("__CanUse{}", context_type.name),
context_type.span(),
)
}
};

let entries = {
let body;
Expand All @@ -50,27 +62,95 @@ impl Parse for DelegateAndCheckSpec {
impl_generics,
trait_name,
context_type,
provider_type,
entries,
})
}
}

impl Parse for DelegateAndCheckEntry {
fn parse(input: ParseStream) -> syn::Result<Self> {
let keys = if input.peek(Bracket) {
let check_params = parse_check_params(input)?;

let mut keys = if input.peek(Bracket) {
let body;
bracketed!(body in input);
Punctuated::parse_terminated(&body)?
} else {
let key: Type = input.parse()?;
let key: DelegateAndCheckKey = input.parse()?;
Punctuated::from_iter(iter::once(key))
};

if let Some(check_params) = check_params {
for key in &mut keys {
key.check_params
.get_or_insert_default()
.extend(check_params.clone());
}
}

let mode = input.parse()?;

let value = input.parse()?;

Ok(Self { keys, mode, value })
}
}

impl Parse for DelegateAndCheckKey {
fn parse(input: ParseStream) -> syn::Result<Self> {
let check_params = parse_check_params(input)?;

let component_type: Type = input.parse()?;
Ok(Self {
component_type,
check_params,
})
}
}

pub fn parse_check_trait_name(input: ParseStream) -> syn::Result<Option<Ident>> {
if input.peek(Pound) {
let attributes = input.call(Attribute::parse_outer)?;

let [attribute]: [Attribute; 1] = attributes
.try_into()
.map_err(|_| input.error("Expected exactly one attribute for the check trait name"))?;

if !attribute.path().is_ident("check_trait") {
return Err(syn::Error::new(
attribute.span(),
"Expected `#[check_trait]` attribute for specifying the check trait name",
));
}

let ident: Ident = attribute.parse_args()?;
Ok(Some(ident))
} else {
Ok(None)
}
}

pub fn parse_check_params(input: ParseStream) -> syn::Result<Option<Punctuated<Type, Comma>>> {
if input.peek(Pound) {
let attributes = input.call(Attribute::parse_outer)?;

let [attribute]: [Attribute; 1] = attributes
.try_into()
.map_err(|_| input.error("Expected exactly one key attribute"))?;

let check_params = if attribute.path().is_ident("check_params") {
attribute.parse_args_with(Punctuated::parse_terminated)?
} else if attribute.path().is_ident("skip_check") {
Punctuated::new()
} else {
return Err(syn::Error::new(
attribute.span(),
"Expected either `#[skip_check]` or `#[check_params]` attribute for specifying the check generics",
));
};

Ok(Some(check_params))
} else {
Ok(None)
}
}
Loading
Loading