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
use core::fmt;
use std::collections::HashMap;
use syn::parse::{Parse, ParseStream};
use syn::{Meta, NestedMeta, Token};
use crate::error::Error;
use crate::parser::{parse_enable_disable, parse_shorthand};
use crate::utils::PathExt;
#[derive(Clone, PartialEq, Eq)]
pub struct Forward {
default_state: bool,
fields: HashMap<syn::Path, bool>,
}
impl Default for Forward {
fn default() -> Self {
Self {
default_state: false,
fields: {
let mut result = HashMap::new();
// whitelist of built-in attributes, that will always be forwarded:
// (except, if you disable them)
result.insert(syn::parse_str("doc").unwrap(), true);
result.insert(syn::parse_str("cfg").unwrap(), true);
result.insert(syn::parse_str("cfg_attr").unwrap(), true);
result.insert(syn::parse_str("allow").unwrap(), true);
result.insert(syn::parse_str("warn").unwrap(), true);
result.insert(syn::parse_str("deny").unwrap(), true);
result.insert(syn::parse_str("forbid").unwrap(), true);
result.insert(syn::parse_str("deprecated").unwrap(), true);
result.insert(syn::parse_str("inline").unwrap(), true);
result.insert(syn::parse_str("cold").unwrap(), true);
result.insert(syn::parse_str("target_feature").unwrap(), true);
result
},
}
}
}
impl fmt::Debug for Forward {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// it is very hard to read the debug implementation, if the entire structure
// of `Path` is shown, so it is converted to a `String`.
f.debug_struct("Forward")
.field("default_state", &self.default_state)
.field(
"fields",
&self
.fields
.iter()
.map(|(path, state)| (path.to_string(), state))
.collect::<HashMap<_, _>>(),
)
.finish()
}
}
impl Parse for Forward {
fn parse(input: ParseStream) -> syn::Result<Self> {
let input = parse_shorthand(input)?;
let (state, input) = parse_enable_disable(&input)?;
for nested in input.parse_terminated::<_, Token![,]>(syn::NestedMeta::parse)? {
match nested {
NestedMeta::Meta(meta) => {
if !meta.path().is_ident("forward") {
continue;
}
match meta {
// enable(forward) -> everything will be forwarded, by default
// disable(forward) -> nothing will be forwarded, by default
Meta::Path(_) => {
return Ok(Self {
default_state: state,
fields: HashMap::new(),
});
}
// #[shorthand(enable(forward(x, y, z)))]
Meta::List(list) => {
return Ok(Self {
default_state: false,
fields: {
let iterator = list
.nested
.into_iter()
.map(|v| {
match v {
// enable(forward(x))
NestedMeta::Meta(meta) => {
if let Meta::Path(p) = meta {
Ok((p, state))
} else {
Err(Error::unexpected_meta(&meta)
.with_alts(&["Path"]))
}
}
// enable(forward("x"))
NestedMeta::Lit(lit) => {
Err(Error::unexpected_lit(&lit))
}
}
})
.collect::<Vec<Result<_, _>>>();
if iterator.iter().any(Result::is_err) {
Err::<_, syn::Error>(
Error::multiple(
iterator.into_iter().filter_map(Result::err),
)
.into(),
)
} else {
Ok(iterator.into_iter().filter_map(Result::ok).collect())
}
}?,
});
}
// #[shorthand(enable(forward(x = "")))]
Meta::NameValue(_) => {
return Err(Error::unexpected_meta(&meta)
.with_alts(&["Path", "List"])
.into());
}
}
}
// #[shorthand(enable(""))]
NestedMeta::Lit(lit) => {
return Err(Error::unexpected_lit(&lit).into());
}
}
}
// this is unreachable, because it is checked with Forward::is_forward, that the
// input is a valid `Forward` attribute, before it is passed to this
// function via `syn::parse`
unreachable!("failed to parse `TokenStream`")
}
}
impl Forward {
// parses something like this:
//
// #[shorthand(enable(forward, x))]
// #[shorthand(disable(forward, x))]
pub fn is_forward(meta: &Meta) -> bool {
if let Meta::List(list) = meta {
for part in &list.nested {
if let NestedMeta::Meta(meta) = part {
if let Meta::List(list) = meta {
for part in &list.nested {
if let NestedMeta::Meta(meta) = part {
return meta.path().is_ident("forward");
}
}
}
}
}
}
false
}
pub fn update(&mut self, other: Self) -> &mut Self {
for (key, value) in other.fields {
if let Some(v) = self.fields.get_mut(&key) {
*v = value;
} else {
self.fields.insert(key, value);
}
}
self
}
pub fn is(&self, input: &str) -> bool {
let result = self.default_state;
for (key, value) in &self.fields {
if key.is_ident(input) {
return *value;
}
}
result
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::AttributeExt;
#[test]
fn parse_forward() {
let _: Forward = syn::parse_str("#[shorthand(enable(forward))]").unwrap();
let _: Forward = syn::parse_str("#[shorthand(disable(forward))]").unwrap();
let _: Forward = syn::parse_str("#[shorthand(enable(forward(x, y, z)))]").unwrap();
let _: Forward = syn::parse_str("#[shorthand(disable(forward(x, y, z)))]").unwrap();
}
#[test]
fn is_forward() {
let valid_attributes = [
"#[shorthand(enable(forward))]",
"#[shorthand(disable(forward))]",
"#[shorthand(enable(forward(x, y, z)))]",
"#[shorthand(disable(forward(x, y, z)))]",
];
for valid in &valid_attributes {
assert!(Forward::is_forward(
&syn::Attribute::from_str(valid)
.unwrap()
.parse_meta()
.unwrap()
));
}
let invalid_attributes = [
"#[shorthand(enable(forward_))]",
"#[shorthand(disable(_forward))]",
"#[shorthand(enable(xaforward(x, y, z)))]",
"#[shorthand(forward(x, y, z))]",
"#[shorthand(forward_everything)]",
];
for invalid in &invalid_attributes {
assert!(!Forward::is_forward(
&syn::Attribute::from_str(invalid)
.unwrap()
.parse_meta()
.unwrap()
));
}
}
}