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
use crate::{convert::FromMeta, DefaultValue};
#[cfg(feature = "legacy")]
use macro_compose::{Collector, Context, Lint};
use proc_macro2::Span;
use quote::{format_ident, ToTokens};
use std::{iter::FromIterator, mem::replace};
use syn::{
    parse::Parse, parse2, parse_quote, punctuated::Punctuated, Attribute, Error, Lit, Meta,
    NestedMeta, Result,
};

/// a field definition
/// # Example
/// ```
/// # use macro_input_core as macro_input;
/// use macro_input::{DefaultValue, Def};
///
/// const BAR_FIELD: Def = Def::new("foo", "bar", false, DefaultValue::Bool(None));
/// const BAZ_FIELD: Def = Def::new("foo", "baz", false, DefaultValue::Str(None));
/// ```
pub struct Def<'a> {
    /// the path/namespace of the field
    pub path: &'a str,
    /// the name of the field
    pub name: &'a str,
    /// whether or not this field is required
    pub required: bool,
    /// the typed default value
    pub default: DefaultValue,
}

impl<'a> Def<'a> {
    /// create a new field definition
    #[must_use]
    pub const fn new(path: &'a str, name: &'a str, required: bool, default: DefaultValue) -> Self {
        Def {
            path,
            name,
            required,
            default,
        }
    }

    /// strip away the attributes for this field
    ///
    /// This is useful for attribute macros because rust has no way of knowing which attributes were used.
    /// ```
    /// # use macro_input_core as macro_input;
    /// use macro_input::{DefaultValue, Def};
    /// use syn::{parse_quote, Attribute};
    ///
    /// // construct some attributes
    /// let foo_attr: Attribute = parse_quote!(#[foo(bar = false)]);
    /// let other_attr: Attribute = parse_quote!(#[some(thing = "value")]);
    /// let mut attrs = vec![foo_attr, other_attr.clone()];
    ///
    /// // strip away all mentions of the field bar in foo
    /// const BAR_FIELD: Def = Def::new("foo", "bar", false, DefaultValue::Bool(None));
    /// BAR_FIELD.strip(&mut attrs);
    ///
    /// // the Vec no longer contains #[foo(bar = false)] but #[some(thing = "value")] remains
    /// assert_eq!(attrs, vec![other_attr]);
    /// ```
    pub fn strip(&self, attrs: &mut Vec<Attribute>) {
        let data = replace(attrs, Vec::new());
        attrs.extend(data.into_iter().filter_map(|mut a| {
            if self.strip_from_attribute(&mut a) {
                None
            } else {
                Some(a)
            }
        }));
    }

    /// strip the attribute and return whether it was empty
    fn strip_from_attribute(&self, attr: &mut Attribute) -> bool {
        let mut meta = if let Ok(meta) = attr.parse_meta() {
            meta
        } else {
            return false;
        };

        // check the path
        if !meta.path().is_ident(self.path) {
            return false;
        }

        match &mut meta {
            Meta::List(list) => {
                let new_punctuated = list
                    .nested
                    .iter()
                    .filter(|meta| {
                        if let NestedMeta::Meta(meta) = meta {
                            !meta.path().is_ident(self.name)
                        } else {
                            true
                        }
                    })
                    .cloned();
                list.nested = Punctuated::from_iter(new_punctuated);

                let empty = list.nested.is_empty();
                *attr = parse_quote!(#[#list]);
                empty
            }
            Meta::Path(_) => true,
            _ => false,
        }
    }

    /// try to find the meta that has the value for this field
    ///
    /// # Errors
    /// may return the error if the field is required but not found
    pub fn get_meta(&self, attrs: &[Attribute]) -> Result<Option<Meta>> {
        for attr in attrs.iter() {
            let meta = attr.parse_meta()?;
            if meta.path().is_ident(self.path) {
                if let Meta::List(list) = meta {
                    for meta in list.nested.iter() {
                        if let NestedMeta::Meta(meta) = meta {
                            if meta.path().is_ident(self.name) {
                                return Ok(Some(meta.clone()));
                            }
                        }
                    }
                }
            }
        }

        if self.required {
            return Err(Error::new(
                Span::call_site(),
                format!(
                    "attribute for required field not found: {}::{}",
                    self.path, self.name
                ),
            ));
        }

        // construct a default meta
        if let Some(lit) = self.default.as_lit() {
            let name = format_ident!("{}", self.path);

            return Ok(Some(parse_quote!(#name = #lit)));
        }

        Ok(None)
    }

    /// try to find the literal that has the value for this field
    ///
    /// # Errors
    /// may return the error if the field is required but not found
    pub fn get_lit(&self, attrs: &[Attribute]) -> Result<Option<Lit>> {
        Ok(self.get_meta(attrs)?.and_then(|m| match m {
            Meta::NameValue(nvm) => Some(nvm.lit),
            _ => None,
        }))
    }

    /// try to parse the literal that has the value for this field
    ///
    /// # Errors
    /// may return the error if parsing fails
    pub fn get<L: Parse>(&self, attrs: &[Attribute]) -> Result<Option<L>> {
        self.get_lit(attrs)?
            .map(|lit| {
                let tokens = lit.to_token_stream();
                parse2(tokens)
            })
            .transpose()
    }

    /// try to extract the value from the literal that has the value for this field
    ///
    /// ```
    /// # use macro_input_core as macro_input;
    /// use macro_input::{DefaultValue, Def};
    /// use syn::{parse_quote, Attribute};
    ///
    /// # fn main() -> syn::Result<()> {
    /// let attr = parse_quote!(#[foo(bar = false)]);
    /// const BAR_FIELD: Def = Def::new("foo", "bar", false, DefaultValue::Bool(None));
    /// let value = BAR_FIELD.get_value::<bool>(&[attr])?;
    /// assert_eq!(value, false);
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    /// may return an error if the field doesn't exist or has a value of the wrong type
    pub fn get_value<V: FromMeta>(&self, attrs: &[Attribute]) -> Result<V> {
        self.get_meta(attrs).and_then(FromMeta::from)
    }
}

#[cfg(feature = "legacy")]
impl Lint<Vec<Attribute>> for Def<'_> {
    fn lint(&self, input: &Vec<Attribute>, c: &mut Collector) {
        let mut found = false;

        for attr in input.iter() {
            let meta = attr.parse_meta().unwrap();
            if meta.path().is_ident(self.path) {
                if let Meta::List(list) = meta {
                    for meta in list.nested.iter() {
                        if let NestedMeta::Meta(meta) = meta {
                            if meta.path().is_ident(self.name) {
                                match meta {
                                    Meta::NameValue(meta) => {
                                        if found {
                                            c.error(Error::new_spanned(
                                                meta,
                                                format!("dupplicate {} attribute", self.path),
                                            ));
                                        } else {
                                            found = true;
                                        }

                                        let some_lit = Some(&meta.lit);
                                        let mut subcontext = Context::new_by_ref(c, &some_lit);
                                        subcontext.lint(
                                            &self
                                                .default
                                                .ty(!self.required
                                                    && !self.default.has_default_data()),
                                        );
                                    }
                                    Meta::Path(_) => {
                                        if found {
                                            c.error(Error::new_spanned(
                                                meta,
                                                format!("dupplicate {} attribute", self.path),
                                            ));
                                        } else {
                                            found = true;
                                        }

                                        let mut subcontext = Context::new_by_ref(c, &None);
                                        subcontext.lint(
                                            &self
                                                .default
                                                .ty(!self.required
                                                    && !self.default.has_default_data()),
                                        );
                                    }
                                    _ => {
                                        c.error(Error::new_spanned(&meta, "unexpected meta list"));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        if !found && self.required {
            c.error(Error::new(
                Span::call_site(),
                format!("missing required {} attribute", self.name),
            ));
        }
    }
}