Skip to main content

extern_trait_impl/
lib.rs

1mod args;
2mod decl;
3mod imp;
4
5use args::{DeclArgs, ImplArgs};
6use proc_macro::TokenStream;
7use syn::{Error, parse_macro_input};
8
9#[proc_macro_attribute]
10pub fn extern_trait(args: TokenStream, input: TokenStream) -> TokenStream {
11    if args.is_empty() {
12        imp::expand(
13            ImplArgs {
14                extern_trait: syn::parse_quote!(::extern_trait),
15            },
16            parse_macro_input!(input),
17        )
18    } else {
19        // Try to parse as DeclArgs first (for trait declarations)
20        // If that fails with a trait-specific error, try ImplArgs (for impl blocks with crate = ...)
21        let args_clone = args.clone();
22        match syn::parse::<DeclArgs>(args) {
23            Ok(decl_args) => decl::expand(decl_args, parse_macro_input!(input)),
24            Err(_) => {
25                // Try parsing as ImplArgs instead
26                imp::expand(parse_macro_input!(args_clone), parse_macro_input!(input))
27            }
28        }
29    }
30    .unwrap_or_else(Error::into_compile_error)
31    .into()
32}