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
use proc_macro2::Span;
use quote::quote;
use syn::{punctuated::Pair, Attribute, Ident, Meta, MetaList, NestedMeta, Path};

/// Returns the `Path` of a nested meta. If it is a literal, `None` is returned.
///
/// # Parameters
///
/// * `nested_meta`: The `NestedMeta` to extract the `Path` from.
pub fn nested_meta_to_path(nested_meta: &NestedMeta) -> Option<&Path> {
    // kcov-ignore-start
    match nested_meta {
        // kcov-ignore-end
        NestedMeta::Meta(meta) => Some(meta.path()),
        NestedMeta::Lit(..) => None, // kcov-ignore
    }
}

/// Returns whether the `MetaList` contains the specified `NestedMeta`.
///
/// This can be used to check if a `#[derive(..)]` contains `SomeDerive`.
///
/// # Parameters
///
/// * `meta_list`: The `MetaList` to check.
/// * `operand`: `NestedMeta` that may be in the list.
pub fn meta_list_contains(meta_list: &MetaList, operand: &NestedMeta) -> bool {
    meta_list
        .nested
        .iter()
        .any(|nested_meta| nested_meta == operand)
}

/// Returns an `Ident` by concatenating `String` representations.
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())
}

/// 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()
        .map(Attribute::parse_meta)
        .filter_map(Result::ok)
        .filter(|meta| meta.path() == namespace)
        .any(|meta| {
            if let Meta::List(meta_list) = meta {
                meta_list
                    .nested
                    .iter()
                    .filter_map(|nested_meta| {
                        if let NestedMeta::Meta(meta) = nested_meta {
                            Some(meta)
                        } else {
                            None // kcov-ignore
                        }
                    })
                    .any(|meta| meta.path() == tag)
            } else {
                false
            }
            // kcov-ignore-start
        })
    // kcov-ignore-end
}

/// 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.
///
/// # 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<NestedMeta> {
    let error_message = {
        format!(
            "Expected exactly one identifier for `#[{}({}(..))]`.",
            format_path(namespace),
            format_path(tag),
        )
    };
    let namespace_meta_lists = namespace_meta_lists(attrs, namespace);
    let meta_param = tag_meta_list_owned(namespace_meta_lists, tag)
        // We want to insert a resource for each item in the list.
        .map(|meta_list| {
            if meta_list.nested.len() != 1 {
                panic!("{}. `{:?}`", &error_message, &meta_list.nested);
            }

            meta_list
                .nested
                .into_pairs()
                .map(Pair::into_value)
                .next()
                .expect("Expected one meta item to exist.")
        })
        .next();

    meta_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.
pub fn tag_parameters(attrs: &[Attribute], namespace: &Path, tag: &Path) -> Vec<NestedMeta> {
    let namespace_meta_lists = namespace_meta_lists(attrs, namespace);
    let parameters = tag_meta_list_owned(namespace_meta_lists, tag)
        .flat_map(|meta_list| meta_list.nested.into_pairs().map(Pair::into_value))
        .collect::<Vec<NestedMeta>>();

    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.
pub fn namespace_meta_lists(attrs: &[Attribute], namespace: &Path) -> Vec<MetaList> {
    attrs
        .iter()
        .map(Attribute::parse_meta)
        .filter_map(Result::ok)
        .filter(|meta| meta.path() == namespace)
        .filter_map(|meta| {
            if let Meta::List(meta_list) = meta {
                Some(meta_list)
            } else {
                None
            }
        })
        .collect::<Vec<MetaList>>()
}

/// Returns an iterator over meta lists from `#[namespace(tag(..))]`.
///
/// For an owned version of the iterator, see `tag_meta_list_owned`
///
/// # Parameters
///
/// * `namespace_meta_lists`: The `#[namespace(..)]` meta lists.
/// * `tag`: The `path()` of the second-level attribute.
pub fn tag_meta_list<'f>(
    namespace_meta_lists: &'f [MetaList],
    tag: &'f Path,
) -> impl Iterator<Item = &'f MetaList> + 'f {
    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 // kcov-ignore
            }
        })
        .filter(move |meta| meta.path() == tag)
        // `meta` is the `tag(..)` item.
        .filter_map(|meta| {
            if let Meta::List(meta_list) = meta {
                Some(meta_list)
            } else {
                None // kcov-ignore
            }
        })
}

/// Returns an iterator over meta lists from `#[namespace(tag(..))]`.
///
/// # Parameters
///
/// * `namespace_meta_lists`: The `#[namespace(..)]` meta lists.
/// * `tag`: The `path()` of the second-level attribute.
pub fn tag_meta_list_owned<'f>(
    namespace_meta_lists: Vec<MetaList>,
    tag: &'f Path,
) -> impl Iterator<Item = MetaList> + 'f {
    namespace_meta_lists
        .into_iter()
        .flat_map(|meta_list| meta_list.nested.into_pairs().map(Pair::into_value))
        .filter_map(|nested_meta| {
            if let NestedMeta::Meta(meta) = nested_meta {
                Some(meta)
            } else {
                None // kcov-ignore
            }
        })
        .filter(move |meta| meta.path() == tag)
        // `meta` is the `tag(..)` item.
        .filter_map(|meta| {
            if let Meta::List(meta_list) = meta {
                Some(meta_list)
            } else {
                None // kcov-ignore
            }
        })
}

/// 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>()
}