From aa548ded39c7ba1927019c748c359523b21d59e8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 29 Oct 2022 14:38:51 +0200 Subject: refactor!: add dependency history type BREAKING CHANGE: Binding builders & configurators now take dependency history type arguments, the DetectedCircular variant of InjectableError now contains a dependency history field & the injectable traits take dependency history instead of a Vec --- src/test_utils.rs | 179 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 152 insertions(+), 27 deletions(-) (limited to 'src/test_utils.rs') diff --git a/src/test_utils.rs b/src/test_utils.rs index 650e338..b4ec951 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -7,6 +7,7 @@ pub mod subjects use syrette_macros::declare_interface; + use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::IDIContainer; use crate::interfaces::injectable::Injectable; use crate::ptr::TransientPtr; @@ -45,13 +46,15 @@ pub mod subjects declare_interface!(UserManager -> IUserManager); - impl Injectable for UserManager + impl + Injectable for UserManager where - DIContainerType: IDIContainer, + DIContainerType: IDIContainer, + DependencyHistoryType: IDependencyHistory, { fn resolve( _di_container: &Rc, - _dependency_history: Vec<&'static str>, + _dependency_history: DependencyHistoryType, ) -> Result, crate::errors::injectable::InjectableError> where Self: Sized, @@ -111,13 +114,15 @@ pub mod subjects declare_interface!(Number -> INumber); - impl Injectable for Number + impl + Injectable for Number where - DIContainerType: IDIContainer, + DIContainerType: IDIContainer, + DependencyHistoryType: IDependencyHistory, { fn resolve( _di_container: &Rc, - _dependency_history: Vec<&'static str>, + _dependency_history: DependencyHistoryType, ) -> Result, crate::errors::injectable::InjectableError> where Self: Sized, @@ -138,6 +143,7 @@ pub mod subjects_async use async_trait::async_trait; use syrette_macros::declare_interface; + use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::IAsyncDIContainer; use crate::interfaces::async_injectable::AsyncInjectable; use crate::ptr::TransientPtr; @@ -177,13 +183,15 @@ pub mod subjects_async declare_interface!(UserManager -> IUserManager); #[async_trait] - impl AsyncInjectable for UserManager + impl + AsyncInjectable for UserManager where - DIContainerType: IAsyncDIContainer, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, { async fn resolve( _: &Arc, - _dependency_history: Vec<&'static str>, + _dependency_history: DependencyHistoryType, ) -> Result, crate::errors::injectable::InjectableError> where Self: Sized, @@ -244,13 +252,15 @@ pub mod subjects_async declare_interface!(Number -> INumber, async = true); #[async_trait] - impl AsyncInjectable for Number + impl + AsyncInjectable for Number where - DIContainerType: IAsyncDIContainer, + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync + 'static, { async fn resolve( _: &Arc, - _dependency_history: Vec<&'static str>, + _dependency_history: DependencyHistoryType, ) -> Result, crate::errors::injectable::InjectableError> where Self: Sized, @@ -272,6 +282,7 @@ pub mod mocks use std::rc::Rc; use super::*; + use crate::dependency_history::IDependencyHistory; use crate::di_container::blocking::binding::builder::BindingBuilder; use crate::di_container::blocking::details::DIContainerInternals; use crate::di_container::blocking::IDIContainer; @@ -280,10 +291,18 @@ pub mod mocks use crate::ptr::SomePtr; mock! { - pub DIContainer {} - - impl IDIContainer for DIContainer { - fn bind(self: &mut Rc) -> BindingBuilder + pub DIContainer + where + DependencyHistoryType: IDependencyHistory + 'static + {} + + impl IDIContainer for + DIContainer + where + DependencyHistoryType: IDependencyHistory + 'static + { + fn bind(self: &mut Rc) -> + BindingBuilder where Interface: 'static + ?Sized; @@ -301,14 +320,18 @@ pub mod mocks #[doc(hidden)] fn get_bound( self: &Rc, - dependency_history: Vec<&'static str>, + dependency_history: DependencyHistoryType, name: Option<&'static str>, ) -> Result, DIContainerError> where Interface: 'static + ?Sized; } - impl DIContainerInternals for DIContainer + impl DIContainerInternals< + DependencyHistoryType + > for DIContainer + where + DependencyHistoryType: IDependencyHistory { fn has_binding(self: &Rc, name: Option<&'static str>) -> bool where @@ -318,14 +341,14 @@ pub mod mocks fn set_binding( self: &Rc, name: Option<&'static str>, - provider: Box>, + provider: Box>, ) where Interface: 'static + ?Sized; fn remove_binding( self: &Rc, name: Option<&'static str>, - ) -> Option>> + ) -> Option>> where Interface: 'static + ?Sized; } @@ -338,6 +361,7 @@ pub mod mocks use std::sync::Arc; use super::*; + use crate::dependency_history::IDependencyHistory; use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder; use crate::di_container::asynchronous::details::DIContainerInternals; use crate::di_container::asynchronous::IAsyncDIContainer; @@ -346,11 +370,20 @@ pub mod mocks use crate::ptr::SomeThreadsafePtr; mock! { - pub AsyncDIContainer {} + pub AsyncDIContainer + where + DependencyHistoryType: IDependencyHistory + 'static + Send + Sync + {} #[async_trait::async_trait] - impl IAsyncDIContainer for AsyncDIContainer { - fn bind(self: &mut Arc) -> AsyncBindingBuilder + impl IAsyncDIContainer< + DependencyHistoryType + > for AsyncDIContainer + where + DependencyHistoryType: IDependencyHistory + 'static + Send + Sync + { + fn bind(self: &mut Arc) -> + AsyncBindingBuilder where Interface: 'static + ?Sized + Send + Sync; @@ -370,7 +403,7 @@ pub mod mocks #[doc(hidden)] async fn get_bound( self: &Arc, - dependency_history: Vec<&'static str>, + dependency_history: DependencyHistoryType, name: Option<&'static str>, ) -> Result, AsyncDIContainerError> where @@ -378,7 +411,12 @@ pub mod mocks } #[async_trait::async_trait] - impl DIContainerInternals for AsyncDIContainer { + impl DIContainerInternals< + DependencyHistoryType + > for AsyncDIContainer + where + DependencyHistoryType: IDependencyHistory + 'static + Send + Sync + { async fn has_binding( self: &Arc, name: Option<&'static str>, @@ -389,17 +427,104 @@ pub mod mocks async fn set_binding( self: &Arc, name: Option<&'static str>, - provider: Box>, + provider: Box>, ) where Interface: 'static + ?Sized; async fn remove_binding( self: &Arc, name: Option<&'static str>, - ) -> Option>> + ) -> Option>> where Interface: 'static + ?Sized; } } } + + #[cfg(feature = "async")] + pub mod async_provider + { + use std::sync::Arc; + + use async_trait::async_trait; + + use super::*; + use crate::dependency_history::IDependencyHistory; + use crate::di_container::asynchronous::IAsyncDIContainer; + use crate::errors::injectable::InjectableError; + use crate::provider::r#async::{AsyncProvidable, IAsyncProvider}; + + mock! { + pub AsyncProvider< + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync + > {} + + #[async_trait] + impl< + DIContainerType: IAsyncDIContainer, + DependencyHistoryType: IDependencyHistory + Send + Sync + > IAsyncProvider for AsyncProvider< + DIContainerType, + DependencyHistoryType + > + { + async fn provide( + &self, + di_container: &Arc, + dependency_history: DependencyHistoryType + ) -> Result, InjectableError>; + + fn do_clone(&self) -> + Box>; + } + } + } + + pub mod blocking_provider + { + use std::rc::Rc; + + use super::*; + use crate::dependency_history::IDependencyHistory; + use crate::di_container::blocking::IDIContainer; + use crate::errors::injectable::InjectableError; + use crate::provider::blocking::{IProvider, Providable}; + + mock! { + pub Provider + where + DIContainerType: IDIContainer, + DependencyHistoryType: IDependencyHistory, + {} + + impl IProvider< + DIContainerType, + DependencyHistoryType + > for Provider + where + DIContainerType: IDIContainer, + DependencyHistoryType: IDependencyHistory + { + fn provide( + &self, + di_container: &Rc, + dependency_history: DependencyHistoryType, + ) -> Result, InjectableError>; + } + } + } + + mock! { + pub DependencyHistory {} + + impl crate::dependency_history::IDependencyHistory for DependencyHistory + { + fn push(&mut self); + + fn contains(&self) -> bool; + } + + impl crate::dependency_history::private::Sealed for DependencyHistory {} + } } -- cgit v1.2.3-18-g5258