diff options
author | HampusM <hampus@hampusmat.com> | 2023-09-27 20:22:46 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-09-27 20:22:46 +0200 |
commit | 6533246020c9dcc1acfc02b1e5ed74fbbce4ea1f (patch) | |
tree | 5a852f3cd7816b46d9119362b42568d21f207bb5 /macros | |
parent | 6a2c4025080776a386ab6dcd074fd05ae7319f9c (diff) |
fix: set injectable macro dummies directly after parsing input
Diffstat (limited to 'macros')
-rw-r--r-- | macros/src/injectable/dummy.rs | 58 | ||||
-rw-r--r-- | macros/src/injectable/implementation.rs | 69 | ||||
-rw-r--r-- | macros/src/injectable/mod.rs | 1 | ||||
-rw-r--r-- | macros/src/lib.rs | 53 |
4 files changed, 102 insertions, 79 deletions
diff --git a/macros/src/injectable/dummy.rs b/macros/src/injectable/dummy.rs new file mode 100644 index 0000000..3ace798 --- /dev/null +++ b/macros/src/injectable/dummy.rs @@ -0,0 +1,58 @@ +use quote::quote; +use syn::{Generics, Type}; + +#[cfg(not(tarpaulin_include))] +pub fn expand_dummy_blocking_impl( + generics: &Generics, + self_type: &Type, +) -> proc_macro2::TokenStream +{ + quote! { + impl #generics syrette::interfaces::injectable::Injectable< + syrette::di_container::blocking::DIContainer, + > for #self_type + { + fn resolve( + _: &syrette::di_container::blocking::DIContainer, + _: syrette::dependency_history::DependencyHistory + ) -> Result< + syrette::ptr::TransientPtr<Self>, + syrette::errors::injectable::InjectableError> + { + unimplemented!(); + } + } + } +} + +#[cfg(not(tarpaulin_include))] +#[cfg(feature = "async")] +pub fn expand_dummy_async_impl( + generics: &Generics, + self_type: &Type, +) -> proc_macro2::TokenStream +{ + quote! { + impl #generics syrette::interfaces::async_injectable::AsyncInjectable< + syrette::di_container::asynchronous::AsyncDIContainer, + > for #self_type + { + fn resolve<'di_container, 'fut>( + _: &'di_container syrette::di_container::asynchronous::AsyncDIContainer, + _: syrette::dependency_history::DependencyHistory + ) -> syrette::future::BoxFuture< + 'fut, + Result< + syrette::ptr::TransientPtr<Self>, + syrette::errors::injectable::InjectableError + > + > + where + Self: Sized + 'fut, + 'di_container: 'fut + { + unimplemented!(); + } + } + } +} diff --git a/macros/src/injectable/implementation.rs b/macros/src/injectable/implementation.rs index 98e0abf..34c5437 100644 --- a/macros/src/injectable/implementation.rs +++ b/macros/src/injectable/implementation.rs @@ -1,10 +1,9 @@ use std::error::Error; -use proc_macro2::{Ident, Span, TokenStream}; +use proc_macro2::{Ident, Span}; use quote::{format_ident, quote, ToTokens}; use syn::spanned::Spanned; use syn::{ - parse2, parse_str, ExprMethodCall, FnArg, @@ -37,17 +36,11 @@ pub struct InjectableImpl<Dep: IDependency> impl<Dep: IDependency> InjectableImpl<Dep> { #[cfg(not(tarpaulin_include))] - pub fn parse( - input: TokenStream, + pub fn new( + mut item_impl: ItemImpl, constructor: &Ident, ) -> Result<Self, InjectableImplError> { - let mut item_impl = parse2::<ItemImpl>(input).map_err(|err| { - InjectableImplError::NotAImplementation { - err_span: err.span(), - } - })?; - if let Some((_, trait_path, _)) = item_impl.trait_ { return Err(InjectableImplError::TraitImpl { trait_path_span: trait_path.span(), @@ -313,62 +306,6 @@ impl<Dep: IDependency> InjectableImpl<Dep> } } - #[cfg(not(tarpaulin_include))] - pub fn expand_dummy_blocking_impl(&self) -> proc_macro2::TokenStream - { - let generics = &self.generics; - let self_type = &self.self_type; - - quote! { - impl #generics syrette::interfaces::injectable::Injectable< - syrette::di_container::blocking::DIContainer, - > for #self_type - { - fn resolve( - _: &syrette::di_container::blocking::DIContainer, - _: syrette::dependency_history::DependencyHistory - ) -> Result< - syrette::ptr::TransientPtr<Self>, - syrette::errors::injectable::InjectableError> - { - unimplemented!(); - } - } - } - } - - #[cfg(not(tarpaulin_include))] - pub fn expand_dummy_async_impl(&self) -> proc_macro2::TokenStream - { - let generics = &self.generics; - let self_type = &self.self_type; - - quote! { - impl #generics syrette::interfaces::async_injectable::AsyncInjectable< - syrette::di_container::asynchronous::AsyncDIContainer, - > for #self_type - { - fn resolve<'di_container, 'fut>( - _: &'di_container - syrette::di_container::asynchronous::AsyncDIContainer, - _: syrette::dependency_history::DependencyHistory - ) -> syrette::future::BoxFuture< - 'fut, - Result< - syrette::ptr::TransientPtr<Self>, - syrette::errors::injectable::InjectableError - > - > - where - Self: Sized + 'fut, - 'di_container: 'fut - { - unimplemented!(); - } - } - } - } - fn create_get_dep_method_calls( dependencies: &[Dep], is_async: bool, diff --git a/macros/src/injectable/mod.rs b/macros/src/injectable/mod.rs index b713aeb..0e6ddfd 100644 --- a/macros/src/injectable/mod.rs +++ b/macros/src/injectable/mod.rs @@ -1,4 +1,5 @@ pub mod dependency; +pub mod dummy; pub mod implementation; pub mod macro_args; pub mod named_attr_input; diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 09f54ff..78895be 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -7,15 +7,24 @@ use proc_macro::TokenStream; use proc_macro_error::{proc_macro_error, set_dummy, ResultExt}; -use quote::quote; +use quote::{format_ident, quote}; use syn::punctuated::Punctuated; use syn::token::Dyn; -use syn::{parse, TraitBound, TraitBoundModifier, Type, TypeParamBound, TypeTraitObject}; +use syn::{ + parse, + ItemImpl, + TraitBound, + TraitBoundModifier, + Type, + TypeParamBound, + TypeTraitObject, +}; use crate::caster::generate_caster; use crate::declare_interface_args::DeclareInterfaceArgs; use crate::injectable::dependency::Dependency; -use crate::injectable::implementation::InjectableImpl; +use crate::injectable::dummy::expand_dummy_blocking_impl; +use crate::injectable::implementation::{InjectableImpl, InjectableImplError}; use crate::injectable::macro_args::InjectableMacroArgs; use crate::macro_flag::MacroFlag; @@ -34,6 +43,9 @@ mod fn_trait; #[cfg(test)] mod test_utils; +#[cfg(feature = "async")] +use crate::injectable::dummy::expand_dummy_async_impl; + #[allow(dead_code)] const PACKAGE_VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -147,11 +159,32 @@ const PACKAGE_VERSION: &str = env!("CARGO_PKG_VERSION"); #[proc_macro_attribute] pub fn injectable(args_stream: TokenStream, input_stream: TokenStream) -> TokenStream { - use quote::format_ident; + let item_impl = parse::<ItemImpl>(input_stream) + .map_err(|err| InjectableImplError::NotAImplementation { + err_span: err.span(), + }) + .unwrap_or_abort(); - let input_stream: proc_macro2::TokenStream = input_stream.into(); + let dummy_blocking_impl = + expand_dummy_blocking_impl(&item_impl.generics, &item_impl.self_ty); + + #[cfg(not(feature = "async"))] + set_dummy(quote! { + #item_impl + #dummy_blocking_impl + }); - set_dummy(input_stream.clone()); + #[cfg(feature = "async")] + { + let dummy_async_impl = + expand_dummy_async_impl(&item_impl.generics, &item_impl.self_ty); + + set_dummy(quote! { + #item_impl + #dummy_blocking_impl + #dummy_async_impl + }); + } let args = parse::<InjectableMacroArgs>(args_stream).unwrap_or_abort(); @@ -200,13 +233,7 @@ pub fn injectable(args_stream: TokenStream, input_stream: TokenStream) -> TokenS } let injectable_impl = - InjectableImpl::<Dependency>::parse(input_stream, &constructor).unwrap_or_abort(); - - set_dummy(if is_async { - injectable_impl.expand_dummy_async_impl() - } else { - injectable_impl.expand_dummy_blocking_impl() - }); + InjectableImpl::<Dependency>::new(item_impl, &constructor).unwrap_or_abort(); injectable_impl.validate().unwrap_or_abort(); |