From c1e682c25c24be3174d44ceb95b0537c38299d0c Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 12 Sep 2022 20:22:13 +0200 Subject: feat!: allow factories access to DI container BREAKING CHANGE: Factory types should now be written with the Fn trait instead of the IFactory trait and the to_factory & to_default_factory methods of BindingBuilder now expect a function returning a factory function --- macros/src/fn_trait.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 macros/src/fn_trait.rs (limited to 'macros/src/fn_trait.rs') diff --git a/macros/src/fn_trait.rs b/macros/src/fn_trait.rs new file mode 100644 index 0000000..f9b3514 --- /dev/null +++ b/macros/src/fn_trait.rs @@ -0,0 +1,68 @@ +use quote::ToTokens; +use syn::parse::Parse; +use syn::punctuated::Punctuated; +use syn::token::Paren; +use syn::{parenthesized, Ident, Token, Type}; + +/// A function trait. `dyn Fn(u32) -> String` +#[derive(Debug, Clone)] +pub struct FnTrait +{ + pub dyn_token: Token![dyn], + pub trait_ident: Ident, + pub paren_token: Paren, + pub inputs: Punctuated, + pub r_arrow_token: Token![->], + pub output: Type, +} + +impl Parse for FnTrait +{ + fn parse(input: syn::parse::ParseStream) -> syn::Result + { + let dyn_token = input.parse::()?; + + let trait_ident = input.parse::()?; + + if trait_ident.to_string().as_str() != "Fn" { + return Err(syn::Error::new(trait_ident.span(), "Expected 'Fn'")); + } + + let content; + + let paren_token = parenthesized!(content in input); + + let inputs = content.parse_terminated(Type::parse)?; + + let r_arrow_token = input.parse::]>()?; + + let output = input.parse::()?; + + Ok(Self { + dyn_token, + trait_ident, + paren_token, + inputs, + r_arrow_token, + output, + }) + } +} + +impl ToTokens for FnTrait +{ + fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) + { + self.dyn_token.to_tokens(tokens); + + self.trait_ident.to_tokens(tokens); + + self.paren_token.surround(tokens, |tokens_inner| { + self.inputs.to_tokens(tokens_inner); + }); + + self.r_arrow_token.to_tokens(tokens); + + self.output.to_tokens(tokens); + } +} -- cgit v1.2.3-18-g5258