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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
use crate::quote::ToTokens;
use proc_macro2::TokenStream;
use std::rc::Rc;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use syn::spanned::Spanned;
use syn::{Ident, Lit, Path};

use super::error::{Error, Result};
use super::{Meta, MetaValue, NestedMeta};

pub trait FromMeta: Sized {
  fn from_nested_meta(item: &NestedMeta) -> Result<Self> {
    (match item {
      NestedMeta::Literal(lit) => Self::from_lit(lit),
      NestedMeta::Meta(mi) => match mi {
        Meta::Path(p) => Self::from_path(p),
        Meta::NameValue(_) => Err(Error::unexpected_type("name value").with_span(item)),
        Meta::List(_) => Err(Error::unexpected_type("list").with_span(item)),
      },
    })
    .map_err(|e| e.with_span(item))
  }

  /// Create an instance from a `syn::Meta` by dispatching to the format-appropriate
  /// trait function. This generally should not be overridden by implementers.
  ///
  /// # Error Spans
  /// If this method is overridden and can introduce errors that weren't passed up from
  /// other `from_meta` calls, the override must call `with_span` on the error using the
  /// `item` to make sure that the emitted diagnostic points to the correct location in
  /// source code.
  fn from_meta(item: &Meta) -> Result<Self> {
    (match item {
      Meta::Path(_) => Self::from_empty(),
      Meta::List(value) => {
        Self::from_list(value.nested.iter().collect::<Vec<&NestedMeta>>().as_ref())
      }
      Meta::NameValue(value) => Self::from_value(&value.val),
    })
    .map_err(|e| e.with_span(item))
  }

  /// Create an instance from the presence of the word in the attribute with no
  /// additional options specified.
  fn from_empty() -> Result<Self> {
    Err(Error::unsupported_format("empty"))
  }

  /// Create an instance from a list of nested meta items.
  #[allow(unused_variables)]
  fn from_list(items: &[&NestedMeta]) -> Result<Self> {
    match items.len() {
      0 => Self::from_empty(),
      1 => Self::from_nested_meta(&items[0]),
      _ => Err(Error::unsupported_format("list")),
    }
  }

  /// Create an instance from a literal value of either `foo = "bar"` or `foo("bar")`.
  /// This dispatches to the appropriate method based on the type of literal encountered,
  /// and generally should not be overridden by implementers.
  ///
  /// # Error Spans
  /// If this method is overridden, the override must make sure to add `value`'s span
  /// information to the returned error by calling `with_span(value)` on the `Error` instance.
  fn from_value(value: &MetaValue) -> Result<Self> {
    (match value {
      MetaValue::Path(path) => Self::from_path(path),
      MetaValue::Literal(lit) => Self::from_lit(lit),
    })
    .map_err(|e| e.with_span(value))
  }

  fn from_lit(lit: &Lit) -> Result<Self> {
    (match lit {
      Lit::Bool(b) => Self::from_bool(b.value, b),
      Lit::Str(s) => Self::from_string(&s.value(), s),
      Lit::Char(c) => Self::from_char(c.value(), c),
      Lit::Int(i) => Self::from_int(i.base10_parse()?, i),
      _ => Err(Error::unexpected_lit_type(lit)),
    })
    .map_err(|e| e.with_span(lit))
  }

  /// Create an instance from a char literal in a value position.
  #[allow(unused_variables)]
  fn from_char<S: Spanned>(value: char, span: &S) -> Result<Self> {
    Err(Error::unexpected_type("char").with_span(span))
  }

  /// Create an instance from a int literal in a value position.
  #[allow(unused_variables)]
  fn from_int<S: Spanned>(value: u64, span: &S) -> Result<Self> {
    Err(Error::unexpected_type("int").with_span(span))
  }

  /// Create an instance from a char literal in a value position.
  #[allow(unused_variables)]
  fn from_path(value: &Path) -> Result<Self> {
    if value.leading_colon.is_none()
      && value.segments.len() == 1
      && value.segments[0].arguments == syn::PathArguments::None
    {
      Self::from_ident(&value.segments[0].ident)
    } else {
      Err(Error::unexpected_type("path"))
    }
  }

  /// Create an instance from a string literal in a value position.
  #[allow(unused_variables)]
  fn from_string<S: Spanned>(value: &str, span: &S) -> Result<Self> {
    Err(Error::unexpected_type("string").with_span(span))
  }

  /// Create an instance from a bool literal in a value position.
  #[allow(unused_variables)]
  fn from_bool<S: Spanned>(value: bool, span: &S) -> Result<Self> {
    Err(Error::unexpected_type("bool").with_span(span))
  }

  #[allow(unused_variables)]
  fn from_ident(value: &Ident) -> Result<Self> {
    Err(Error::unexpected_type("ident"))
  }
}

// FromMeta impls for std and syn types.

impl FromMeta for () {
  fn from_empty() -> Result<Self> {
    Ok(())
  }
}

impl FromMeta for bool {
  fn from_empty() -> Result<Self> {
    Ok(true)
  }

  fn from_bool<S: Spanned>(value: bool, _: &S) -> Result<Self> {
    Ok(value)
  }

  fn from_string<S: Spanned>(value: &str, span: &S) -> Result<Self> {
    value
      .parse()
      .map_err(|_| Error::unknown_value(value).with_span(span))
  }
}

impl FromMeta for AtomicBool {
  fn from_meta(mi: &Meta) -> Result<Self> {
    FromMeta::from_meta(mi)
      .map(AtomicBool::new)
      .map_err(|e| e.with_span(mi))
  }
}

impl FromMeta for String {
  fn from_string<S: Spanned>(s: &str, _span: &S) -> Result<Self> {
    Ok(s.to_string())
  }

  fn from_path(p: &Path) -> Result<Self> {
    let mut ss = TokenStream::new();
    p.to_tokens(&mut ss);
    Ok(format!("{}", ss))
  }
}

/// Generate an impl of `FromMeta` that will accept strings which parse to numbers or
/// integer literals.
macro_rules! from_meta_num {
  ($ty:ident) => {
    impl FromMeta for $ty {
      fn from_string<S: Spanned>(s: &str, span: &S) -> Result<Self> {
        s.parse()
          .map_err(|_| Error::unknown_value(s).with_span(span))
      }

      fn from_lit(value: &Lit) -> Result<Self> {
        (match value {
          Lit::Str(s) => Self::from_string(&s.value(), s),
          Lit::Int(s) => Ok(s.base10_parse()?),
          v => Err(Error::unexpected_lit_type(value).with_span(&v)),
        })
        .map_err(|e| e.with_span(value))
      }
    }
  };
}

from_meta_num!(u8);
from_meta_num!(u16);
from_meta_num!(u32);
from_meta_num!(u64);
from_meta_num!(usize);
from_meta_num!(i8);
from_meta_num!(i16);
from_meta_num!(i32);
from_meta_num!(i64);
from_meta_num!(isize);

/// Generate an impl of `FromMeta` that will accept strings which parse to floats or
/// float literals.
macro_rules! from_meta_float {
  ($ty:ident) => {
    impl FromMeta for $ty {
      fn from_string<S: Spanned>(s: &str, span: &S) -> Result<Self> {
        s.parse()
          .map_err(|_| Error::unknown_value(s).with_span(span))
      }

      fn from_lit(value: &Lit) -> Result<Self> {
        (match value {
          Lit::Str(s) => Self::from_string(&s.value(), s),
          Lit::Float(s) => Ok(s.base10_parse()?),
          v => Err(Error::unexpected_lit_type(value).with_span(&v)),
        })
        .map_err(|e| e.with_span(value))
      }
    }
  };
}

from_meta_float!(f32);
from_meta_float!(f64);

/// Parsing support for identifiers. This attempts to preserve span information
/// when available, but also supports parsing strings with the call site as the
/// emitted span.
impl FromMeta for syn::Ident {
  fn from_ident(value: &Ident) -> Result<Self> {
    Ok(value.clone())
  }

  fn from_string<S: Spanned>(value: &str, span: &S) -> Result<Self> {
    Ok(syn::Ident::new(value, span.span()))
  }
}

/// Parsing support for paths. This attempts to preserve span information when available,
/// but also supports parsing strings with the call site as the emitted span.
impl FromMeta for syn::Path {
  fn from_path(path: &Path) -> Result<Self> {
    Ok(path.clone())
  }

  fn from_string<S: Spanned>(value: &str, span: &S) -> Result<Self> {
    syn::parse_str(value).map_err(|_| Error::unknown_value(value).with_span(span))
  }

  fn from_lit(value: &Lit) -> Result<Self> {
    if let Lit::Str(ref path_str) = *value {
      path_str
        .parse()
        .map_err(|_| Error::unknown_lit_str_value(path_str).with_span(value))
    } else {
      Err(Error::unexpected_lit_type(value).with_span(value))
    }
  }
}

impl FromMeta for syn::Lit {
  fn from_lit(value: &Lit) -> Result<Self> {
    Ok(value.clone())
  }
}

macro_rules! from_meta_lit {
  ($impl_ty:path, $lit_variant:path) => {
    impl FromMeta for $impl_ty {
      fn from_lit(value: &Lit) -> Result<Self> {
        if let $lit_variant(ref value) = *value {
          Ok(value.clone())
        } else {
          Err(Error::unexpected_lit_type(value).with_span(value))
        }
      }
    }
  };
}

from_meta_lit!(syn::LitInt, Lit::Int);
from_meta_lit!(syn::LitFloat, Lit::Float);
from_meta_lit!(syn::LitStr, Lit::Str);
from_meta_lit!(syn::LitByte, Lit::Byte);
from_meta_lit!(syn::LitByteStr, Lit::ByteStr);
from_meta_lit!(syn::LitChar, Lit::Char);
from_meta_lit!(syn::LitBool, Lit::Bool);
from_meta_lit!(proc_macro2::Literal, Lit::Verbatim);

impl FromMeta for Meta {
  fn from_meta(value: &Meta) -> Result<Self> {
    Ok(value.clone())
  }
}

impl FromMeta for ident_case::RenameRule {
  fn from_string<S: Spanned>(value: &str, span: &S) -> Result<Self> {
    value
      .parse()
      .map_err(|_| Error::unknown_value(value).with_span(span))
  }
}

impl<T: FromMeta> FromMeta for Option<T> {
  fn from_meta(item: &Meta) -> Result<Self> {
    FromMeta::from_meta(item).map(Some)
  }
}

impl<T: FromMeta> FromMeta for Box<T> {
  fn from_meta(item: &Meta) -> Result<Self> {
    FromMeta::from_meta(item).map(Box::new)
  }
}

impl<T: FromMeta> FromMeta for Result<T> {
  fn from_meta(item: &Meta) -> Result<Self> {
    Ok(FromMeta::from_meta(item))
  }
}

/// Parses the meta-item, and in case of error preserves a copy of the input for
/// later analysis.
impl<T: FromMeta> FromMeta for ::std::result::Result<T, Meta> {
  fn from_meta(item: &Meta) -> Result<Self> {
    T::from_meta(item)
      .map(Ok)
      .or_else(|_| Ok(Err(item.clone())))
  }
}

impl<T: FromMeta> FromMeta for Rc<T> {
  fn from_meta(item: &Meta) -> Result<Self> {
    FromMeta::from_meta(item).map(Rc::new)
  }
}

impl<T: FromMeta> FromMeta for Arc<T> {
  fn from_meta(item: &Meta) -> Result<Self> {
    FromMeta::from_meta(item).map(Arc::new)
  }
}

impl<T: FromMeta> FromMeta for Vec<T> {
  fn from_empty() -> Result<Self> {
    Ok(Vec::new())
  }

  fn from_list(items: &[&NestedMeta]) -> Result<Self> {
    let mut ret = Vec::with_capacity(items.len());
    for item in items {
      ret.push(T::from_nested_meta(item)?);
    }

    Ok(ret)
  }

  fn from_value(val: &MetaValue) -> Result<Self> {
    let mut ret = Vec::with_capacity(1);
    ret.push(T::from_value(val)?);
    Ok(ret)
  }
}

/// Tests for `FromMeta` implementations. Wherever the word `ignore` appears in test input,
/// it should not be considered by the parsing.
#[cfg(test)]
mod tests {
  use proc_macro2::TokenStream;
  use syn;

  use super::Meta;
  use super::{FromMeta, Result};
  use crate::AttrExt;

  /// parse a string as a syn::Meta instance.
  fn pm(tokens: TokenStream) -> ::std::result::Result<Meta, String> {
    let attribute: syn::Attribute = parse_quote!(#[#tokens]);
    attribute.meta().map_err(|_| "Unable to parse".into())
  }

  fn fm<T: FromMeta>(tokens: TokenStream) -> T {
    FromMeta::from_meta(&pm(tokens).expect("Tests should pass well-formed input"))
      .expect("Tests should pass valid input")
  }

  #[test]
  fn unit_succeeds() {
    assert_eq!(fm::<()>(quote!(ignore)), ());
  }

  #[test]
  fn bool_succeeds() {
    // word format
    assert_eq!(fm::<bool>(quote!(ignore)), true);

    // bool literal
    assert_eq!(fm::<bool>(quote!(ignore = true)), true);
    assert_eq!(fm::<bool>(quote!(ignore = false)), false);

    // string literals
    assert_eq!(fm::<bool>(quote!(ignore = "true")), true);
    assert_eq!(fm::<bool>(quote!(ignore = "false")), false);
  }

  #[test]
  fn string_succeeds() {
    // cooked form
    assert_eq!(&fm::<String>(quote!(ignore = "world")), "world");

    // raw form
    assert_eq!(&fm::<String>(quote!(ignore = r#"world"#)), "world");
  }

  #[test]
  fn number_succeeds() {
    assert_eq!(fm::<u8>(quote!(ignore = "2")), 2u8);
    assert_eq!(fm::<i16>(quote!(ignore = "-25")), -25i16);
    assert_eq!(fm::<f64>(quote!(ignore = "1.4e10")), 1.4e10);
  }

  #[test]
  fn int_without_quotes() {
    assert_eq!(fm::<u8>(quote!(ignore = 2)), 2u8);
    assert_eq!(fm::<u16>(quote!(ignore = 255)), 255u16);
    assert_eq!(fm::<u32>(quote!(ignore = 5000)), 5000u32);

    // Check that we aren't tripped up by incorrect suffixes
    assert_eq!(fm::<u32>(quote!(ignore = 5000i32)), 5000u32);
  }

  #[test]
  fn float_without_quotes() {
    assert_eq!(fm::<f32>(quote!(ignore = 2.)), 2.0f32);
    assert_eq!(fm::<f32>(quote!(ignore = 2.0)), 2.0f32);
    assert_eq!(fm::<f64>(quote!(ignore = 1.4e10)), 1.4e10f64);
  }

  #[test]
  fn meta_succeeds() {
    assert_eq!(
      fm::<Meta>(quote!(hello(world, today))),
      pm(quote!(hello(world, today))).unwrap()
    );
  }

  /// Tests that fallible parsing will always produce an outer `Ok` (from `fm`),
  /// and will accurately preserve the inner contents.
  #[test]
  fn result_succeeds() {
    fm::<Result<()>>(quote!(ignore)).unwrap();
    fm::<Result<()>>(quote!(ignore(world))).unwrap_err();
  }
}