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
use quote::quote;
use syn::{Data, DeriveInput, Generics, Ident, Meta, NestedMeta, Visibility};
use crate::attributes::Attributes;
use crate::error::Error;
use crate::forward::Forward;
use crate::rename::Rename;
use crate::utils::{MetaExt, PathExt};
use crate::visibility::FieldVisibility;
#[derive(Debug, Clone)]
pub(crate) struct Options {
pub ident: Ident,
pub attrs: Vec<syn::Attribute>,
pub vis: Visibility,
pub generics: Generics,
pub data: Data,
forward: Forward,
pub visibility: Visibility,
pub attributes: Attributes,
pub rename: Rename,
is_initial: bool,
}
impl Options {
const FIELDS: [&'static str; 4] = ["enable", "disable", "visibility", "rename"];
fn parse_attributes<T>(mut result: Self, attrs: &T) -> Result<Self, Error>
where
for<'a> &'a T: IntoIterator<Item = &'a syn::Attribute>,
{
// there is a list of errors, so you can see more than one compiler error
// and don't have to recompile the entire codebase, just to see the next
// error...
let mut errors = Vec::new();
// iterate through all attributes
for attr in attrs {
let meta = {
match attr.parse_meta() {
Ok(val) => val,
Err(e) => {
errors.push(Error::syn(e));
continue;
}
}
};
if Forward::is_forward(&meta) {
result.forward.update({
match syn::parse2(quote!(#attr)) {
Ok(val) => val,
Err(e) => {
errors.push(Error::syn(e));
continue;
}
}
});
continue;
}
if let "shorthand" = attr.path.to_string().as_str() {
if let Meta::List(data) = meta {
for item in &data.nested {
if let NestedMeta::Meta(inner) = &item {
// name is for ex. `enable` or `disable`
let name = inner.to_string();
// this flag will check for any unknown fields
let mut unknown = true;
// All known fields are in `Self::FIELDS`, this
// makes it easier to add new fields.
// TODO: remove the loop?
for field in &Self::FIELDS {
if &name == field {
if field == &"enable" || field == &"disable" {
match Attributes::with_meta(result.attributes, field, inner)
{
Ok(val) => {
result.attributes = val;
}
Err(err) => {
errors.push(err);
}
}
} else if field == &"visibility" {
match syn::parse2::<FieldVisibility>(quote!(#attr)) {
Ok(value) => {
result.visibility = value
.into_inner()
.unwrap_or_else(|| result.vis.clone());
}
Err(err) => {
errors.push(Error::syn(err));
}
}
} else if field == &"rename" {
match syn::parse2(quote!(#attr)) {
Ok(attr) => {
result.rename = attr;
}
Err(err) => {
errors.push(Error::syn(err));
}
}
} else {
unreachable!(format!("unhandled field: {}", field));
}
unknown = false;
break;
}
}
// If the field is `unknown` add it to the list of `errors`:
if unknown {
errors.push(
Error::unknown_field(name.as_str())
.with_alts(&Self::FIELDS)
.with_span(&inner),
);
}
}
}
} else {
errors.push(Error::unexpected_meta(&meta).with_alts(&["List"]));
continue;
}
} else {
if result.forward.is(attr.path.to_string().as_str()) && !result.is_initial {
result.attrs.push(attr.clone());
}
continue;
}
}
if !errors.is_empty() {
return Err(Error::multiple(errors));
}
if result.is_initial {
result.is_initial = false;
}
Ok(result)
}
pub fn with_attrs<T>(&self, attrs: &T) -> Result<Self, Error>
where
for<'a> &'a T: IntoIterator<Item = &'a syn::Attribute>,
{
let result = self.clone();
Ok(Self::parse_attributes(result, attrs)?)
}
}
impl Options {
pub fn from_derive_input(input: &DeriveInput) -> Result<Self, Error> {
let result = Self {
ident: input.ident.clone(),
generics: input.generics.clone(),
vis: input.vis.clone(),
attrs: Vec::new(),
data: input.data.clone(),
forward: Forward::default(),
visibility: FieldVisibility::default()
.into_inner()
.unwrap_or_else(|| input.vis.clone()),
attributes: Attributes::default(),
rename: Rename::default(),
is_initial: true,
};
Ok(Self::parse_attributes(result, &input.attrs)?)
}
}