wire_framework_derive/
lib.rs

1extern crate proc_macro;
2
3use proc_macro::TokenStream;
4use syn::{DeriveInput, parse_macro_input};
5
6use crate::macro_impl::{MacroImpl, MacroKind};
7
8pub(crate) mod helpers;
9mod labels;
10mod macro_impl;
11
12/// Derive macro for the `FromContext` trait.
13/// Allows to automatically fetch all the resources and tasks from the context.
14///
15/// See the trait documentation for more details.
16#[proc_macro_derive(FromContext, attributes(context))]
17pub fn from_context_derive(input: TokenStream) -> TokenStream {
18    // Parse the input tokens into a syntax tree
19    let input = parse_macro_input!(input as DeriveInput);
20    MacroImpl::parse(MacroKind::FromContext, input)
21        .and_then(|from_context| from_context.render())
22        .unwrap_or_else(syn::Error::into_compile_error)
23        .into()
24}
25
26/// Derive macro for the `IntoContext` trait.
27/// Allows to automatically insert all the resources in tasks created by the wiring layer
28/// into the context.
29///
30/// See the trait documentation for more details.
31#[proc_macro_derive(IntoContext, attributes(context))]
32pub fn into_context_derive(input: TokenStream) -> TokenStream {
33    // Parse the input tokens into a syntax tree
34    let input = parse_macro_input!(input as DeriveInput);
35    MacroImpl::parse(MacroKind::IntoContext, input)
36        .and_then(|from_context| from_context.render())
37        .unwrap_or_else(syn::Error::into_compile_error)
38        .into()
39}