| 1 |
extern crate proc_macro; |
| 2 |
extern crate proc_macro2; |
| 3 |
extern crate quote; |
| 4 |
|
| 5 |
use std::collections::HashSet; |
| 6 |
|
| 7 |
use quote::{quote, ToTokens}; |
| 8 |
use syn::{parse_macro_input, punctuated::Punctuated, spanned::Spanned}; |
| 9 |
|
| 10 |
// --------------------------------------------------------------------------- |
| 11 |
|
| 12 |
mod default; |
| 13 |
mod derive; |
| 14 |
mod types; |
| 15 |
mod utils; |
| 16 |
|
| 17 |
// --------------------------------------------------------------------------- |
| 18 |
|
| 19 |
struct Args { |
| 20 |
default: Option<syn::Path>, |
| 21 |
derives: HashSet<types::Type>, |
| 22 |
} |
| 23 |
|
| 24 |
impl Args { |
| 25 |
fn from_meta(arg: &syn::Meta) -> syn::Result<Self> { |
| 26 |
let mut default = None; |
| 27 |
let mut derives = HashSet::new(); |
| 28 |
|
| 29 |
match arg { |
| 30 |
syn::Meta::List(ref l) if l.path.to_token_stream().to_string() == "derive" => { |
| 31 |
let types = l.parse_args_with( |
| 32 |
Punctuated::<syn::Path, syn::Token![,]>::parse_separated_nonempty, |
| 33 |
)?; |
| 34 |
for pair in types.into_pairs() { |
| 35 |
if let Some(d) = types::Type::from_path(pair.value()) { |
| 36 |
derives.insert(d); |
| 37 |
} else { |
| 38 |
return Err(syn::Error::new( |
| 39 |
pair.span(), |
| 40 |
"unknown blanket derive option", |
| 41 |
)); |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
syn::Meta::NameValue(ref n) if n.path.to_token_stream().to_string() == "default" => { |
| 46 |
match n.value { |
| 47 |
syn::Expr::Lit(ref lit) => { |
| 48 |
if let syn::Lit::Str(ref s) = lit.lit { |
| 49 |
match syn::parse_str(&s.value()) { |
| 50 |
Ok(path) if default.is_none() => { |
| 51 |
default = Some(path); |
| 52 |
} |
| 53 |
Ok(_) => { |
| 54 |
return Err(syn::Error::new( |
| 55 |
s.span(), |
| 56 |
"duplicate default module given", |
| 57 |
)) |
| 58 |
} |
| 59 |
Err(_) => { |
| 60 |
return Err(syn::Error::new( |
| 61 |
s.span(), |
| 62 |
"expected module identifier", |
| 63 |
)) |
| 64 |
} |
| 65 |
} |
| 66 |
} else { |
| 67 |
return Err(syn::Error::new(lit.lit.span(), "expected string literal")); |
| 68 |
} |
| 69 |
} |
| 70 |
syn::Expr::Path(ref expr) => { |
| 71 |
if default.replace(expr.path.clone()).is_some() { |
| 72 |
return Err(syn::Error::new( |
| 73 |
n.span(), |
| 74 |
"duplicate default module given", |
| 75 |
)); |
| 76 |
} |
| 77 |
} |
| 78 |
_ => { |
| 79 |
return Err(syn::Error::new( |
| 80 |
n.value.span(), |
| 81 |
"expected path or string literal", |
| 82 |
)); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
_ => return Err(syn::Error::new(arg.span(), "unexpected argument")), |
| 87 |
} |
| 88 |
|
| 89 |
Ok(Self { default, derives }) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// --------------------------------------------------------------------------- |
| 94 |
|
| 95 |
/// Generate blanket implementations for a trait. |
| 96 |
/// |
| 97 |
/// This procedural macro must be used on a `trait` block. It can be used |
| 98 |
/// to either derive a blanket implementation, or to delegate the method |
| 99 |
/// implementations of a trait to a module. |
| 100 |
/// |
| 101 |
#[proc_macro_attribute] |
| 102 |
pub fn blanket( |
| 103 |
args: proc_macro::TokenStream, |
| 104 |
input: proc_macro::TokenStream, |
| 105 |
) -> proc_macro::TokenStream { |
| 106 |
// parse input |
| 107 |
let trait_ = parse_macro_input!(input as syn::ItemTrait); |
| 108 |
let args = parse_macro_input!(args as syn::Meta); |
| 109 |
// parse macro arguments and immediately exit if they are invalid |
| 110 |
let args = match Args::from_meta(&args) { |
| 111 |
Ok(args) => args, |
| 112 |
Err(e) => { |
| 113 |
let err = e.to_compile_error(); |
| 114 |
return proc_macro::TokenStream::from(quote!(#err #trait_)); |
| 115 |
} |
| 116 |
}; |
| 117 |
// generate output |
| 118 |
let mut out = proc_macro2::TokenStream::new(); |
| 119 |
// update trait methods declaration if given a `default = "..."` argument, |
| 120 |
// otherwise simply keep the output |
| 121 |
match args.default { |
| 122 |
None => out.extend(quote!(#trait_)), |
| 123 |
Some(d) => match default::defer_trait_methods(trait_.clone(), d) { |
| 124 |
Ok(trait_) => out.extend(quote!(#trait_)), |
| 125 |
Err(err) => out.extend(err.to_compile_error()), |
| 126 |
}, |
| 127 |
}; |
| 128 |
// add derived implementations |
| 129 |
for d in args.derives { |
| 130 |
match d.defer_trait_methods(&trait_) { |
| 131 |
Ok(item) => out.extend(quote!(#item)), |
| 132 |
Err(e) => out.extend(e.to_compile_error()), |
| 133 |
} |
| 134 |
} |
| 135 |
// // return the new `proc-macro2` token stream as a `proc-macro` stream |
| 136 |
proc_macro::TokenStream::from(out) |
| 137 |
} |