Skip to content

Commit 880116e

Browse files
committed
feat: v1.11.0
1 parent 21afac9 commit 880116e

File tree

7 files changed

+210
-41
lines changed

7 files changed

+210
-41
lines changed

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
/target
2-
/test-code
3-
Cargo.lock
1+
**/target
2+
Cargo.lock

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lombok-macros"
3-
version = "1.10.1"
3+
version = "1.11.0"
44
edition = "2024"
55
authors = ["ltpp-universe <root@ltpp.vip>"]
66
license = "MIT"
@@ -12,13 +12,14 @@ exclude = [
1212
"target",
1313
"Cargo.lock",
1414
"sh",
15-
".github"
15+
".github",
16+
"debug"
1617
]
1718

1819
[dependencies]
19-
proc-macro2 = "1.0.94"
20+
proc-macro2 = "1.0.95"
2021
quote = "1.0.40"
21-
syn = "2.0.100"
22+
syn = "2.0.101"
2223

2324
[lib]
2425
proc-macro = true

debug/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "debug"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
lombok-macros = { path = "../" }

debug/src/main.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use lombok_macros::*;
2+
use std::fmt::Debug;
3+
4+
#[derive(Data, Debug, Clone, DisplayDebugFormat)]
5+
struct LombokTest<'a, 'b, T: Clone + Debug> {
6+
#[get(pub(crate))]
7+
#[set(pub(crate))]
8+
list: Vec<String>,
9+
#[get(pub(crate))]
10+
opt_str_lifetime_a: Option<&'a T>,
11+
#[set(private)]
12+
#[get_mut(pub(crate))]
13+
opt_str_lifetime_b: Option<&'b str>,
14+
}
15+
16+
fn main() {
17+
let mut data: LombokTest<usize> = LombokTest {
18+
list: Vec::new(),
19+
opt_str_lifetime_a: None,
20+
opt_str_lifetime_b: None,
21+
};
22+
let list: Vec<String> = vec!["hello".to_string(), "world".to_string()];
23+
data.set_list(list.clone());
24+
match data.get_list() {
25+
left_val => {
26+
assert_eq!(*left_val, list);
27+
}
28+
}
29+
let get_opt_str_lifetime_a: Option<usize> = data.get_opt_str_lifetime_a().cloned();
30+
assert_eq!(get_opt_str_lifetime_a, None);
31+
let get_mut_opt_str_lifetime_b: &mut Option<&str> = data.get_mut_opt_str_lifetime_b();
32+
*get_mut_opt_str_lifetime_b = Some("opt_str_lifetime_b");
33+
assert_eq!(
34+
data.get_mut_opt_str_lifetime_b().clone(),
35+
Some("opt_str_lifetime_b")
36+
);
37+
println!("{}", data.to_string());
38+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ fn main() {
227227
use lombok_macros::*;
228228
use std::fmt::Debug;
229229

230-
#[derive(Lombok, Debug, Clone, DisplayDebugFormat)]
230+
#[derive(Data, Debug, Clone, DisplayDebugFormat)]
231231
struct LombokTest<'a, 'b, T: Clone + Debug> {
232232
#[get(pub(crate))]
233233
#[set(pub(crate))]

src/generate/fn.rs

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,17 @@ use crate::*;
44
///
55
/// # Parameters
66
/// - `field`: A reference to the `Field` structure representing the field for which the getter and setter will be generated.
7+
/// - `need_getter`: A boolean indicating whether a getter function should be generated.
8+
/// - `need_setter`: A boolean indicating whether a setter function should be generated.
79
///
810
/// # Returns
911
/// - A `NewTokenStream` containing the generated getter and setter functions.
10-
pub fn generate_getter_setter(field: &Field) -> NewTokenStream {
12+
pub fn generate_getter_setter(
13+
field: &Field,
14+
need_getter: bool,
15+
need_getter_mut: bool,
16+
need_setter: bool,
17+
) -> NewTokenStream {
1118
let attr_name: String = field
1219
.ident
1320
.as_ref()
@@ -26,25 +33,37 @@ pub fn generate_getter_setter(field: &Field) -> NewTokenStream {
2633
cfg_map.entry(name).or_insert_with(Vec::new).push(cfg);
2734
}
2835
let get_quote = |vis: NewTokenStream| {
29-
quote! {
30-
#vis fn #get_name(&self) -> &#attr_ty {
31-
&self.#attr_name_ident
36+
if need_getter {
37+
quote! {
38+
#vis fn #get_name(&self) -> &#attr_ty {
39+
&self.#attr_name_ident
40+
}
3241
}
42+
} else {
43+
quote! {}
3344
}
3445
};
3546
let get_mut_quote = |vis: NewTokenStream| {
36-
quote! {
37-
#vis fn #get_mut_name(&mut self) -> &mut #attr_ty {
38-
&mut self.#attr_name_ident
47+
if need_getter_mut {
48+
quote! {
49+
#vis fn #get_mut_name(&mut self) -> &mut #attr_ty {
50+
&mut self.#attr_name_ident
51+
}
3952
}
53+
} else {
54+
quote! {}
4055
}
4156
};
4257
let set_quote = |vis: NewTokenStream| {
43-
quote! {
44-
#vis fn #set_name(&mut self, val: #attr_ty) -> &mut Self {
45-
self.#attr_name_ident = val;
46-
self
58+
if need_setter {
59+
quote! {
60+
#vis fn #set_name(&mut self, val: #attr_ty) -> &mut Self {
61+
self.#attr_name_ident = val;
62+
self
63+
}
4764
}
65+
} else {
66+
quote! {}
4867
}
4968
};
5069
let mut has_add_get: bool = false;
@@ -99,10 +118,18 @@ pub fn generate_getter_setter(field: &Field) -> NewTokenStream {
99118
///
100119
/// # Parameters
101120
/// - `input`: An `OldTokenStream` representing the input tokens to be processed.
121+
/// - `need_getter`: A boolean indicating whether getter functions should be generated.
122+
/// - `need_getter_mut`: A boolean indicating whether mutable getter functions should be generated.
123+
/// - `need_setter`: A boolean indicating whether setter functions should be generated.
102124
///
103125
/// # Returns
104126
/// - An `OldTokenStream` containing the transformed tokens with `Lombok`-style data code.
105-
pub fn inner_lombok_data(input: OldTokenStream) -> OldTokenStream {
127+
pub fn inner_lombok_data(
128+
input: OldTokenStream,
129+
need_getter: bool,
130+
need_getter_mut: bool,
131+
need_setter: bool,
132+
) -> OldTokenStream {
106133
let input: DeriveInput = parse_macro_input!(input as DeriveInput);
107134
let name: &Ident = &input.ident;
108135
let type_bounds: Vec<TypeParam> = input
@@ -145,7 +172,7 @@ pub fn inner_lombok_data(input: OldTokenStream) -> OldTokenStream {
145172
Data::Struct(ref s) => s
146173
.fields
147174
.iter()
148-
.map(generate_getter_setter)
175+
.map(|field| generate_getter_setter(field, need_getter, need_getter_mut, need_setter))
149176
.collect::<Vec<_>>(),
150177
_ => panic!("{}", UNSUPPORTED_LOMBOK_DERIVE),
151178
};

src/lib.rs

Lines changed: 117 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,118 @@ pub(crate) use syn::{
2424
TypeParam, parse_macro_input,
2525
};
2626

27-
/// This is an example of how to use the `Lombok` procedural macro with `get` and `set` attributes.
27+
/// This is an example of how to use the `Lombok` procedural macro with `get` attributes.
2828
///
29-
/// The `Lombok` procedural macro is used to automatically generate getters and setters for struct fields.
30-
/// The `get` attribute controls the visibility of the getter function, and the `set` attribute controls
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))]
55+
pub 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))]
87+
pub 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))]
123+
pub 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
31131
/// the visibility of the setter function.
32132
///
33133
/// Example:
34134
///
35135
/// ```rust
36136
/// use lombok_macros::*;
37137
///
38-
/// #[derive(Lombok, Debug, Clone)]
138+
/// #[derive(Data, Debug, Clone)]
39139
/// struct LombokTest<'a, 'b, T: Clone> {
40140
/// #[get(pub(crate))]
41141
/// #[set(pub(crate))]
@@ -45,25 +145,22 @@ pub(crate) use syn::{
45145
/// #[set(private)]
46146
/// opt_str_lifetime_b: Option<&'b str>,
47147
/// }
48-
///
49-
/// fn main() {
50-
/// let mut data: LombokTest<usize> = LombokTest {
51-
/// list: Vec::new(),
52-
/// opt_str_lifetime_a: None,
53-
/// opt_str_lifetime_b: None,
54-
/// };
55-
/// let list: Vec<String> = vec!["hello".to_string(), "world".to_string()];
56-
/// data.set_list(list.clone());
57-
/// match data.get_list() {
58-
/// left_val => {
59-
/// assert_eq!(*left_val, list);
60-
/// }
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);
61158
/// }
62159
/// }
63160
/// ```
64-
#[proc_macro_derive(Lombok, attributes(set, get, get_mut))]
65-
pub fn lombok_data(input: TokenStream) -> TokenStream {
66-
inner_lombok_data(input)
161+
#[proc_macro_derive(Data, attributes(set, get, get_mut))]
162+
pub fn data(input: TokenStream) -> TokenStream {
163+
inner_lombok_data(input, true, true, true)
67164
}
68165

69166
/// A procedural macro that implements the `std::fmt::Display` trait for a type,

0 commit comments

Comments
 (0)