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
use darling::FromDeriveInput;
use quote::quote;
use syn::{parse_macro_input, DeriveInput, Ident, Path};

#[derive(Debug, FromDeriveInput)]
#[darling(attributes(context))]
struct ContextDeriveInput {
  ident: Ident,
  state: Path,
}

/// Implements [Context](https://docs.rs/qcontext/latest/qcontext/trait.Context.html)
///
/// ## Attributes
///
/// * `#[context(state = "TCell<Context, usize>")]` sets [`Context::State`](https://docs.rs/qcontext/latest/qcontext/trait.Context.htmll#associatedtype.State)
#[proc_macro_derive(Context, attributes(context))]
pub fn derive_context(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
  let input = parse_macro_input!(input as DeriveInput);

  let ContextDeriveInput { ident, state } = match ContextDeriveInput::from_derive_input(&input) {
    Ok(input) => input,
    Err(err) => return err.write_errors().into(),
  };

  let expanded = quote! {
    impl qcontext::Context for #ident {
      type State = #state;

      fn context() -> &'static qcontext::Lazy<Self::State> {
        static CONTEXT: qcontext::Lazy<#state> = qcontext::Lazy::new();

        &CONTEXT
      }
    }
  };

  expanded.into()
}