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` and `set` attributes.
28///
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
31/// the visibility of the setter function.
32///
33/// Example:
34///
35/// ```rust
36/// use lombok_macros::*;
37///
38/// #[derive(Lombok, Debug, Clone)]
39/// struct LombokTest<'a, 'b, T: Clone> {
40/// #[get(pub(crate))]
41/// #[set(pub(crate))]
42/// list: Vec<String>,
43/// #[get(pub(crate))]
44/// opt_str_lifetime_a: Option<&'a T>,
45/// #[set(private)]
46/// opt_str_lifetime_b: Option<&'b str>,
47/// }
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/// }
61/// }
62/// }
63/// ```
64#[proc_macro_derive(Lombok, attributes(set, get, get_mut))]
65pub fn lombok_data(input: TokenStream) -> TokenStream {
66 inner_lombok_data(input)
67}
68
69/// A procedural macro that implements the `std::fmt::Display` trait for a type,
70/// using the standard debug format (`{:?}`) for formatting.
71///
72/// This macro derives the `Display` implementation for a type, allowing it to be formatted
73/// using `{:?}` in formatting macros. It uses the `inner_display_debug` function to generate
74/// the implementation with the standard debug format.
75///
76/// # Parameters
77/// - `input`: The input token stream representing the Rust item (struct, enum, etc.)
78/// for which the `Display` implementation will be generated.
79///
80/// # Returns
81/// - `TokenStream`: The generated `std::fmt::Display` implementation for the type
82/// using the standard debug format.
83#[proc_macro_derive(DisplayDebug)]
84pub fn display_debug(input: TokenStream) -> TokenStream {
85 inner_display_debug(input)
86}
87
88/// A procedural macro that implements the `std::fmt::Display` trait for a type,
89/// using the detailed debug format (`{:#?}`) for formatting.
90///
91/// This macro derives the `Display` implementation for a type, allowing it to be formatted
92/// using `{:#?}` in formatting macros. It uses the `inner_display_debug_format` function
93/// to generate the implementation with the detailed debug format.
94///
95/// # Parameters
96/// - `input`: The input token stream representing the Rust item (struct, enum, etc.)
97/// for which the `Display` implementation will be generated.
98///
99/// # Returns
100/// - `TokenStream`: The generated `std::fmt::Display` implementation for the type
101/// using the detailed debug format.
102#[proc_macro_derive(DisplayDebugFormat)]
103pub fn display_debug_format(input: TokenStream) -> TokenStream {
104 inner_display_debug_format(input)
105}