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
extern crate ident_case;
extern crate proc_macro2;
#[cfg_attr(test, macro_use)]
extern crate quote;
#[cfg_attr(test, macro_use)]
extern crate syn;

use proc_macro2::TokenStream;
use quote::ToTokens;

use syn::parse::*;
use syn::{parenthesized, parse, token, Attribute, Ident, Lit, LitBool, Path, Token};

pub mod ast;
pub mod error;
mod from_derive_input;
mod from_field;
mod from_meta;
mod from_variant;

pub use ast::*;
pub use from_derive_input::FromDeriveInput;
pub use from_field::FromField;
pub use from_meta::FromMeta;
pub use from_variant::FromVariant;

pub trait PathExt {
  fn single_ident(&self) -> Option<&Ident>;
  fn to_compact_string(&self) -> String;
}

impl PathExt for Path {
  fn single_ident(&self) -> Option<&Ident> {
    if self.leading_colon.is_none()
      && self.segments.len() == 1
      && self.segments[0].arguments == syn::PathArguments::None
    {
      Some(&self.segments[0].ident)
    } else {
      None
    }
  }

  fn to_compact_string(&self) -> String {
    self
      .segments
      .iter()
      .map(|s| {
        let mut ident = s.ident.to_string();
        match &s.arguments {
          syn::PathArguments::None => ident,
          syn::PathArguments::AngleBracketed(args) => {
            if args.colon2_token.is_some() {
              ident.push_str("::");
            }

            ident.push_str("<");
            for arg in &args.args {
              match arg {
                syn::GenericArgument::Lifetime(l) => {
                  ident.push('\'');
                  ident.push_str(&l.ident.to_string());
                }

                syn::GenericArgument::Type(t) => {
                  let mut ts = TokenStream::new();
                  t.to_tokens(&mut ts);
                  ident.push_str(&format!("{}", ts));
                }

                syn::GenericArgument::Binding(b) => {
                  ident.push_str(&b.ident.to_string());
                  ident.push('=');
                  let mut ts = TokenStream::new();
                  b.ty.to_tokens(&mut ts);
                  ident.push_str(&format!("{}", ts));
                }

                syn::GenericArgument::Constraint(c) => {
                  ident.push_str(&c.ident.to_string());
                  ident.push_str(": ");
                  let mut ts = TokenStream::new();
                  c.bounds.to_tokens(&mut ts);
                  ident.push_str(&format!("{}", ts));
                }

                syn::GenericArgument::Const(e) => {
                  let mut ts = TokenStream::new();
                  e.to_tokens(&mut ts);
                  ident.push_str(&format!("{}", ts));
                }
              }
            }

            ident.push_str(">");
            ident
          }
          syn::PathArguments::Parenthesized(args) => {
            let mut ts = TokenStream::new();
            args.to_tokens(&mut ts);
            ident.push_str(&format!("{}", ts));
            ident
          }
        }
      })
      .collect::<Vec<String>>()
      .join("::")
  }
}

pub trait AttrExt {
  fn meta(&self) -> Result<Meta>;
}

impl AttrExt for Attribute {
  fn meta(&self) -> Result<Meta> {
    let parser = |input: ParseStream| parse_meta_after_path(self.path.clone(), input);

    parse::Parser::parse2(parser, self.tokens.clone())
  }
}

#[derive(Debug, Clone, PartialEq)]
pub enum Meta {
  Path(syn::Path),
  List(MetaList),
  NameValue(MetaNameValue),
}

impl Meta {
  pub fn path(&self) -> &Path {
    match self {
      Meta::Path(meta) => meta,
      Meta::List(meta) => &meta.path,
      Meta::NameValue(meta) => &meta.path,
    }
  }

  pub fn is<S>(&self, path: S) -> bool
  where
    S: AsRef<str>,
  {
    match self.path().single_ident() {
      None => false,
      Some(i) => {
        let actual = i.to_string();
        &actual == path.as_ref()
      }
    }
  }
}

#[derive(Debug, Clone, PartialEq)]
pub struct MetaList {
  pub path: syn::Path,
  pub paren_token: syn::token::Paren,
  pub nested: syn::punctuated::Punctuated<NestedMeta, syn::token::Comma>,
}

impl IntoIterator for MetaList {
  type Item = <syn::punctuated::Punctuated<NestedMeta, syn::token::Comma> as IntoIterator>::Item;
  type IntoIter =
    <syn::punctuated::Punctuated<NestedMeta, syn::token::Comma> as IntoIterator>::IntoIter;

  #[inline]
  fn into_iter(self) -> Self::IntoIter {
    self.nested.into_iter()
  }
}

impl<'a> IntoIterator for &'a MetaList {
  type Item =
    <&'a syn::punctuated::Punctuated<NestedMeta, syn::token::Comma> as IntoIterator>::Item;
  type IntoIter =
    <&'a syn::punctuated::Punctuated<NestedMeta, syn::token::Comma> as IntoIterator>::IntoIter;

  #[inline]
  fn into_iter(self) -> Self::IntoIter {
    (&self.nested).into_iter()
  }
}

/// Element of a compile-time attribute list.
#[derive(Debug, Clone, PartialEq)]
pub enum NestedMeta {
  /// A structured meta item, like the `Copy` in `#[derive(Copy)]` which
  /// would be a nested `Meta::Path`.
  Meta(Meta),

  /// A Rust literal, like the `"new_name"` in `#[rename("new_name")]`.
  Literal(syn::Lit),
}

#[derive(Debug, Clone, PartialEq)]
pub struct MetaNameValue {
  pub path: syn::Path,
  pub eq_token: syn::token::Eq,
  pub val: MetaValue,
}

#[derive(Debug, Clone, PartialEq)]
pub enum MetaValue {
  Path(syn::Path),
  Literal(syn::Lit),
}

impl Parse for MetaValue {
  fn parse(input: ParseStream) -> Result<Self> {
    let ahead = input.fork();

    if ahead.call(Path::parse).is_ok() {
      input.parse().map(MetaValue::Path)
    } else if ahead.call(Lit::parse).is_ok() {
      input.parse().map(MetaValue::Literal)
    } else {
      Err(input.error("expected path or literal"))
    }
  }
}

impl Parse for Meta {
  fn parse(input: ParseStream) -> Result<Self> {
    let path = input.call(Path::parse)?;
    parse_meta_after_path(path, input)
  }
}

impl Parse for MetaList {
  fn parse(input: ParseStream) -> Result<Self> {
    let path = input.call(Path::parse)?;
    parse_meta_list_after_path(path, input)
  }
}

impl Parse for MetaNameValue {
  fn parse(input: ParseStream) -> Result<Self> {
    let path = input.call(Path::parse)?;
    parse_meta_name_value_after_path(path, input)
  }
}

impl Parse for NestedMeta {
  fn parse(input: ParseStream) -> Result<Self> {
    let ahead = input.fork();

    if ahead.peek(Lit) && !(ahead.peek(LitBool) && ahead.peek2(Token![=])) {
      input.parse().map(NestedMeta::Literal)
    } else if ahead.call(Path::parse).is_ok() {
      input.parse().map(NestedMeta::Meta)
    } else {
      Err(input.error("expected path or literal"))
    }
  }
}

fn parse_meta_after_path(path: Path, input: ParseStream) -> Result<Meta> {
  if input.peek(token::Paren) {
    parse_meta_list_after_path(path, input).map(Meta::List)
  } else if input.peek(Token![=]) {
    parse_meta_name_value_after_path(path, input).map(Meta::NameValue)
  } else {
    Ok(Meta::Path(path))
  }
}

fn parse_meta_list_after_path(path: Path, input: ParseStream) -> Result<MetaList> {
  let content;
  Ok(MetaList {
    path: path,
    paren_token: parenthesized!(content in input),
    nested: content.parse_terminated(NestedMeta::parse)?,
  })
}

fn parse_meta_name_value_after_path(path: Path, input: ParseStream) -> Result<MetaNameValue> {
  Ok(MetaNameValue {
    path: path,
    eq_token: input.parse()?,
    val: input.parse()?,
  })
}

impl ToTokens for MetaValue {
  fn to_tokens(&self, tokens: &mut TokenStream) {
    match self {
      MetaValue::Literal(l) => l.to_tokens(tokens),
      MetaValue::Path(p) => p.to_tokens(tokens),
    }
  }
}

impl ToTokens for MetaNameValue {
  fn to_tokens(&self, tokens: &mut TokenStream) {
    self.path.to_tokens(tokens);
    self.eq_token.to_tokens(tokens);
    self.val.to_tokens(tokens);
  }
}

impl ToTokens for NestedMeta {
  fn to_tokens(&self, tokens: &mut TokenStream) {
    match self {
      NestedMeta::Meta(meta) => meta.to_tokens(tokens),
      NestedMeta::Literal(lit) => lit.to_tokens(tokens),
    }
  }
}

impl ToTokens for MetaList {
  fn to_tokens(&self, tokens: &mut TokenStream) {
    self.path.to_tokens(tokens);
    self.paren_token.surround(tokens, |tokens| {
      self.nested.to_tokens(tokens);
    })
  }
}

impl ToTokens for Meta {
  fn to_tokens(&self, tokens: &mut TokenStream) {
    match self {
      Meta::Path(path) => path.to_tokens(tokens),
      Meta::List(list) => list.to_tokens(tokens),
      Meta::NameValue(nv) => nv.to_tokens(tokens),
    }
  }
}

#[cfg(test)]
mod test {
  use super::*;

  #[test]
  pub fn derive_test() {
    let input: syn::ItemStruct = parse_quote! { #[evitable::from = ::std::io::Error] struct Foo; };
    let attr = &input.attrs[0];
    let meta = attr.meta().unwrap();
    let path_str = format!("{}", meta.path().into_token_stream());
    println!("{:?}", meta);
    assert_eq!(path_str, "evitable :: from");
  }
}