Skip to content

Commit 287acd4

Browse files
authored
Rename define_components! to cgp_preset! with slight improvement (#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
1 parent 7c680b7 commit 287acd4

File tree

22 files changed

+809
-517
lines changed

22 files changed

+809
-517
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
## Pre-Release
44

5+
- Rename `define_components!` to `cgp_preset!` with slight improvement - [#41](https://github.com/contextgeneric/cgp/pull/41)
6+
- Introduce `replace_with!` macro that allows replacement of an identifier with a list of component types in the body.
7+
- Introduce `for_each_replace!` macro that allows repeated replacement of an identifier with each element of components in the list in the body.
8+
- Rename `define_components!` to `cgp_preset!`.
9+
- Use `replace_with!` inside the generated `with_preset!` macro.
10+
- Re-introduce the `IsPreset` trait to allow bulk delegation of components.
11+
12+
513
- Redesign `derive_component` to `cgp_component` with improved syntax - [#38](https://github.com/contextgeneric/cgp/pull/38)
614
- Rename the attribute `#[derive_component]` to `#[cgp_component]`
715
- The macro syntax has been changed as follows:

crates/cgp-component-macro-lib/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ description = """
1212
"""
1313

1414
[dependencies]
15-
syn = { version = "2.0.37", features = [ "full" ] }
15+
syn = { version = "2.0.37", features = [ "full", "extra-traits" ] }
1616
quote = "1.0.33"
1717
proc-macro2 = "1.0.67"
1818
itertools = "0.11.0"

crates/cgp-component-macro-lib/src/delegate_components/ast.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@ use quote::ToTokens;
55
use syn::parse::{Parse, ParseStream};
66
use syn::punctuated::Punctuated;
77
use syn::token::{Bracket, Colon, Comma, Lt};
8-
use syn::{braced, bracketed, Generics, Ident, Token, Type};
8+
use syn::{braced, bracketed, Generics, Token, Type};
99

1010
pub struct DelegateComponentsAst {
1111
pub target_type: Type,
1212
pub target_generics: Generics,
1313
pub delegate_entries: DelegateEntriesAst,
1414
}
1515

16-
pub struct DefineComponentsAst {
17-
pub components_ident: Ident,
18-
pub components_generics: Generics,
19-
pub delegate_entries: DelegateEntriesAst,
20-
}
21-
2216
pub struct DelegateEntriesAst {
2317
pub entries: Punctuated<DelegateEntryAst, Comma>,
2418
}
@@ -63,26 +57,6 @@ impl Parse for DelegateComponentsAst {
6357
}
6458
}
6559

66-
impl Parse for DefineComponentsAst {
67-
fn parse(input: ParseStream) -> syn::Result<Self> {
68-
let components_ident: Ident = input.parse()?;
69-
70-
let components_generics = if input.peek(Lt) {
71-
input.parse()?
72-
} else {
73-
Default::default()
74-
};
75-
76-
let delegate_entries: DelegateEntriesAst = input.parse()?;
77-
78-
Ok(Self {
79-
components_ident,
80-
components_generics,
81-
delegate_entries,
82-
})
83-
}
84-
}
85-
8660
impl Parse for DelegateEntriesAst {
8761
fn parse(input: ParseStream) -> syn::Result<Self> {
8862
let entries = {

crates/cgp-component-macro-lib/src/delegate_components/define.rs

Lines changed: 0 additions & 67 deletions
This file was deleted.

crates/cgp-component-macro-lib/src/delegate_components/delegate.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use quote::ToTokens;
44
use crate::delegate_components::ast::DelegateComponentsAst;
55
use crate::delegate_components::impl_delegate::impl_delegate_components;
66

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

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

22-
output
22+
Ok(output)
2323
}
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
pub mod ast;
2-
pub mod define;
32
pub mod define_struct;
43
pub mod delegate;
54
pub mod delegates_to;
65
pub mod impl_delegate;
76
pub mod merge_generics;
8-
pub mod substitution_macro;
97

10-
pub use define::define_components;
118
pub use delegate::delegate_components;

crates/cgp-component-macro-lib/src/delegate_components/substitution_macro.rs

Lines changed: 0 additions & 140 deletions
This file was deleted.

0 commit comments

Comments
 (0)