use proc_macro2::{Delimiter, TokenTree};
use proc_macro_error::{abort, emit_error};
use syn::{
spanned::Spanned,
visit_mut::{visit_item_trait_mut, VisitMut},
Attribute, TraitItem,
};
use crate::proxy::{parse_types, ProxyType};
pub(crate) fn remove_our_attrs(trait_def: &mut syn::ItemTrait) {
struct AttrRemover;
impl VisitMut for AttrRemover {
fn visit_trait_item_mut(&mut self, item: &mut TraitItem) {
let item_span = item.span();
let (attrs, is_method) = match item {
TraitItem::Method(m) => (&mut m.attrs, true),
TraitItem::Const(c) => (&mut c.attrs, false),
TraitItem::Type(t) => (&mut t.attrs, false),
TraitItem::Macro(m) => (&mut m.attrs, false),
_ => abort!(
item.span(),
"encountered unexpected `TraitItem`, cannot handle that, sorry!";
note = "auto-impl supports only methods, consts, types and macros currently";
),
};
if !is_method && attrs.iter().any(is_our_attr) {
emit_error!(
item_span,
"`#[auto_impl]` attributes are only allowed on methods",
);
}
attrs.retain(|a| !is_our_attr(a));
}
}
visit_item_trait_mut(&mut AttrRemover, trait_def);
}
pub(crate) fn is_our_attr(attr: &Attribute) -> bool {
attr.path.is_ident("auto_impl")
}
pub(crate) fn parse_our_attr(attr: &Attribute) -> Result<OurAttr, ()> {
assert!(is_our_attr(attr));
let tokens = attr.tokens.clone().into_iter().collect::<Vec<_>>();
let body = match &*tokens {
[TokenTree::Group(g)] => g.stream(),
_ => {
emit_error!(
attr.tokens.span(),
"expected single group delimited by `()`, found '{:?}'",
tokens,
);
return Err(());
}
};
let mut it = body.clone().into_iter();
let name = match it.next() {
Some(TokenTree::Ident(x)) => x,
Some(other) => {
emit_error!(other.span(), "expected ident, found '{}'", other);
return Err(());
}
None => {
emit_error!(attr.tokens.span(), "expected ident, found nothing");
return Err(());
}
};
let params = match it.next() {
Some(TokenTree::Group(ref g)) if g.delimiter() == Delimiter::Parenthesis => g.stream(),
Some(other) => {
emit_error!(
other.span(),
"expected arguments for '{}' in parenthesis `()`, found `{}`",
name,
other,
);
return Err(());
}
None => {
emit_error!(
body.span(),
"expected arguments for '{}' in parenthesis `()`, found nothing",
name,
);
return Err(());
}
};
let out = if name == "keep_default_for" {
let proxy_types = parse_types(params.into());
OurAttr::KeepDefaultFor(proxy_types)
} else {
emit_error!(
name.span(), "invalid attribute '{}'", name;
note = "only `keep_default_for` is supported";
);
return Err(());
};
Ok(out)
}
#[derive(Clone, PartialEq, Debug)]
pub(crate) enum OurAttr {
KeepDefaultFor(Vec<ProxyType>),
}