Skip to content

Commit

Permalink
Rename define_components! to cgp_preset! with slight improvement (#…
Browse files Browse the repository at this point in the history
…41)

* Implement for_each_replace macro logic

* Test and export for_each_replace

* Make exclude list optional

* Use for_each_replace inside define_components

* Rename define_compoents to cgp_preset

* Fix body expansion inside substitution

* Reorganize preset constructs

* Reorganize preset AST

* Split out tests for delegate_components

* Try to capture generic parameters in preset

* Remove phantom type inside IsPreset

* Do not include preset generics inside IsPreset impl for now

* Allow generics inside for_each_replace

* Pass replacements as TokenStream

* Add list-based replacement proc macro

* Use replace_with! instead of for_each_replace!

* Use `impl<T> IsPreset<Name> for T` for bulk delegation

* Add changelog
  • Loading branch information
soareschen authored Dec 8, 2024
1 parent 7c680b7 commit 287acd4
Show file tree
Hide file tree
Showing 22 changed files with 809 additions and 517 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Pre-Release

- Rename `define_components!` to `cgp_preset!` with slight improvement - [#41](https://github.com/contextgeneric/cgp/pull/41)
- Introduce `replace_with!` macro that allows replacement of an identifier with a list of component types in the body.
- Introduce `for_each_replace!` macro that allows repeated replacement of an identifier with each element of components in the list in the body.
- Rename `define_components!` to `cgp_preset!`.
- Use `replace_with!` inside the generated `with_preset!` macro.
- Re-introduce the `IsPreset` trait to allow bulk delegation of components.


- Redesign `derive_component` to `cgp_component` with improved syntax - [#38](https://github.com/contextgeneric/cgp/pull/38)
- Rename the attribute `#[derive_component]` to `#[cgp_component]`
- The macro syntax has been changed as follows:
Expand Down
2 changes: 1 addition & 1 deletion crates/cgp-component-macro-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ description = """
"""

[dependencies]
syn = { version = "2.0.37", features = [ "full" ] }
syn = { version = "2.0.37", features = [ "full", "extra-traits" ] }
quote = "1.0.33"
proc-macro2 = "1.0.67"
itertools = "0.11.0"
Expand Down
28 changes: 1 addition & 27 deletions crates/cgp-component-macro-lib/src/delegate_components/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@ use quote::ToTokens;
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::token::{Bracket, Colon, Comma, Lt};
use syn::{braced, bracketed, Generics, Ident, Token, Type};
use syn::{braced, bracketed, Generics, Token, Type};

pub struct DelegateComponentsAst {
pub target_type: Type,
pub target_generics: Generics,
pub delegate_entries: DelegateEntriesAst,
}

pub struct DefineComponentsAst {
pub components_ident: Ident,
pub components_generics: Generics,
pub delegate_entries: DelegateEntriesAst,
}

pub struct DelegateEntriesAst {
pub entries: Punctuated<DelegateEntryAst, Comma>,
}
Expand Down Expand Up @@ -63,26 +57,6 @@ impl Parse for DelegateComponentsAst {
}
}

impl Parse for DefineComponentsAst {
fn parse(input: ParseStream) -> syn::Result<Self> {
let components_ident: Ident = input.parse()?;

let components_generics = if input.peek(Lt) {
input.parse()?
} else {
Default::default()
};

let delegate_entries: DelegateEntriesAst = input.parse()?;

Ok(Self {
components_ident,
components_generics,
delegate_entries,
})
}
}

impl Parse for DelegateEntriesAst {
fn parse(input: ParseStream) -> syn::Result<Self> {
let entries = {
Expand Down
67 changes: 0 additions & 67 deletions crates/cgp-component-macro-lib/src/delegate_components/define.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use quote::ToTokens;
use crate::delegate_components::ast::DelegateComponentsAst;
use crate::delegate_components::impl_delegate::impl_delegate_components;

pub fn delegate_components(body: TokenStream) -> TokenStream {
let ast: DelegateComponentsAst = syn::parse2(body).unwrap();
pub fn delegate_components(body: TokenStream) -> syn::Result<TokenStream> {
let ast: DelegateComponentsAst = syn::parse2(body)?;

let impl_items = impl_delegate_components(
&ast.target_type,
Expand All @@ -19,5 +19,5 @@ pub fn delegate_components(body: TokenStream) -> TokenStream {
output.extend(impl_item.to_token_stream());
}

output
Ok(output)
}
3 changes: 0 additions & 3 deletions crates/cgp-component-macro-lib/src/delegate_components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
pub mod ast;
pub mod define;
pub mod define_struct;
pub mod delegate;
pub mod delegates_to;
pub mod impl_delegate;
pub mod merge_generics;
pub mod substitution_macro;

pub use define::define_components;
pub use delegate::delegate_components;

This file was deleted.

Loading

0 comments on commit 287acd4

Please sign in to comment.