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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
use quote::quote;
use syn::{punctuated::Punctuated, Attribute, Meta, Path, Token};

/// Returns whether an item's attributes contains a given `#[namespace]`
/// attribute.
///
/// # Parameters
///
/// * `attrs`: The attributes on the item.
/// * `namespace`: The `path()` of the first-level attribute.
pub fn contains_namespace(attrs: &[Attribute], namespace: &Path) -> bool {
    attrs.iter().any(|attr| attr.path() == namespace)
}

/// Returns whether an item's attributes contains a given `#[namespace(tag)]`
/// attribute.
///
/// # Parameters
///
/// * `attrs`: The attributes on the item.
/// * `namespace`: The `path()` of the first-level attribute.
/// * `tag`: The `path()` of the second-level attribute.
pub fn contains_tag(attrs: &[Attribute], namespace: &Path, tag: &Path) -> bool {
    attrs
        .iter()
        .filter(|attr| attr.path() == namespace)
        .any(|attr| {
            attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)
                .map(|tags| tags.iter().any(|tag_meta| tag_meta.path() == tag))
                .unwrap_or(false)
        })
}

/// Returns the parameter from `#[namespace(parameter)]`.
///
/// # Parameters
///
/// * `attrs`: Attributes of the item to inspect.
/// * `namespace`: The `path()` of the first-level attribute.
///
/// # Examples
///
/// ```rust,edition2021
/// use proc_macro_roids::namespace_parameter;
/// use syn::{parse_quote, DeriveInput, Meta, Path};
///
/// let ast: DeriveInput = parse_quote! {
///     #[namespace(One)]
///     pub struct MyEnum;
/// };
///
/// let ns: Path = parse_quote!(namespace);
/// let namespace_param = namespace_parameter(&ast.attrs, &ns);
///
/// let meta_one: Path = parse_quote!(One);
/// let param_one = Meta::Path(meta_one);
/// assert_eq!(Some(param_one), namespace_param);
///
/// let ns_other: Path = parse_quote!(namespace_other);
/// let namespace_param_other = namespace_parameter(&ast.attrs, &ns_other);
/// assert_eq!(None, namespace_param_other);
/// ```
///
/// # Panics
///
/// Panics if the number of parameters for the tag is not exactly one.
#[allow(clippy::let_and_return)] // Needed due to bug in clippy.
pub fn namespace_parameter(attrs: &[Attribute], namespace: &Path) -> Option<Meta> {
    let mut namespace_nested_metas_iter = namespace_nested_metas_iter(attrs, namespace);
    let namespace_parameter = namespace_nested_metas_iter.next();
    let namespace_parameter_second = namespace_nested_metas_iter.next();

    if namespace_parameter_second.is_some() {
        panic!(
            "Expected exactly one parameter for `#[{}(..)]`.",
            format_path(namespace),
        );
    }

    namespace_parameter
}

/// Returns the parameters from `#[namespace(param1, param2, ..)]`.
///
/// # Parameters
///
/// * `attrs`: Attributes of the item to inspect.
/// * `namespace`: The `path()` of the first-level attribute.
///
/// # Examples
///
/// ```rust,edition2021
/// use proc_macro_roids::namespace_parameters;
/// use syn::{parse_quote, DeriveInput, Lit, LitStr, Meta, MetaNameValue, Path};
///
/// let ast: DeriveInput = parse_quote! {
///     #[namespace(One, two = "")]
///     #[namespace(three(Value))]
///     pub struct MyEnum;
/// };
///
/// let ns: Path = parse_quote!(namespace);
/// let namespace_parameters = namespace_parameters(&ast.attrs, &ns);
///
/// let meta_one: Path = parse_quote!(One);
/// let param_one = Meta::Path(meta_one);
/// let meta_two: MetaNameValue = parse_quote!(two = "");
/// let param_two = Meta::NameValue(meta_two);
/// let meta_three: LitStr = parse_quote!("three");
/// let param_three = Meta::List(parse_quote!(three(Value)));
/// assert_eq!(
///     vec![param_one, param_two, param_three],
///     namespace_parameters
/// );
/// ```
pub fn namespace_parameters(attrs: &[Attribute], namespace: &Path) -> Vec<Meta> {
    let namespace_nested_metas_iter = namespace_nested_metas_iter(attrs, namespace);

    namespace_nested_metas_iter.collect::<Vec<Meta>>()
}

/// Returns the parameter from `#[namespace(tag(parameter))]`.
///
/// # Parameters
///
/// * `attrs`: Attributes of the item to inspect.
/// * `namespace`: The `path()` of the first-level attribute.
/// * `tag`: The `path()` of the second-level attribute.
///
/// # Examples
///
/// ```rust,edition2021
/// use proc_macro_roids::tag_parameter;
/// use syn::{parse_quote, DeriveInput, Meta, Path};
///
/// let ast: DeriveInput = parse_quote! {
///     #[namespace(tag(One))]
///     pub struct MyEnum;
/// };
///
/// let ns: Path = parse_quote!(namespace);
/// let tag: Path = parse_quote!(tag);
/// let tag_param = tag_parameter(&ast.attrs, &ns, &tag);
///
/// let meta_one: Path = parse_quote!(One);
/// let param_one = Meta::Path(meta_one);
/// assert_eq!(Some(param_one), tag_param);
///
/// let tag_other: Path = parse_quote!(tag_other);
/// let tag_param_other = tag_parameter(&ast.attrs, &ns, &tag_other);
/// assert_eq!(None, tag_param_other);
/// ```
///
/// # Panics
///
/// Panics if the number of parameters for the tag is not exactly one.
#[allow(clippy::let_and_return)] // Needed due to bug in clippy.
pub fn tag_parameter(attrs: &[Attribute], namespace: &Path, tag: &Path) -> Option<Meta> {
    let namespace_nested_metas_iter = namespace_nested_metas_iter(attrs, namespace);
    let mut tag_nested_metas_iter = tag_nested_metas_iter(namespace_nested_metas_iter, tag);
    let tag_param = tag_nested_metas_iter.next();
    let tag_param_second = tag_nested_metas_iter.next();

    if tag_param_second.is_some() {
        panic!(
            "Expected exactly one parameter for `#[{}({}(..))]`.",
            format_path(namespace),
            format_path(tag),
        );
    }

    tag_param
}

/// Returns the parameters from `#[namespace(tag(param1, param2, ..))]`.
///
/// # Parameters
///
/// * `attrs`: Attributes of the item to inspect.
/// * `namespace`: The `path()` of the first-level attribute.
/// * `tag`: The `path()` of the second-level attribute.
///
/// # Examples
///
/// ```rust,edition2021
/// use proc_macro_roids::tag_parameters;
/// use syn::{parse_quote, DeriveInput, Meta, MetaNameValue, Path};
///
/// let ast: DeriveInput = parse_quote! {
///     #[namespace(tag(One))]
///     #[namespace(tag(two = ""))]
///     pub struct MyEnum;
/// };
///
/// let ns: Path = parse_quote!(namespace);
/// let tag: Path = parse_quote!(tag);
/// let tag_parameters = tag_parameters(&ast.attrs, &ns, &tag);
///
/// let meta_one: Path = parse_quote!(One);
/// let param_one = Meta::Path(meta_one);
/// let meta_two: MetaNameValue = parse_quote!(two = "");
/// let param_two = Meta::NameValue(meta_two);
/// assert_eq!(vec![param_one, param_two], tag_parameters);
/// ```
pub fn tag_parameters(attrs: &[Attribute], namespace: &Path, tag: &Path) -> Vec<Meta> {
    let namespace_nested_metas_iter = namespace_nested_metas_iter(attrs, namespace);
    let parameters = tag_nested_metas_iter(namespace_nested_metas_iter, tag).collect::<Vec<Meta>>();

    parameters
}

/// Returns the meta lists of the form: `#[namespace(..)]`.
///
/// Each `meta_list` is a `namespace(..)` meta item.
///
/// # Parameters
///
/// * `attrs`: Attributes of the item to inspect.
/// * `namespace`: The `path()` of the first-level attribute.
///
/// # Examples
///
/// ```rust,edition2021
/// use proc_macro_roids::namespace_nested_metas_iter;
/// use syn::{parse_quote, DeriveInput, Meta, Path};
///
/// let ast: DeriveInput = parse_quote! {
///     #[namespace(One)]
///     #[namespace(two = "")]
///     pub struct MyEnum;
/// };
///
/// let ns: Path = parse_quote!(namespace);
/// let nested_metas = namespace_nested_metas_iter(&ast.attrs, &ns).collect::<Vec<Meta>>();
///
/// let meta_one: Meta = Meta::Path(parse_quote!(One));
/// let meta_two: Meta = Meta::NameValue(parse_quote!(two = ""));
/// assert_eq!(vec![meta_one, meta_two], nested_metas);
/// ```
pub fn namespace_nested_metas_iter<'f>(
    attrs: &'f [Attribute],
    namespace: &'f Path,
) -> impl Iterator<Item = Meta> + 'f {
    attrs
        .iter()
        .filter_map(move |attr| {
            if attr.path() == namespace {
                attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)
                    .ok()
            } else {
                None
            }
        })
        .flat_map(|nested_metas| nested_metas.into_iter())
}

/// Returns an iterator over nested metas from `#[namespace(tag(..))]`.
///
/// # Parameters
///
/// * `namespace_nested_metas_iter`: The `#[namespace(..)]` meta lists.
/// * `tag`: The `path()` of the second-level attribute.
///
/// # Examples
///
/// ```rust,edition2021
/// use proc_macro_roids::{namespace_nested_metas_iter, tag_nested_metas_iter};
/// use syn::{parse_quote, DeriveInput, Meta, Path};
///
/// let ast: DeriveInput = parse_quote! {
///     #[namespace(tag(One))]
///     #[namespace(tag(two = ""))]
///     pub struct MyEnum;
/// };
///
/// let ns: Path = parse_quote!(namespace);
/// let tag: Path = parse_quote!(tag);
/// let ns_lists = namespace_nested_metas_iter(&ast.attrs, &ns);
/// let nested_metas = tag_nested_metas_iter(ns_lists, &tag).collect::<Vec<Meta>>();
///
/// let meta_one: Meta = Meta::Path(parse_quote!(One));
/// let meta_two: Meta = Meta::NameValue(parse_quote!(two = ""));
/// assert_eq!(vec![meta_one, meta_two], nested_metas);
pub fn tag_nested_metas_iter<'f>(
    namespace_nested_metas_iter: impl Iterator<Item = Meta> + 'f,
    tag: &'f Path,
) -> impl Iterator<Item = Meta> + 'f {
    namespace_nested_metas_iter
        .filter_map(move |meta| {
            if meta.path() == tag {
                meta.require_list()
                    .and_then(|meta_list| {
                        meta_list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)
                    })
                    .ok()
            } else {
                None
            }
        })
        .flatten()
}

/// Returns a `Path` as a String without whitespace between tokens.
pub fn format_path(path: &Path) -> String {
    quote!(#path)
        .to_string()
        .chars()
        .filter(|c| !c.is_whitespace())
        .collect::<String>()
}