Skip to main content

setty_derive/
lib.rs

1mod attrs;
2mod combine;
3mod config;
4mod default;
5mod derive;
6
7/////////////////////////////////////////////////////////////////////////////////////////
8
9#[proc_macro_derive(Config, attributes(config, serde, schemars))]
10pub fn derive_config(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
11    let input = syn::parse_macro_input!(input as syn::DeriveInput);
12
13    let combine_output = match combine::combine_impl(&input) {
14        Ok(output) => output,
15        Err(err) => return err.to_compile_error().into(),
16    };
17
18    let config_output = match config::config_impl(input) {
19        Ok(output) => output,
20        Err(err) => return err.to_compile_error().into(),
21    };
22
23    proc_macro::TokenStream::from(quote::quote! {
24        #config_output
25        #combine_output
26    })
27}
28
29/////////////////////////////////////////////////////////////////////////////////////////
30
31/// Special version of `#[derive(Default)]` that recognizes `#[config(default = $expr)]` attributes
32#[proc_macro_derive(Default, attributes(config, default))]
33pub fn derive_default(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
34    let input = syn::parse_macro_input!(input as syn::DeriveInput);
35
36    match default::default_impl(input) {
37        Ok(output) => proc_macro::TokenStream::from(output),
38        Err(err) => err.to_compile_error().into(),
39    }
40}
41
42/////////////////////////////////////////////////////////////////////////////////////////
43
44/// Special version of `#[derive(..)]` macro. Works just like the standard one, except it
45/// will de-duplicate the derives expanded from [`Config`] and explicit ones.
46///
47/// Thus declaration such as `#[setty::derive(Config, Clone, serde::Deserialize)]` will
48/// always derive `Clone` and `Deserialize` even if those are not configured via top-level features,
49/// and will not duplicate implementations if those features were enabled.
50#[proc_macro_attribute]
51pub fn derive(
52    attr: proc_macro::TokenStream,
53    item: proc_macro::TokenStream,
54) -> proc_macro::TokenStream {
55    match derive::derive_impl(attr.into(), item.into()) {
56        Ok(output) => proc_macro::TokenStream::from(output),
57        Err(err) => err.to_compile_error().into(),
58    }
59}
60
61/////////////////////////////////////////////////////////////////////////////////////////
62
63#[proc_macro_attribute]
64pub fn __erase(
65    _attr: proc_macro::TokenStream,
66    _item: proc_macro::TokenStream,
67) -> proc_macro::TokenStream {
68    proc_macro::TokenStream::new()
69}
70
71/////////////////////////////////////////////////////////////////////////////////////////