From 9e01cdf341a7866180b3a63d745f3b2d7578d28a Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 23 Oct 2022 18:12:23 +0200 Subject: refactor!: reduce DI container coupling BREAKING CHANGE: You now have to import the DI containers's interfaces to use the DI containers's methods --- src/test_utils.rs | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 162 insertions(+), 10 deletions(-) (limited to 'src/test_utils.rs') diff --git a/src/test_utils.rs b/src/test_utils.rs index 1fc7015..650e338 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -7,9 +7,9 @@ pub mod subjects use syrette_macros::declare_interface; + use crate::di_container::blocking::IDIContainer; use crate::interfaces::injectable::Injectable; use crate::ptr::TransientPtr; - use crate::DIContainer; pub trait IUserManager { @@ -45,10 +45,12 @@ pub mod subjects declare_interface!(UserManager -> IUserManager); - impl Injectable for UserManager + impl Injectable for UserManager + where + DIContainerType: IDIContainer, { fn resolve( - _di_container: &Rc, + _di_container: &Rc, _dependency_history: Vec<&'static str>, ) -> Result, crate::errors::injectable::InjectableError> where @@ -109,10 +111,12 @@ pub mod subjects declare_interface!(Number -> INumber); - impl Injectable for Number + impl Injectable for Number + where + DIContainerType: IDIContainer, { fn resolve( - _di_container: &Rc, + _di_container: &Rc, _dependency_history: Vec<&'static str>, ) -> Result, crate::errors::injectable::InjectableError> where @@ -134,9 +138,9 @@ pub mod subjects_async use async_trait::async_trait; use syrette_macros::declare_interface; + use crate::di_container::asynchronous::IAsyncDIContainer; use crate::interfaces::async_injectable::AsyncInjectable; use crate::ptr::TransientPtr; - use crate::AsyncDIContainer; pub trait IUserManager: Send + Sync { @@ -173,10 +177,12 @@ pub mod subjects_async declare_interface!(UserManager -> IUserManager); #[async_trait] - impl AsyncInjectable for UserManager + impl AsyncInjectable for UserManager + where + DIContainerType: IAsyncDIContainer, { async fn resolve( - _: &Arc, + _: &Arc, _dependency_history: Vec<&'static str>, ) -> Result, crate::errors::injectable::InjectableError> where @@ -238,10 +244,12 @@ pub mod subjects_async declare_interface!(Number -> INumber, async = true); #[async_trait] - impl AsyncInjectable for Number + impl AsyncInjectable for Number + where + DIContainerType: IAsyncDIContainer, { async fn resolve( - _: &Arc, + _: &Arc, _dependency_history: Vec<&'static str>, ) -> Result, crate::errors::injectable::InjectableError> where @@ -251,3 +259,147 @@ pub mod subjects_async } } } + +pub mod mocks +{ + #![allow(clippy::ref_option_ref)] // Caused by Mockall + #![allow(dead_code)] // Not all mock functions may be used + + use mockall::mock; + + pub mod blocking_di_container + { + use std::rc::Rc; + + use super::*; + use crate::di_container::blocking::binding::builder::BindingBuilder; + use crate::di_container::blocking::details::DIContainerInternals; + use crate::di_container::blocking::IDIContainer; + use crate::errors::di_container::DIContainerError; + use crate::provider::blocking::IProvider; + use crate::ptr::SomePtr; + + mock! { + pub DIContainer {} + + impl IDIContainer for DIContainer { + fn bind(self: &mut Rc) -> BindingBuilder + where + Interface: 'static + ?Sized; + + fn get(self: &Rc) -> Result, DIContainerError> + where + Interface: 'static + ?Sized; + + fn get_named( + self: &Rc, + name: &'static str, + ) -> Result, DIContainerError> + where + Interface: 'static + ?Sized; + + #[doc(hidden)] + fn get_bound( + self: &Rc, + dependency_history: Vec<&'static str>, + name: Option<&'static str>, + ) -> Result, DIContainerError> + where + Interface: 'static + ?Sized; + } + + impl DIContainerInternals for DIContainer + { + fn has_binding(self: &Rc, name: Option<&'static str>) -> bool + where + Interface: ?Sized + 'static; + + #[doc(hidden)] + fn set_binding( + self: &Rc, + name: Option<&'static str>, + provider: Box>, + ) where + Interface: 'static + ?Sized; + + fn remove_binding( + self: &Rc, + name: Option<&'static str>, + ) -> Option>> + where + Interface: 'static + ?Sized; + } + } + } + + #[cfg(feature = "async")] + pub mod async_di_container + { + use std::sync::Arc; + + use super::*; + use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder; + use crate::di_container::asynchronous::details::DIContainerInternals; + use crate::di_container::asynchronous::IAsyncDIContainer; + use crate::errors::async_di_container::AsyncDIContainerError; + use crate::provider::r#async::IAsyncProvider; + use crate::ptr::SomeThreadsafePtr; + + mock! { + pub AsyncDIContainer {} + + #[async_trait::async_trait] + impl IAsyncDIContainer for AsyncDIContainer { + fn bind(self: &mut Arc) -> AsyncBindingBuilder + where + Interface: 'static + ?Sized + Send + Sync; + + async fn get( + self: &Arc, + ) -> Result, AsyncDIContainerError> + where + Interface: 'static + ?Sized + Send + Sync; + + async fn get_named( + self: &Arc, + name: &'static str, + ) -> Result, AsyncDIContainerError> + where + Interface: 'static + ?Sized + Send + Sync; + + #[doc(hidden)] + async fn get_bound( + self: &Arc, + dependency_history: Vec<&'static str>, + name: Option<&'static str>, + ) -> Result, AsyncDIContainerError> + where + Interface: 'static + ?Sized + Send + Sync; + } + + #[async_trait::async_trait] + impl DIContainerInternals for AsyncDIContainer { + async fn has_binding( + self: &Arc, + name: Option<&'static str>, + ) -> bool + where + Interface: ?Sized + 'static; + + async fn set_binding( + self: &Arc, + name: Option<&'static str>, + provider: Box>, + ) where + Interface: 'static + ?Sized; + + async fn remove_binding( + self: &Arc, + name: Option<&'static str>, + ) -> Option>> + where + Interface: 'static + ?Sized; + } + } + } +} -- cgit v1.2.3-18-g5258