1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#![doc = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))]
//! See the documentation here [macron documentation](https://docs.rs/macron)
use proc_macro::TokenStream;
use quote::quote;
/// The implementation of trait Display
#[proc_macro_derive(Display, attributes(display))]
pub fn impl_display(input: TokenStream) -> TokenStream {
let syn::DeriveInput { ident, data, attrs, .. } = syn::parse_macro_input!(input as syn::DeriveInput);
match data {
// impl Struct:
syn::Data::Struct(st) => {
// is has attr display:
let output = if let Some(fmt) = read_attr_value(&attrs) {
let args = st.fields
.iter()
.map(|field| field.ident.clone().unwrap())
.filter(|ident| {
let name = ident.to_string();
fmt.contains(&format!("{{{name}}}")) || fmt.contains(&format!("{{{name}:"))
});
quote! { write!(f, #fmt, #(#args = &self.#args,)*) }
}
// no attr display:
else {
quote! { write!(f, stringify!(#ident)) }
};
quote! {
impl ::std::fmt::Display for #ident {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
#output
}
}
}.into()
},
// impl Enum:
syn::Data::Enum(en) => {
// no variants:
if en.variants.is_empty() {
return quote! {
impl ::std::fmt::Display for #ident {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
write!(f, "")
}
}
}.into();
}
// has variants:
let vars = en.variants
.iter()
.map(|syn::Variant { ident: var_ident, fields, attrs, .. }| {
match fields {
// Named { ..:.., }
syn::Fields::Named(_) => {
// has attr:
if let Some(fmt) = read_attr_value(&attrs) {
let args = fields
.iter()
.map(|field| field.ident.clone().unwrap())
.filter(|ident| {
let name = ident.to_string();
fmt.contains(&format!("{{{name}}}"))
|| fmt.contains(&format!("{{{name}:"))
});
quote! { Self::#var_ident { #(#args,)* .. } => write!(f, #fmt) }
}
// no attr:
else {
quote! { Self::#var_ident { .. } => write!(f, stringify!(#var_ident)) }
}
},
// Unnamed(.., .., )
syn::Fields::Unnamed(_) => {
// has attr:
if let Some(mut fmt) = read_attr_value(&attrs) {
let args = (0..fields.len())
.into_iter()
.map(|i| {
let arg = if fmt.contains(&format!("{{{i}}}"))
|| fmt.contains(&format!("{{{i}:")) {
format!("arg{i}")
} else {
fmt.push_str(&format!("{{{i}}}"));
format!("_")
};
proc_macro2::Ident::new(&arg, proc_macro2::Span::call_site())
})
.collect::<Vec<_>>();
let vals = args.clone()
.into_iter()
.map(|arg|
if arg != "_" {
quote! { #arg }
} else {
quote! { "" }
}
);
quote! { Self::#var_ident(#(#args,)*) => write!(f, #fmt, #(#vals,)*) }
}
// no attr:
else {
if fields.len() == 1 {
quote! { Self::#var_ident(arg) => write!(f, "{0}", arg) }
} else {
quote! { Self::#var_ident(..) => write!(f, stringify!(#var_ident)) }
}
}
},
syn::Fields::Unit => {
if let Some(fmt) = read_attr_value(&attrs) {
quote! { Self::#var_ident => write!(f, #fmt) }
} else {
quote! { Self::#var_ident => write!(f, stringify!(#var_ident)) }
}
}
}
});
quote! {
impl ::std::fmt::Display for #ident {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
match &self {
#(
#vars,
)*
}
}
}
}.into()
},
_ => panic!("Expected a 'struct' or 'enum'")
}
}
// Reads an attribute value
fn read_attr_value(attrs: &[syn::Attribute]) -> Option<String>{
attrs
.iter()
.find(|attr| attr.path().is_ident("display"))
.map(|attr|
match &attr.meta {
syn::Meta::NameValue(meta) => {
if let syn::Expr::Lit(lit) = &meta.value {
if let syn::Lit::Str(s) = &lit.lit {
return s.value();
}
}
panic!("Expected the attribute format like this '#[display = \"a text..\"]'");
},
_ => panic!("Expected the attribute format like this '#[display = \"a text..\"]'")
}
)
}