lombokrs_codegen/
lib.rs

1/*
2 * Copyright © 2024 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// lib
18
19// ----------------------------------------------------------------
20
21extern crate proc_macro;
22
23use proc_macro::TokenStream;
24
25use crate::builder::derive_builder;
26use crate::getter::derive_getter;
27use crate::setter::derive_setter;
28
29// ----------------------------------------------------------------
30
31mod builder;
32mod getter;
33mod setter;
34
35// ----------------------------------------------------------------
36
37/// `Setter` is a macro that helps you automatically generate setter methods for structs.
38///
39/// - set_x()
40///   - set_id()
41///   - ...
42///
43/// # Examples
44///
45/// ```rust
46/// use lombokrs_codegen::{Getter, Setter};
47/// // use lombokrs::*;
48///
49/// #[derive(Setter, Getter, Debug)]
50/// pub struct User {
51///     id: u32,
52///     age: u8,
53///     name: String,
54///     email: String,
55///     hobby: Vec<String>,
56/// }
57///
58/// // with lifetime
59/// #[derive(Setter, Getter, Debug)]
60/// pub struct LifetimeUser<'a> {
61///     id: u32,
62///     age: u8,
63///     name: &'a str,
64///     email: &'a str,
65///     hobby: Box<&'a str>,
66/// }
67///
68/// // ----------------------------------------------------------------
69///
70/// impl User {
71///     pub fn new(id: u32, age: u8, name: String, email: String, hobby: Vec<String>) -> Self {
72///         Self {
73///             id,
74///             age,
75///             name,
76///             email,
77///             hobby,
78///         }
79///     }
80/// }
81///
82/// // ----------------------------------------------------------------
83///
84/// // Usage:
85///
86///     let mut user = User::new(
87///         10086,
88///         18,
89///         "photowey".to_string(),
90///         "photowey@gmail.com".to_string(),
91///         vec!["badminton".to_string()],
92///     );
93///
94///     user.set_id(9527);
95///     user.set_age(25);
96///     user.set_name("lombokrs".to_string());
97///     user.set_email("lombokrs@gmail.com".to_string());
98///     user.set_hobby(vec!["football".to_string()]);
99///
100///     assert_eq!(&9527u32, user.get_id());
101///     assert_eq!(&25u8, user.get_age());
102///
103///     assert_eq!("lombokrs", user.get_name());
104///     assert_eq!("lombokrs@gmail.com", user.get_email());
105///     assert_eq!(&vec!["football".to_string()], user.get_hobby());
106///
107/// ```
108#[proc_macro_derive(Setter)]
109pub fn setter_derive(input: TokenStream) -> TokenStream {
110    derive_setter(input)
111}
112
113/// `Getter` is a macro that helps you automatically generate getter methods for structs.
114///
115/// - get_x()
116///   - get_id()
117///   - ...
118/// - x()
119///   - id()
120///   - ...
121///
122/// # Examples
123///
124/// ```rust
125/// use lombokrs_codegen::{Getter, Setter};
126/// // use lombokrs::*;
127///
128/// #[derive(Setter, Getter, Debug)]
129/// pub struct User {
130///     id: u32,
131///     age: u8,
132///     name: String,
133///     email: String,
134///     hobby: Vec<String>,
135/// }
136///
137/// // with lifetime
138/// #[derive(Setter, Getter, Debug)]
139/// pub struct LifetimeUser<'a> {
140///     id: u32,
141///     age: u8,
142///     name: &'a str,
143///     email: &'a str,
144///     hobby: Box<&'a str>,
145/// }
146///
147/// // ----------------------------------------------------------------
148///
149/// impl User {
150///     pub fn new(id: u32, age: u8, name: String, email: String, hobby: Vec<String>) -> Self {
151///         Self {
152///             id,
153///             age,
154///             name,
155///             email,
156///             hobby,
157///         }
158///     }
159/// }
160///
161/// // ----------------------------------------------------------------
162///
163/// // Usage:
164///
165///    let user = User::new(
166///         10086,
167///         18,
168///         "photowey".to_string(),
169///         "photowey@gmail.com".to_string(),
170///         vec!["badminton".to_string()],
171///     );
172///
173///     assert_eq!(&10086u32, user.get_id());
174///     assert_eq!(&18u8, user.get_age());
175///
176///     assert_eq!("photowey", user.get_name());
177///     assert_eq!("photowey@gmail.com", user.get_email());
178///     assert_eq!(&vec!["badminton".to_string()], user.get_hobby());
179/// ```
180#[proc_macro_derive(Getter)]
181pub fn getter_derive(input: TokenStream) -> TokenStream {
182    derive_getter(input)
183}
184
185/// `Getter` is a macro that helps you automatically generate `XxxBuilder` struct and chain methods for structs.
186///
187/// # Examples
188///
189/// ```rust
190/// use lombokrs_codegen::{Builder, Getter, Setter};
191/// // use lombokrs::*;
192///
193/// #[derive(Setter, Getter, Builder, Debug)]
194/// pub struct User {
195///     id: u32,
196///     age: u8,
197///     name: String,
198///     email: String,
199///     hobby: Vec<String>,
200///     // @since 0.2.0
201///     #[builder(method = "activity")]
202///     activities: Vec<String>,
203/// }
204///
205/// // with lifetime
206/// #[derive(Setter, Getter, Builder, Debug)]
207/// pub struct LifetimeUser<'a> {
208///     id: u32,
209///     age: u8,
210///     name: &'a str,
211///     email: &'a str,
212///     hobby: Box<&'a str>,
213/// }
214///
215/// // ----------------------------------------------------------------
216///
217/// // Usage:
218///
219///    let user = User::builder()
220///         .id(10086)
221///         .age(18)
222///         .name("photowey".to_string())
223///         .email("photowey@gmail.com".to_string())
224///         .hobby(vec!["badminton".to_string()])
225///         // @since 0.2.0
226///         .activities(vec!["badminton".to_string()])
227///         // #[builder] function
228///         .activity("badminton".to_string())
229///         .build()
230///         .unwrap();
231///
232///     assert_eq!(&10086u32, user.get_id());
233///     assert_eq!(&18u8, user.get_age());
234///
235///     assert_eq!("photowey", user.get_name());
236///     assert_eq!("photowey@gmail.com", user.get_email());
237///     assert_eq!(&vec!["badminton".to_string()], user.get_hobby());
238///
239/// // ----------------------------------------------------------------
240///
241///     let rvt = User::builder()
242///         //.id(10086)
243///         .age(18)
244///         .name("photowey".to_string())
245///         .email("photowey@gmail.com".to_string())
246///         .hobby(vec!["badminton".to_string()])
247///         // @since 0.2.0
248///         .activities(vec!["badminton".to_string()])
249///         // #[builder] function
250///         .activity("badminton".to_string())
251///         .build();
252///
253///     // Missing field: `id`!
254///     assert!(rvt.is_err())
255/// ```
256#[proc_macro_derive(Builder, attributes(builder))]
257pub fn builder_derive(input: TokenStream) -> TokenStream {
258    derive_builder(input)
259}
260
261/// `Data` is a composite macro that includes [`Setter`], [`Getter`], and [`Builder`].
262#[proc_macro_derive(Data)]
263pub fn data_derive(input: TokenStream) -> TokenStream {
264    TokenStream::from_iter(
265        vec![
266            derive_setter(input.clone()),
267            derive_getter(input.clone()),
268            derive_builder(input.clone()),
269        ]
270        .into_iter(),
271    )
272}