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
use std::fmt::Display;
use proc_macro2::Span;
use syn::{Attribute, Ident, Meta, MetaList, NestedMeta};
pub fn nested_meta_to_ident(nested_meta: &NestedMeta) -> Option<Ident> {
match nested_meta {
NestedMeta::Meta(meta) => Some(meta.name()),
NestedMeta::Literal(..) => None,
}
}
pub fn meta_list_contains(meta_list: &MetaList, operand: &NestedMeta) -> bool {
meta_list
.nested
.iter()
.any(|nested_meta| nested_meta == operand)
}
pub fn ident_concat(left: &str, right: &str) -> Ident {
let mut combined = String::with_capacity(left.len() + right.len());
combined.push_str(left);
combined.push_str(right);
Ident::new(&combined, Span::call_site())
}
#[allow(clippy::let_and_return)]
pub fn tag_parameter<NS, Tag>(attrs: &[Attribute], namespace: NS, tag: Tag) -> Option<Meta>
where
NS: Display,
Tag: Display,
Ident: PartialEq<NS>,
Ident: PartialEq<Tag>,
{
let error_message = format!(
"Expected exactly one identifier for `#[{}({}(..))]`.",
&namespace, &tag,
);
let namespace_meta_lists = namespace_meta_lists(attrs, namespace);
let meta_param = tag_meta_list(&namespace_meta_lists, tag)
.map(|meta_list| {
if meta_list.nested.len() != 1 {
panic!("{}. `{:?}`", &error_message, &meta_list.nested);
}
meta_list
.nested
.first()
.map(|pair| {
let nested_meta = pair.value();
if let NestedMeta::Meta(meta) = nested_meta {
meta.clone()
} else {
panic!(
"`{:?}` is an invalid value in this position.\n\
Expected a single identifier.",
nested_meta,
);
}
})
.expect("Expected one meta item to exist.")
})
.next();
meta_param
}
pub fn tag_parameters<NS, Tag>(attrs: &[Attribute], namespace: NS, tag: Tag) -> Vec<Meta>
where
Ident: PartialEq<NS>,
Ident: PartialEq<Tag>,
{
let namespace_meta_lists = namespace_meta_lists(attrs, namespace);
let parameters = tag_meta_list(&namespace_meta_lists, tag)
.flat_map(|meta_list| meta_list.nested.iter())
.filter_map(|nested_meta| {
if let NestedMeta::Meta(meta) = nested_meta {
Some(meta.clone())
} else {
None
}
})
.collect::<Vec<Meta>>();
parameters
}
fn namespace_meta_lists<NS>(attrs: &[Attribute], namespace: NS) -> Vec<MetaList>
where
Ident: PartialEq<NS>,
{
attrs
.iter()
.map(Attribute::parse_meta)
.filter_map(Result::ok)
.filter(|meta| meta.name() == namespace)
.filter_map(|meta| {
if let Meta::List(meta_list) = meta {
Some(meta_list)
} else {
None
}
})
.collect::<Vec<MetaList>>()
}
fn tag_meta_list<'f, Tag>(
namespace_meta_lists: &'f [MetaList],
tag: Tag,
) -> impl Iterator<Item = &'f MetaList> + 'f
where
Tag: 'f,
Ident: PartialEq<Tag>,
{
namespace_meta_lists
.iter()
.flat_map(|meta_list| meta_list.nested.iter())
.filter_map(|nested_meta| {
if let NestedMeta::Meta(meta) = nested_meta {
Some(meta)
} else {
None
}
})
.filter(move |meta| meta.name() == tag)
.filter_map(|meta| {
if let Meta::List(meta_list) = meta {
Some(meta_list)
} else {
None
}
})
}