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