lombok_macros/
lib.rs

1use proc_macro::TokenStream;
2pub(crate) mod cfg;
3pub(crate) mod func;
4pub(crate) mod generate;
5pub(crate) mod parse;
6pub(crate) mod visibility;
7
8pub(crate) use cfg::*;
9pub(crate) use func::*;
10pub(crate) use generate::*;
11pub(crate) use parse::*;
12pub(crate) use visibility::*;
13
14pub(crate) use proc_macro::TokenStream as OldTokenStream;
15pub(crate) use proc_macro2::{TokenStream as NewTokenStream, TokenTree as NewTokenTree};
16pub(crate) use quote::{ToTokens, format_ident, quote};
17pub(crate) use std::collections::HashMap;
18pub(crate) use std::str::FromStr;
19pub(crate) use syn::{
20    Data, DeriveInput, Field,
21    GenericParam::{self},
22    Ident, Lifetime,
23    Type::{self},
24    TypeParam, parse_macro_input,
25};
26
27/// This is an example of how to use the `Lombok` procedural macro with `get` attributes.
28///
29/// The `Lombok` procedural macro is used to automatically generate getter methods for struct fields.
30/// The `get` attribute controls the visibility of the generated getter method.
31///
32/// Example:
33///
34/// ```rust
35/// use lombok_macros::*;
36///
37/// #[derive(Getter, Clone)]
38/// struct LombokTest2<'a, 'b, T: Clone> {
39///     #[get(pub(crate))]
40///     list: Vec<String>,
41///     #[get(pub(crate))]
42///     opt_str_lifetime_a: Option<&'a T>,
43///     opt_str_lifetime_b: Option<&'b str>,
44/// }
45/// let list: Vec<String> = vec!["hello".to_string(), "world".to_string()];
46/// let data2: LombokTest2<usize> = LombokTest2 {
47///     list: list.clone(),
48///     opt_str_lifetime_a: None,
49///     opt_str_lifetime_b: None,
50/// };
51/// let get_list: Vec<String> = data2.get_list().clone();
52/// assert_eq!(get_list, list);
53/// ```
54#[proc_macro_derive(Getter, attributes(get))]
55pub fn getter(input: TokenStream) -> TokenStream {
56    inner_lombok_data(input, true, false, false)
57}
58
59/// This is an example of how to use the `Lombok` procedural macro with `get_mut` attributes.
60///
61/// The `Lombok` procedural macro is used to automatically generate mutable getters for struct fields.
62/// The `get_mut` attribute controls the visibility of the mutable getter function.
63///
64/// Example:
65///
66/// ```rust
67/// use lombok_macros::*;
68///
69/// #[derive(GetterMut, Clone)]
70/// struct LombokTest2<'a, 'b, T: Clone> {
71///     #[get_mut(pub(crate))]
72///     list: Vec<String>,
73///     #[get_mut(pub(crate))]
74///     opt_str_lifetime_a: Option<&'a T>,
75///     opt_str_lifetime_b: Option<&'b str>,
76/// }
77/// let list: Vec<String> = vec!["hello".to_string(), "world".to_string()];
78/// let mut data2: LombokTest2<usize> = LombokTest2 {
79///     list: list.clone(),
80///     opt_str_lifetime_a: None,
81///     opt_str_lifetime_b: None,
82/// };
83/// let get_list: Vec<String> = data2.get_mut_list().clone();
84/// assert_eq!(get_list, list);
85/// ```
86#[proc_macro_derive(GetterMut, attributes(get_mut))]
87pub fn getter_mut(input: TokenStream) -> TokenStream {
88    inner_lombok_data(input, false, true, false)
89}
90
91/// This is an example of how to use the `Lombok` procedural macro with `set` attributes.
92///
93/// The `Lombok` procedural macro is used to automatically generate setters for struct fields.
94/// The `set` attribute controls the visibility of the setter function.
95///
96/// Example:
97///
98/// ```rust
99/// use lombok_macros::*;
100///
101/// #[derive(Setter, Debug, Clone)]
102/// struct LombokTest<'a, 'b, T: Clone> {
103///     #[set(pub(crate))]
104///     list: Vec<String>,
105///     opt_str_lifetime_a: Option<&'a T>,
106///     #[set(private)]
107///     opt_str_lifetime_b: Option<&'b str>,
108/// }
109/// let mut data: LombokTest<usize> = LombokTest {
110///     list: Vec::new(),
111///     opt_str_lifetime_a: None,
112///     opt_str_lifetime_b: None,
113/// };
114/// let list: Vec<String> = vec!["hello".to_string(), "world".to_string()];
115/// data.set_list(list.clone());
116/// match data.list {
117///     left_val => {
118///         assert_eq!(*left_val, list);
119///     }
120/// }
121/// ```
122#[proc_macro_derive(Setter, attributes(set))]
123pub fn setter(input: TokenStream) -> TokenStream {
124    inner_lombok_data(input, false, false, true)
125}
126
127/// This is an example of how to use the `Lombok` procedural macro with `get`, `get_mut`, and `set` attributes.
128///
129/// The `Lombok` procedural macro is used to automatically generate getters, mutable getters, and setters for struct fields.
130/// The `get` and `get_mut` attributes control the visibility of the getter functions, while the `set` attribute controls
131/// the visibility of the setter function.
132///
133/// Example:
134///
135/// ```rust
136/// use lombok_macros::*;
137///
138/// #[derive(Data, Debug, Clone)]
139/// struct LombokTest<'a, 'b, T: Clone> {
140///     #[get(pub(crate))]
141///     #[set(pub(crate))]
142///     list: Vec<String>,
143///     #[get(pub(crate))]
144///     opt_str_lifetime_a: Option<&'a T>,
145///     #[set(private)]
146///     opt_str_lifetime_b: Option<&'b str>,
147/// }
148/// let mut data: LombokTest<usize> = LombokTest {
149///     list: Vec::new(),
150///     opt_str_lifetime_a: None,
151///     opt_str_lifetime_b: None,
152/// };
153/// let list: Vec<String> = vec!["hello".to_string(), "world".to_string()];
154/// data.set_list(list.clone());
155/// match data.get_list() {
156///     left_val => {
157///         assert_eq!(*left_val, list);
158///     }
159/// }
160/// ```
161#[proc_macro_derive(Data, attributes(set, get, get_mut))]
162pub fn data(input: TokenStream) -> TokenStream {
163    inner_lombok_data(input, true, true, true)
164}
165
166/// A procedural macro that implements the `std::fmt::Display` trait for a type,
167/// using the standard debug format (`{:?}`) for formatting.
168///
169/// This macro derives the `Display` implementation for a type, allowing it to be formatted
170/// using `{:?}` in formatting macros. It uses the `inner_display_debug` function to generate
171/// the implementation with the standard debug format.
172///
173/// # Parameters
174/// - `input`: The input token stream representing the Rust item (struct, enum, etc.)
175///   for which the `Display` implementation will be generated.
176///
177/// # Returns
178/// - `TokenStream`: The generated `std::fmt::Display` implementation for the type
179///   using the standard debug format.
180#[proc_macro_derive(DisplayDebug)]
181pub fn display_debug(input: TokenStream) -> TokenStream {
182    inner_display_debug(input)
183}
184
185/// A procedural macro that implements the `std::fmt::Display` trait for a type,
186/// using the detailed debug format (`{:#?}`) for formatting.
187///
188/// This macro derives the `Display` implementation for a type, allowing it to be formatted
189/// using `{:#?}` in formatting macros. It uses the `inner_display_debug_format` function
190/// to generate the implementation with the detailed debug format.
191///
192/// # Parameters
193/// - `input`: The input token stream representing the Rust item (struct, enum, etc.)
194///   for which the `Display` implementation will be generated.
195///
196/// # Returns
197/// - `TokenStream`: The generated `std::fmt::Display` implementation for the type
198///   using the detailed debug format.
199#[proc_macro_derive(DisplayDebugFormat)]
200pub fn display_debug_format(input: TokenStream) -> TokenStream {
201    inner_display_debug_format(input)
202}