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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Definition of macros for Confetti-rs
extern crate proc_macro;
use proc_macro::{TokenStream, TokenTree};
use quote::{quote, format_ident};
use syn::{parse_macro_input, DeriveInput, Data, Fields, Lit, Meta, NestedMeta, Attribute};
use syn::spanned::Spanned;
/// Derives the FromConf and ToConf traits for struct types
#[proc_macro_derive(ConfMap, attributes(conf_map))]
pub fn derive_conf_map(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let name_str = name.to_string();
let (impl_from_conf, impl_to_conf) = match &input.data {
Data::Struct(data_struct) => {
match &data_struct.fields {
Fields::Named(fields_named) => {
let from_conf_fields = fields_named.named.iter().map(|field| {
let field_name = field.ident.as_ref().unwrap();
let field_name_str = field_name.to_string();
let field_type = &field.ty;
// Check for conf_map attributes
let conf_name = get_conf_name_from_attrs(&field.attrs, &field_name_str);
let is_optional = is_option_type(&field_type);
let field_extraction = if is_optional {
quote! {
#field_name: {
if let Some(child) = directive.children.iter().find(|d| d.name.value == #conf_name) {
if !child.arguments.is_empty() {
Some(crate::mapper::ValueConverter::from_conf_value(&child.arguments[0].value)?)
} else {
None
}
} else {
None
}
}
}
} else {
quote! {
#field_name: {
if let Some(child) = directive.children.iter().find(|d| d.name.value == #conf_name) {
if !child.arguments.is_empty() {
crate::mapper::ValueConverter::from_conf_value(&child.arguments[0].value)?
} else {
return Err(crate::mapper::MapperError::MissingField(#conf_name.to_string()));
}
} else {
return Err(crate::mapper::MapperError::MissingField(#conf_name.to_string()));
}
}
}
};
field_extraction
});
let to_conf_fields = fields_named.named.iter().map(|field| {
let field_name = field.ident.as_ref().unwrap();
let field_name_str = field_name.to_string();
// Check for conf_map attributes
let conf_name = get_conf_name_from_attrs(&field.attrs, &field_name_str);
let is_optional = is_option_type(&field.ty);
if is_optional {
quote! {
if let Some(value) = &self.#field_name {
let arg_value = crate::mapper::ValueConverter::to_conf_value(value)?;
let arg = crate::ConfArgument {
value: arg_value,
span: 0..0,
is_quoted: true,
is_triple_quoted: false,
is_expression: false,
};
let child = crate::ConfDirective {
name: crate::ConfArgument {
value: #conf_name.to_string(),
span: 0..0,
is_quoted: false,
is_triple_quoted: false,
is_expression: false,
},
arguments: vec![arg],
children: vec![],
};
children.push(child);
}
}
} else {
quote! {
let arg_value = crate::mapper::ValueConverter::to_conf_value(&self.#field_name)?;
let arg = crate::ConfArgument {
value: arg_value,
span: 0..0,
is_quoted: true,
is_triple_quoted: false,
is_expression: false,
};
let child = crate::ConfDirective {
name: crate::ConfArgument {
value: #conf_name.to_string(),
span: 0..0,
is_quoted: false,
is_triple_quoted: false,
is_expression: false,
},
arguments: vec![arg],
children: vec![],
};
children.push(child);
}
}
});
let from_impl = quote! {
impl crate::mapper::FromConf for #name {
fn from_directive(directive: &crate::ConfDirective) -> Result<Self, crate::mapper::MapperError> {
if directive.name.value != #name_str {
return Err(crate::mapper::MapperError::ParseError(
format!("Expected directive name {}, found {}", #name_str, directive.name.value)
));
}
Ok(Self {
#(#from_conf_fields),*
})
}
}
};
let to_impl = quote! {
impl crate::mapper::ToConf for #name {
fn to_directive(&self) -> Result<crate::ConfDirective, crate::mapper::MapperError> {
let mut children = Vec::new();
#(#to_conf_fields)*
Ok(crate::ConfDirective {
name: crate::ConfArgument {
value: #name_str.to_string(),
span: 0..0,
is_quoted: false,
is_triple_quoted: false,
is_expression: false,
},
arguments: vec![],
children,
})
}
}
};
(from_impl, to_impl)
},
_ => {
// Only supports named fields
return syn::Error::new(
data_struct.fields.span(),
"ConfMap can only be derived for structs with named fields"
).to_compile_error().into();
}
}
},
_ => {
// Only supports structs
return syn::Error::new(
input.span(),
"ConfMap can only be derived for structs"
).to_compile_error().into();
}
};
let expanded = quote! {
#impl_from_conf
#impl_to_conf
};
expanded.into()
}
// Helper functions
fn get_conf_name_from_attrs(attrs: &[Attribute], default_name: &str) -> String {
for attr in attrs {
if attr.path.is_ident("conf_map") {
if let Ok(meta) = attr.parse_meta() {
if let Meta::List(meta_list) = meta {
for nested_meta in meta_list.nested.iter() {
if let NestedMeta::Meta(Meta::NameValue(name_value)) = nested_meta {
if name_value.path.is_ident("name") {
if let Lit::Str(lit_str) = &name_value.lit {
return lit_str.value();
}
}
}
}
}
}
}
}
// Return the field name as default
default_name.to_string()
}
fn is_option_type(ty: &syn::Type) -> bool {
if let syn::Type::Path(type_path) = ty {
if let Some(segment) = type_path.path.segments.last() {
return segment.ident == "Option";
}
}
false
}