talmaci_struct_fragments_rs/lib.rs
1//! Compile-time reusable struct field composition for Rust.
2//!
3//! Use [`compose`] directly on a named-field struct to append public nested
4//! fields. Use [`struct_fragments`] on an inline module when fields must be
5//! flattened from reusable fragment definitions.
6
7use proc_macro::TokenStream;
8
9mod expand;
10mod model;
11mod parse;
12
13/// Appends public nested fields to a standalone named-field struct.
14///
15/// Every entry has the form `field_name: Type`. Local fields stay first and
16/// generated fields follow in entry order. Nested types are ordinary Rust types
17/// and do not need to be fragments.
18///
19/// Standalone `compose` deliberately does not accept flattened fragment names or
20/// `self`. Flattening requires the module-level [`struct_fragments`] macro so the
21/// fragment syntax tree is available in the same invocation.
22///
23/// The original struct's attributes, derives, visibility, generics, where
24/// clauses, documentation, and local fields are preserved.
25///
26/// # Example
27///
28/// ```
29/// use talmaci_struct_fragments_rs::compose;
30///
31/// struct Metadata {
32/// label: String,
33/// }
34///
35/// #[compose(metadata: Option<Metadata>, tags: Vec<String>)]
36/// struct User {
37/// id: i32,
38/// }
39///
40/// let user = User {
41/// id: 1,
42/// metadata: None,
43/// tags: vec!["rust".to_owned()],
44/// };
45/// assert_eq!(user.tags, ["rust"]);
46/// ```
47#[proc_macro_attribute]
48pub fn compose(args: TokenStream, input: TokenStream) -> TokenStream {
49 let result = (|| {
50 let args = syn::parse::<parse::ComposeArgs>(args)?;
51 parse::validate_standalone_compose(&args.entries)?;
52 let destination = syn::parse::<syn::ItemStruct>(input)?;
53 expand::expand_standalone(destination, &args.entries)
54 })();
55
56 match result {
57 Ok(output) => output.into(),
58 Err(error) => error.into_compile_error().into(),
59 }
60}
61
62/// Composes reusable field fragments within an inline module.
63///
64/// Fragment structs remain ordinary Rust structs in the expanded output. The
65/// helper attributes `fragment` and `compose` are consumed by this macro.
66///
67/// Compose entries have three forms:
68///
69/// - `Fragment` flattens a same-module fragment's fields.
70/// - `field_name: Type` creates one public nested field. `Type` can be any
71/// ordinary Rust type and does not need to be a fragment.
72/// - `self` inserts the destination's locally declared fields at that position.
73/// Without `self`, local fields are appended after the compose entries.
74///
75/// Duplicate names across flattened, nested, and local fields are compile-time
76/// errors. Only named-field structs are supported, and generic fragments are
77/// intentionally rejected.
78///
79/// # Example
80///
81/// ```
82/// use talmaci_struct_fragments_rs::struct_fragments;
83///
84/// #[struct_fragments]
85/// mod models {
86/// #[fragment]
87/// pub struct UserCore {
88/// pub id: i32,
89/// pub name: String,
90/// }
91///
92/// pub struct Profile {
93/// pub bio: String,
94/// }
95///
96/// #[compose(UserCore, self, profile: Profile)]
97/// pub struct User {
98/// pub active: bool,
99/// }
100/// }
101///
102/// let user = models::User {
103/// id: 1,
104/// name: "Ada".to_owned(),
105/// active: true,
106/// profile: models::Profile { bio: "Engineer".to_owned() },
107/// };
108/// assert_eq!(user.id, 1);
109/// ```
110#[proc_macro_attribute]
111pub fn struct_fragments(args: TokenStream, input: TokenStream) -> TokenStream {
112 if !args.is_empty() {
113 return syn::Error::new(proc_macro2::Span::call_site(), "`struct_fragments` does not accept arguments")
114 .into_compile_error()
115 .into();
116 }
117
118 match syn::parse(input).and_then(expand::expand) {
119 Ok(output) => output.into(),
120 Err(error) => error.into_compile_error().into(),
121 }
122}