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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use crate::config::UserConfig;
use log::debug;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro_error::{abort, ResultExt};
use quote::quote;
use single::Single;
use syn::{
spanned::Spanned, AngleBracketedGenericArguments, DataStruct, Field, GenericArgument, Ident,
PathArguments, PathSegment, Type, TypePath,
};
/// A description of how this field should be handled when generating `new`
#[derive(Debug, Clone)]
pub struct FieldConfig {
/// Argument type in `new`
input_type: Type,
/// Argument name in `new`
input_name: Ident,
/// Name of this field in the struct.
/// None for tuple structs
struct_name: Option<Ident>,
/// Transform to apply in body of `new`
transform: TokenStream2,
}
impl FieldConfig {
/// Argument to `new`
pub fn input(&self) -> TokenStream2 {
let input_name = self.input_name.clone();
let input_type = self.input_type.clone();
quote!(#input_name: #input_type)
}
/// Body inside `new`
pub fn transform(&self) -> TokenStream2 {
let input_name = self.input_name.clone();
let transform = self.transform.clone();
quote!(let #input_name = (#transform)(#input_name))
}
/// Argument to constructor
pub fn output(&self) -> TokenStream2 {
let input_name = self.input_name.clone();
match self.struct_name.clone() {
Some(struct_name) => quote!( #struct_name: #input_name ),
None => quote!(#input_name),
}
}
}
fn magic_field_config(field: Field, input_name: Ident) -> Option<FieldConfig> {
match field.ty {
Type::Path(TypePath {
qself: None,
path:
syn::Path {
leading_colon: None,
segments,
},
}) => match segments.into_iter().collect::<Vec<_>>().as_slice() {
// String -> impl AsRef<str>
[PathSegment {
ident,
arguments: PathArguments::None,
}] if ident.to_string() == "String" => Some(FieldConfig {
input_type: syn::parse2(quote!(impl ::std::convert::AsRef<::std::primitive::str>))
.unwrap(),
input_name,
struct_name: field.ident,
transform: quote!(|s| ::std::string::String::from(::std::convert::AsRef::<
::std::primitive::str,
>::as_ref(&s))),
}),
// Vec<T> -> impl IntoIterator<Item = ...>
[PathSegment {
ident,
arguments:
PathArguments::AngleBracketed(AngleBracketedGenericArguments { args, .. }),
}] if ident.to_string() == "Vec" => {
match args.into_iter().collect::<Vec<_>>().as_slice() {
// Vec<String> -> impl IntoIterator<Item = impl AsRef<str>>
[GenericArgument::Type(Type::Path(TypePath {
qself: None,
path:
syn::Path {
leading_colon: None,
segments,
},
}))] if simple_segment(segments, "String") => Some(FieldConfig {
input_type: syn::parse2(quote!(
impl ::std::iter::IntoIterator<Item = impl AsRef<::std::primitive::str>>
))
.unwrap(),
input_name,
struct_name: field.ident,
transform: quote!(|i| {
let mut v = std::vec::Vec::new();
for item in i {
v.push(::std::string::String::from(::std::convert::AsRef::<
::std::primitive::str,
>::as_ref(
&item
)))
}
v
}),
}),
// Vec<PathBuf> -> impl IntoIterator<Item = impl AsRef<Path>>
[GenericArgument::Type(Type::Path(TypePath {
qself: None,
path:
syn::Path {
leading_colon: None,
segments,
},
}))] if simple_segment(segments, "PathBuf") => Some(FieldConfig {
input_type: syn::parse2(quote!(
impl ::std::iter::IntoIterator<Item = impl AsRef<::std::path::Path>>
))
.unwrap(),
input_name,
struct_name: field.ident,
transform: quote!(|i| {
let mut v = std::vec::Vec::new();
for item in i {
v.push(::std::path::PathBuf::from(::std::convert::AsRef::<
::std::path::Path,
>::as_ref(
&item
)))
}
v
}),
}),
// Vec<T> -> impl IntoIterator<Item = T>
[GenericArgument::Type(ty)] => Some(FieldConfig {
input_type: syn::parse2(quote!(impl ::std::iter::IntoIterator<Item = #ty>))
.unwrap(),
input_name,
struct_name: field.ident,
transform: quote!(|i| {
let mut v = std::vec::Vec::new();
for item in i {
v.push(item)
}
v
}),
}),
_ => None,
}
}
// PathBuf -> impl AsRef<Path>
[PathSegment { ident, .. }] if ident.to_string() == "PathBuf" => Some(FieldConfig {
input_type: syn::parse2(quote!(impl ::std::convert::AsRef<::std::path::Path>))
.unwrap(),
input_name,
struct_name: field.ident,
transform: quote!(|s| ::std::path::PathBuf::from(::std::convert::AsRef::<
::std::path::Path,
>::as_ref(&s))),
}),
_ => None,
},
_ => None,
}
}
fn simple_segment<'a>(
segments: impl IntoIterator<Item = &'a PathSegment>,
expected: impl AsRef<str>,
) -> bool {
match segments.into_iter().collect::<Vec<_>>().as_slice() {
[PathSegment {
ident,
arguments: PathArguments::None,
}] if ident.to_string() == expected.as_ref() => true,
_ => false,
}
}
pub fn make_field_configs(data_struct: &DataStruct) -> Vec<FieldConfig> {
data_struct
.fields
.clone()
.into_iter()
.enumerate()
.map(|(n, field)| {
// Get the #[generic_new(...)], if there is one
let generic_new_attribute = match field
.attrs
.iter()
.filter(|attr| {
attr.path
.segments
.first()
.map(|segment| segment.ident.to_string().as_str() == "generic_new")
.unwrap_or(false)
})
.single()
{
Ok(a) => Some(a),
Err(e) => match e {
single::Error::NoElements => None,
single::Error::MultipleElements => {
abort!(field.span(), "Can't specify `generic_new` more than once")
}
},
};
debug!("{generic_new_attribute:?}");
// Turn it into UserConfig
let user_config = generic_new_attribute.map(|attribute| {
attribute
.parse_args::<UserConfig>()
.expect_or_abort("Couldn't parse attributes")
});
let span = field.span();
let struct_name = field.clone().ident;
let input_name = field
.clone()
.ident
.unwrap_or_else(|| Ident::new(&format!("arg{n}"), span));
let noop_config = FieldConfig {
input_type: field.clone().ty,
input_name: input_name.clone(),
struct_name: struct_name.clone(),
transform: quote!(|i| i),
};
match user_config {
// User has explicitly asked us to ignore this type, so leave as-is
Some(UserConfig::Ignore) => noop_config,
// User has provided their own conversion
Some(UserConfig::Custom(ty, conv)) => FieldConfig {
input_type: ty,
input_name,
struct_name,
transform: quote!(#conv),
},
None => magic_field_config(field, input_name).unwrap_or(noop_config),
}
})
.collect()
}