Skip to main content

lombok_macros/
lib.rs

1//! lombok-macros
2//!
3//! A Rust procedural macro collection providing Lombok-like functionality.
4//! Automatically generates getters/setters with field-level visibility control,
5//! custom Debug implementations with field skipping, and Display trait implementations.
6//! Supports structs, enums, generics and lifetimes.
7
8mod config;
9mod func;
10mod generate;
11mod parse;
12mod visibility;
13
14use {config::*, func::*, generate::*, parse::*, visibility::*};
15
16use {
17    proc_macro::TokenStream,
18    proc_macro2::{Delimiter, Span, token_stream::IntoIter},
19    quote::{ToTokens, quote},
20    syn::{
21        AngleBracketedGenericArguments, Data, DeriveInput, Field, Fields, GenericArgument,
22        GenericParam, Generics, Ident, Index, Lifetime, PathArguments, Type, TypeArray, TypeGroup,
23        TypeParam, TypeParamBound, TypeParen, TypePath, TypePtr, TypeReference, TypeSlice,
24        TypeTraitObject, TypeTuple, Variant, WhereClause, parse_macro_input, parse_quote, parse2,
25    },
26};
27
28use std::{
29    collections::{HashMap, HashSet},
30    fmt::{Display, Formatter},
31    iter::Peekable,
32    str::FromStr,
33};
34
35/// A procedural macro that automatically generates getter methods for struct and enum fields.
36///
37/// This macro derives getter methods with configurable visibility and return type behavior.
38/// The generated getters can return either references to field values or cloned copies,
39/// with support for Option and Result types.
40///
41/// # Supported Attributes
42/// - `#[get(pub)]` - Generates a public getter with reference return type
43/// - `#[get(pub)]` - Generates a public getter that returns a reference (`&T`)
44/// - `#[get(pub, clone)]` - Generates a public getter that returns a cloned value (`T`)
45/// - `#[get(pub, copy)]` - Generates a public getter that returns a copy of the field value (`self.field`) for Copy types
46/// - `#[get(pub, deref)]` - Generates a public getter that returns a dereferenced value (`*field`) with enhanced match control for Option/Result types
47/// - `#[get(pub(crate))]` - Generates a crate-visible getter
48/// - `#[get(private)]` - Generates a private getter
49///
50/// # Return Type Behavior
51/// - `reference` - Returns `&T` - a reference to the field value
52/// - `clone` - Returns `T` - a cloned copy of the field value  
53/// - `copy` - Returns `T` - a copy of the field value (`self.field`) for types implementing Copy trait
54/// - `deref` - Returns dereferenced values with enhanced match control:
55///   - `Option<T>` → `T` with detailed None panic messages
56///   - `Result<T, E>` → `T` with detailed Err panic messages
57///   - `Box<T>` → `T` by dereferencing the box
58///   - `Rc<T>` → `T` by cloning the inner value
59///   - `Arc<T>` → `T` by cloning the inner value
60///   - Other types → `T` by dereferencing
61/// - Default behavior: Returns `&T` for non-Option/Result types, `T` for Option/Result types
62///
63/// # Default Behavior Details
64/// - **Non-Option/Result types**: Returns `&T` (reference to field)
65/// - **Option/Result types**: Returns `T` (cloned value) to avoid exposing internal references
66/// - This ensures safe access patterns while maintaining performance for common use cases
67///
68/// # Examples
69///
70/// ## Basic Usage
71///
72/// ```rust
73/// use lombok_macros::*;
74///
75/// #[derive(Clone, Getter)]
76/// struct BasicStruct {
77///     #[get(pub)]
78///     name: String,
79///     #[get(pub)]
80///     description: String,
81///     #[get(pub, type(clone))]
82///     data: Vec<i32>,
83///     #[get(pub, type(copy))]
84///     count: i32,
85/// }
86///
87/// let basic = BasicStruct {
88///     name: "test".to_string(),
89///     description: "description".to_string(),
90///     data: vec![1, 2, 3],
91///     count: 42,
92/// };
93/// let name_ref: &String = basic.get_name();
94/// let description_ref: &String = basic.get_description();
95/// let data_clone: Vec<i32> = basic.get_data();
96/// let count_copy: i32 = basic.get_count();
97/// assert_eq!(*name_ref, "test");
98/// assert_eq!(*description_ref, "description");
99/// assert_eq!(data_clone, vec![1, 2, 3]);
100/// assert_eq!(count_copy, 42);
101/// ```
102///
103/// ## Option and Result Types
104///
105/// ```rust
106/// use lombok_macros::*;
107///
108/// #[derive(Clone, Getter)]
109/// struct OptionalStruct {
110///     #[get(pub)]
111///     optional: Option<String>,
112///     #[get(pub)]
113///     optional_ref: Option<String>,
114///     #[get(pub)]
115///     result: Result<String, String>,
116/// }
117///
118/// let opt_struct = OptionalStruct {
119///     optional: Some("value".to_string()),
120///     optional_ref: Some("ref_value".to_string()),
121///     result: Ok("success".to_string()),
122/// };
123/// let optional_value: String = opt_struct.get_optional();
124/// let optional_reference: String = opt_struct.get_optional_ref();
125/// let result_value: String = opt_struct.get_result();
126/// assert_eq!(optional_value, "value");
127/// assert_eq!(optional_reference, "ref_value");
128/// assert_eq!(result_value, "success");
129/// ```
130///
131/// ## Tuple Structs
132///
133/// ```rust
134/// use lombok_macros::*;
135///
136/// #[derive(Clone, Getter)]
137/// struct TupleStruct(
138///     #[get(pub)] String,
139///     #[get(pub, type(clone))] Vec<i32>,
140/// );
141///
142/// let tuple = TupleStruct("hello".to_string(), vec![1, 2, 3]);
143/// let field0: &String = tuple.get_0();
144/// let field1: Vec<i32> = tuple.get_1();
145/// assert_eq!(*field0, "hello");
146/// assert_eq!(field1, vec![1, 2, 3]);
147/// ```
148///
149/// ## Copy Return Type
150///
151/// ```rust
152/// use lombok_macros::*;
153///
154/// #[derive(Clone, Getter)]
155/// struct CopyStruct {
156///     #[get(pub, type(copy))]
157///     value: i32,
158///     #[get(pub, type(copy))]
159///     flag: bool,
160///     #[get(pub, type(copy))]
161///     count: u64,
162/// }
163///
164/// let copy_struct = CopyStruct {
165///     value: 42,
166///     flag: true,
167///     count: 1000,
168/// };
169/// let copied_value: i32 = copy_struct.get_value();
170/// let copied_flag: bool = copy_struct.get_flag();
171/// let copied_count: u64 = copy_struct.get_count();
172/// assert_eq!(copied_value, 42);
173/// assert_eq!(copied_flag, true);
174/// assert_eq!(copied_count, 1000);
175/// ```
176///
177/// ## Deref Return Type with Enhanced Match Control
178///
179/// ```rust
180/// use lombok_macros::*;
181///
182/// #[derive(Clone, Getter)]
183/// struct DerefStruct {
184///     #[get(pub, type(deref))]
185///     optional: Option<bool>,
186///     #[get(pub, type(deref))]
187///     result: Result<String, &'static str>,
188///     #[get(pub, type(deref))]
189///     boxed_value: Box<i32>,
190///     #[get(pub, type(deref))]
191///     rc_value: std::rc::Rc<String>,
192///     #[get(pub, type(deref))]
193///     arc_value: std::sync::Arc<Vec<u8>>,
194/// }
195///
196/// let deref_struct = DerefStruct {
197///     optional: Some(true),
198///     result: Ok("success".to_string()),
199///     boxed_value: Box::new(100),
200///     rc_value: std::rc::Rc::new("test".to_string()),
201///     arc_value: std::sync::Arc::new(vec![1, 2, 3]),
202/// };
203/// let optional_value: bool = deref_struct.get_optional();
204/// let result_value: String = deref_struct.get_result();
205/// let boxed_value: i32 = deref_struct.get_boxed_value();
206/// let rc_value: String = deref_struct.get_rc_value();
207/// let arc_value: Vec<u8> = deref_struct.get_arc_value();
208/// assert_eq!(optional_value, true);
209/// assert_eq!(result_value, "success");
210/// assert_eq!(boxed_value, 100);
211/// assert_eq!(rc_value, "test");
212/// assert_eq!(arc_value, vec![1, 2, 3]);
213/// ```
214///
215/// ## Generics and Lifetimes
216///
217/// ```rust
218/// use lombok_macros::*;
219///
220/// #[derive(Clone, Getter)]
221/// struct GenericStruct<'a, T: Clone> {
222///     #[get(pub)]
223///     value: &'a T,
224///     #[get(pub, type(clone))]
225///     owned: T,
226/// }
227///
228/// let data = 42;
229/// let generic = GenericStruct {
230///     value: &data,
231///     owned: 42,
232/// };
233/// let value_ref: &i32 = generic.get_value();
234/// let owned_clone: i32 = generic.get_owned();
235/// assert_eq!(*value_ref, 42);
236/// assert_eq!(owned_clone, 42);
237/// ```
238/// # Arguments
239///
240/// - `TokenStream` - The input token stream containing the struct or enum to derive getters for.
241///
242/// # Returns
243///
244/// - `TokenStream` - The expanded `impl` block containing the generated getters.
245#[proc_macro_derive(Getter, attributes(get))]
246pub fn getter(input: TokenStream) -> TokenStream {
247    inner_lombok_data(input, true, false, false)
248}
249
250/// A procedural macro that automatically generates mutable getter methods for struct and enum fields.
251///
252/// This macro derives mutable getter methods that provide mutable references to field values,
253/// allowing modification of the struct's fields while maintaining proper borrowing semantics.
254///
255/// # Supported Attributes
256/// - `#[get_mut(pub)]` - Generates a public mutable getter
257/// - `#[get_mut(pub(crate))]` - Generates a crate-visible mutable getter
258/// - `#[get_mut(pub(super))]` - Generates a mutable getter visible to parent module
259/// - `#[get_mut(private)]` - Generates a private mutable getter
260///
261/// # Example
262///
263/// ```rust
264/// use lombok_macros::*;
265///
266/// #[derive(Clone, GetterMut)]
267/// struct StructWithLifetimes<'a, 'b, T: Clone> {
268///     #[get_mut(pub(crate))]
269///     list: Vec<String>,
270///     #[get_mut(pub(crate))]
271///     optional_lifetime_a: Option<&'a T>,
272///     optional_lifetime_b: Option<&'b str>,
273/// }
274///
275/// let list: Vec<String> = vec!["hello".to_string(), "world".to_string()];
276/// let mut struct_with_lifetimes: StructWithLifetimes<usize> = StructWithLifetimes {
277///     list: list.clone(),
278///     optional_lifetime_a: None,
279///     optional_lifetime_b: None,
280/// };
281/// let mut list_reference: &mut Vec<String> = struct_with_lifetimes.get_mut_list();
282/// list_reference.push("new_item".to_string());
283/// assert_eq!(*list_reference, vec!["hello".to_string(), "world".to_string(), "new_item".to_string()]);
284/// ```
285/// # Arguments
286///
287/// - `TokenStream` - The input token stream containing the struct or enum to derive mutable getters for.
288///
289/// # Returns
290///
291/// - `TokenStream` - The expanded `impl` block containing the generated mutable getters.
292#[proc_macro_derive(GetterMut, attributes(get_mut))]
293pub fn getter_mut(input: TokenStream) -> TokenStream {
294    inner_lombok_data(input, false, true, false)
295}
296
297/// A procedural macro that automatically generates setter methods for struct and enum fields.
298///
299/// This macro derives setter methods that allow modification of struct fields with
300/// configurable visibility and parameter type conversion options.
301///
302/// # Supported Attributes
303/// - `#[set(pub)]` - Generates a public setter
304/// - `#[set(pub(crate))]` - Generates a crate-visible setter
305/// - `#[set(pub(super))]` - Generates a setter visible to parent module
306/// - `#[set(private)]` - Generates a private setter
307/// - `#[set(pub, type(AsRef<str>))]` - Generates a setter with custom parameter type conversion
308/// - `#[set(pub, Into)]` - Generates a setter using `impl Into<T>` trait bound
309/// - `#[set(pub, type(AsRef<[u8]>))]` - Generates a setter with `impl AsRef<[u8]>` parameter type
310///
311/// # Parameter Type Conversion
312/// Setters support flexible parameter type conversion through trait bounds:
313/// - `type(AsRef<T>)` - Accepts any type implementing `AsRef<T>` and converts using `.as_ref().to_owned()`
314/// - `type(Into<T>)` - Accepts any type implementing `Into<T>` and converts using `.into()`
315/// - `type(CustomTrait<T>)` - Accepts any type implementing the specified custom trait bound
316///
317/// # Examples
318///
319/// ## Basic Usage
320///
321/// ```rust
322/// use lombok_macros::*;
323///
324/// #[derive(Clone, Debug, Setter)]
325/// struct BasicStruct {
326///     #[set(pub)]
327///     name: String,
328///     #[set(pub(crate))]
329///     value: i32,
330///     #[set(private)]
331///     secret: String,
332/// }
333///
334/// let mut basic = BasicStruct {
335///     name: "initial".to_string(),
336///     value: 0,
337///     secret: "hidden".to_string(),
338/// };
339/// basic.set_name("updated".to_string());
340/// basic.set_value(42);
341/// assert_eq!(basic.name, "updated");
342/// assert_eq!(basic.value, 42);
343/// ```
344///
345/// ## Parameter Type Conversion
346///
347/// ```rust
348/// use lombok_macros::*;
349///
350/// #[derive(Clone, Debug, Setter)]
351/// struct ConversionStruct {
352///     #[set(pub, type(AsRef<str>))]
353///     name: String,
354///     #[set(pub, type(Into<i32>))]
355///     value: i32,
356///     #[set(pub, type(AsRef<[u8]>))]
357///     data: Vec<u8>,
358/// }
359///
360/// let mut conversion = ConversionStruct {
361///     name: "initial".to_string(),
362///     value: 0,
363///     data: vec![1, 2, 3],
364/// };
365///
366/// conversion.set_name("updated");
367/// assert_eq!(conversion.name, "updated");
368///
369/// conversion.set_value(1u8);
370/// assert_eq!(conversion.value, 1);
371///
372/// conversion.set_data(&[4, 5, 6]);
373/// assert_eq!(conversion.data, vec![4, 5, 6]);
374/// ```
375///
376/// ## Tuple Structs
377///
378/// ```rust
379/// use lombok_macros::*;
380///
381/// #[derive(Clone, Debug, Setter)]
382/// struct TupleStruct(
383///     #[set(pub)] String,
384///     #[set(pub)] i32,
385/// );
386///
387/// let mut tuple = TupleStruct("hello".to_string(), 1);
388/// tuple.set_0("world".to_string());
389/// tuple.set_1(100);
390/// assert_eq!(tuple.0, "world");
391/// assert_eq!(tuple.1, 100);
392/// ```
393/// # Arguments
394///
395/// - `TokenStream` - The input token stream containing the struct or enum to derive setters for.
396///
397/// # Returns
398///
399/// - `TokenStream` - The expanded `impl` block containing the generated setters.
400#[proc_macro_derive(Setter, attributes(set))]
401pub fn setter(input: TokenStream) -> TokenStream {
402    inner_lombok_data(input, false, false, true)
403}
404
405/// A procedural macro that combines getter, mutable getter, and setter functionality in a single derive.
406///
407/// This macro derives all three types of accessor methods (getters, mutable getters, and setters)
408/// for struct and enum fields, providing comprehensive data manipulation capabilities with
409/// configurable visibility and behavior options.
410///
411/// # Supported Attributes
412/// - `#[get(...)]` - Controls getter generation (supports `reference`, `clone`, `copy`, `deref` options)
413/// - `#[get_mut(...)]` - Controls mutable getter generation
414/// - `#[set(...)]` - Controls setter generation (supports parameter type conversion with `type(AsRef<T>)`, `Into`, etc.)
415///
416/// # Visibility Control
417/// Each attribute supports the same visibility options:
418/// - `pub` - Public access
419/// - `pub(crate)` - Crate-level access
420/// - `pub(super)` - Parent module access
421/// - `private` - Private access
422///
423/// # Examples
424///
425/// ## Basic Combination
426///
427/// ```rust
428/// use lombok_macros::*;
429///
430/// #[derive(Clone, Data, Debug)]
431/// struct User {
432///     #[get(pub)]
433///     #[set(pub)]
434///     name: String,
435///     #[get(pub, type(clone))]
436///     #[set(pub)]
437///     email: String,
438///     #[get(pub, type(copy))]
439///     age: u32,
440///     #[get_mut(pub)]
441///     mutable_age: u32,
442/// }
443///
444/// let mut user = User {
445///     name: "Alice".to_string(),
446///     email: "alice@ltpp.vip".to_string(),
447///     age: 30,
448///     mutable_age: 25,
449/// };
450/// let name_reference: &String = user.get_name();
451/// let email_clone: String = user.get_email();
452/// let age_copy: u32 = user.get_age();
453/// assert_eq!(*name_reference, "Alice");
454/// assert_eq!(email_clone, "alice@ltpp.vip");
455/// assert_eq!(age_copy, 30);
456///
457/// user.set_name("Bob".to_string());
458/// user.set_email("bob@ltpp.vip".to_string());
459/// let updated_email: String = user.get_email();
460/// assert_eq!(updated_email, "bob@ltpp.vip");
461///
462/// (*user.get_mut_mutable_age() = 31);
463///
464/// assert_eq!(*user.get_mutable_age(), 31);
465/// ```
466///
467/// ## Multiple Field Types
468///
469/// ```rust
470/// use lombok_macros::*;
471///
472/// #[derive(Clone, Data, Debug)]
473/// struct ComplexStruct {
474///     #[get(pub)]
475///     id: i32,
476///     #[get(pub)]
477///     #[set(pub)]
478///     optional: Option<String>,
479///     #[get(pub)]
480///     result: Result<i32, String>,
481///     #[get(pub(crate))]
482///     #[set(private)]
483///     internal_data: Vec<u8>,
484/// }
485///
486/// let mut complex = ComplexStruct {
487///     id: 1,
488///     optional: Some("value".to_string()),
489///     result: Ok(42),
490///     internal_data: vec![1, 2, 3],
491/// };
492///
493/// let id_reference: &i32 = complex.get_id();
494/// let optional_clone: String = complex.get_optional();
495/// let result_reference: i32 = complex.get_result();
496/// assert_eq!(*id_reference, 1);
497/// assert_eq!(optional_clone, "value");
498/// assert_eq!(result_reference, 42);
499/// ```
500///
501/// ## Tuple Struct with Combined Accessors
502///
503/// ```rust
504/// use lombok_macros::*;
505///
506/// #[derive(Clone, Data, Debug)]
507/// struct Point(
508///     #[get(pub)] f64,
509///     #[get(pub, type(clone))]
510///     #[set(pub)] f64,
511/// );
512///
513/// let mut point = Point(1.0, 2.0);
514/// let x_coordinate: &f64 = point.get_0();
515/// let y_coordinate: f64 = point.get_1();
516/// assert_eq!(*x_coordinate, 1.0);
517/// assert_eq!(y_coordinate, 2.0);
518///
519/// point.set_1(3.0);
520/// let updated_y_coordinate: f64 = point.get_1();
521/// assert_eq!(updated_y_coordinate, 3.0);
522/// ```
523/// # Arguments
524///
525/// - `TokenStream` - The input token stream containing the struct to derive all accessors for.
526///
527/// # Returns
528///
529/// - `TokenStream` - The expanded `impl` block containing the generated accessors.
530#[proc_macro_derive(Data, attributes(get, get_mut, set))]
531pub fn data(input: TokenStream) -> TokenStream {
532    let mut result: proc_macro2::TokenStream = proc_macro2::TokenStream::new();
533    let lombok_data: TokenStream = inner_lombok_data(input.clone(), true, true, true);
534    result.extend(
535        lombok_data
536            .to_string()
537            .parse::<proc_macro2::TokenStream>()
538            .unwrap_or_default(),
539    );
540    result.into()
541}
542
543/// A procedural macro that implements the `std::fmt::Display` trait for a type,
544/// using the standard debug format (`{:?}`) for formatting.
545///
546/// This macro derives the `Display` implementation for a type, allowing it to be formatted
547/// using `{:?}` in formatting macros. It uses the `inner_display_debug` function to generate
548/// the implementation with the standard debug format.
549///
550/// # Arguments
551/// - `TokenStream` - The input token stream representing the Rust item (struct, enum, etc.)
552///   for which the `Display` implementation will be generated.
553///
554/// # Returns
555/// - `TokenStream` - The generated `std::fmt::Display` implementation for the type
556///   using the standard debug format.
557#[proc_macro_derive(DisplayDebug)]
558pub fn display_debug(input: TokenStream) -> TokenStream {
559    inner_display_debug(input)
560}
561
562/// A procedural macro that implements the `std::fmt::Display` trait for a type,
563/// using the detailed debug format (`{:#?}`) for formatting.
564///
565/// This macro derives the `Display` implementation for a type, allowing it to be formatted
566/// using `{:#?}` in formatting macros. It uses the `inner_display_debug_format` function
567/// to generate the implementation with the detailed debug format.
568///
569/// # Arguments
570/// - `TokenStream` - The input token stream representing the Rust item (struct, enum, etc.)
571///   for which the `Display` implementation will be generated.
572///
573/// # Returns
574/// - `TokenStream` - The generated `std::fmt::Display` implementation for the type
575///   using the detailed debug format.
576#[proc_macro_derive(DisplayDebugFormat)]
577pub fn display_debug_format(input: TokenStream) -> TokenStream {
578    inner_display_debug_format(input)
579}
580
581/// A procedural macro that implements the `std::fmt::Debug` trait for a type,
582/// with support for the `#[debug(skip)]` attribute to skip specific fields.
583///
584/// This macro derives a custom Debug implementation that behaves like the standard
585/// library's Debug derive, but allows individual fields to be excluded from the
586/// debug output by annotating them with `#[debug(skip)]`.
587///
588/// # Supported Attributes
589/// - `#[debug(skip)]` - Excludes the field from the debug output
590///
591/// # Examples
592///
593/// ## Struct Example
594/// ```rust
595/// use lombok_macros::*;
596///
597/// #[derive(CustomDebug)]
598/// struct User {
599///     name: String,
600///     #[debug(skip)]
601///     password: String,
602///     email: String,
603/// }
604///
605/// let user = User {
606///     name: "Alice".to_string(),
607///     password: "secret123".to_string(),
608///     email: "alice@ltpp.vip".to_string(),
609/// };
610/// let expected_debug = "User { name: \"Alice\", email: \"alice@ltpp.vip\" }";
611/// assert_eq!(format!("{:?}", user), expected_debug);
612/// ```
613///
614/// ## Enum Example
615/// ```rust
616/// use lombok_macros::*;
617///
618/// #[derive(CustomDebug)]
619/// enum Response {
620///     Success { data: String },
621///     Error {
622///         message: String,
623///         #[debug(skip)]
624///         internal_code: u32,
625///     },
626/// }
627///
628/// let success = Response::Success { data: "Hello".to_string() };
629/// let error = Response::Error { message: "Failed".to_string(), internal_code: 500 };
630/// let expected_success = "Success { data: \"Hello\" }";
631/// let expected_error = "Error { message: \"Failed\" }";
632/// assert_eq!(format!("{:?}", success), expected_success);
633/// assert_eq!(format!("{:?}", error), expected_error);
634/// ```
635///
636/// # Arguments
637///
638/// - `TokenStream` - The input token stream representing the Rust item (struct, enum, etc.)
639///   for which the Debug implementation will be generated.
640///
641/// # Returns
642///
643/// - `TokenStream` - The generated `std::fmt::Debug` implementation for the type
644///   that respects the `#[debug(skip)]` attribute.
645#[proc_macro_derive(CustomDebug, attributes(debug))]
646pub fn custom_debug(input: TokenStream) -> TokenStream {
647    inner_custom_debug(input)
648}
649
650/// A procedural macro that generates a constructor function for structs.
651///
652/// This macro automatically generates a `new` function that takes all non-skipped fields
653/// as parameters and returns a new instance of the struct. Fields marked with `#[new(skip)]`
654/// will be initialized with their default values.
655///
656/// # Supported Attributes
657/// - `#[new(skip)]` - Excludes the field from constructor parameters and uses default initialization
658/// - `#[new(pub)]` - Generates a public constructor  
659/// - `#[new(pub(crate))]` - Generates a crate-visible constructor  
660/// - `#[new(pub(super))]` - Generates a constructor visible to parent module  
661/// - `#[new(private)]` - Generates a private constructor
662///
663/// # Default Behavior
664/// - The generated constructor is `pub` by default
665/// - All fields are included in the constructor unless marked with `#[new(skip)]`
666/// - Skipped fields are initialized using `Default::default()`
667///
668/// # Examples
669///
670/// ## Basic Usage
671/// ```rust
672/// use lombok_macros::*;
673///
674/// #[derive(New)]
675/// struct Person {
676///     name: String,
677///     age: u32,
678/// }
679///
680/// let person = Person::new("Alice".to_string(), 30);
681/// assert_eq!(person.name, "Alice");
682/// assert_eq!(person.age, 30);
683/// ```
684///
685/// ## With Skip Attribute
686/// ```rust
687/// use lombok_macros::*;
688///
689/// #[derive(New)]
690/// struct User {
691///     username: String,
692///     email: String,
693///     #[new(skip)]
694///     created_at: String,
695/// }
696///
697/// let user = User::new("alice".to_string(), "alice@ltpp.vip".to_string());
698/// assert_eq!(user.username, "alice");
699/// assert_eq!(user.email, "alice@ltpp.vip");
700/// assert_eq!(user.created_at, "");
701/// ```
702///
703/// ## With Custom Visibility
704/// ```rust
705/// use lombok_macros::*;
706///
707/// #[derive(New)]
708/// #[new(pub(crate))]
709/// struct InternalStruct {
710///     value: i32,
711/// }
712///
713/// let internal = InternalStruct::new(42);
714/// assert_eq!(internal.value, 42);
715/// ```
716///
717/// ## Tuple Structs
718/// ```rust
719/// use lombok_macros::*;
720///
721/// #[derive(New)]
722/// struct Point(
723///     f64,
724///     f64,
725/// );
726///
727/// let origin = Point::new(0.0, 0.0);
728/// assert_eq!(origin.0, 0.0);
729/// assert_eq!(origin.1, 0.0);
730/// ```
731///
732/// ## Generic Types
733/// ```rust
734/// use lombok_macros::*;
735///
736/// #[derive(New)]
737/// struct Container<T: Default + Clone> {
738///     data: T,
739///     #[new(skip)]
740///     count: usize,
741/// }
742///
743/// let container = Container::new("data".to_string());
744/// assert_eq!(container.data, "data");
745/// assert_eq!(container.count, 0);
746/// ```
747///
748/// # Arguments
749/// - `TokenStream` - The input token stream representing the struct for which to generate the constructor.
750///
751/// # Returns
752/// - `TokenStream` - The generated constructor implementation.
753#[proc_macro_derive(New, attributes(new))]
754pub fn new(input: TokenStream) -> TokenStream {
755    let derive_input: DeriveInput = parse_macro_input!(input as DeriveInput);
756    let visibility: Visibility = parse_new_visibility(&derive_input);
757    inner_new_constructor(&derive_input, visibility)
758}