aboutsummaryrefslogtreecommitdiff
path: root/src/di_container.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/di_container.rs')
-rw-r--r--src/di_container.rs253
1 files changed, 96 insertions, 157 deletions
diff --git a/src/di_container.rs b/src/di_container.rs
index 22ae3ba..5ee1c52 100644
--- a/src/di_container.rs
+++ b/src/di_container.rs
@@ -312,8 +312,6 @@ impl Default for DIContainer
#[cfg(test)]
mod tests
{
- use std::fmt::Debug;
-
use mockall::mock;
use super::*;
@@ -321,17 +319,34 @@ mod tests
use crate::provider::IProvider;
use crate::ptr::TransientPtr;
- #[test]
- fn can_bind_to()
+ mod subjects
{
- trait IUserManager
+ //! Test subjects.
+
+ use std::fmt::Debug;
+
+ use syrette_macros::declare_interface;
+
+ use super::DIContainer;
+ use crate::interfaces::injectable::Injectable;
+ use crate::ptr::TransientPtr;
+
+ pub trait IUserManager
{
fn add_user(&self, user_id: i128);
fn remove_user(&self, user_id: i128);
}
- struct UserManager {}
+ pub struct UserManager {}
+
+ impl UserManager
+ {
+ pub fn new() -> Self
+ {
+ Self {}
+ }
+ }
impl IUserManager for UserManager
{
@@ -346,6 +361,10 @@ mod tests
}
}
+ use crate as syrette;
+
+ declare_interface!(UserManager -> IUserManager);
+
impl Injectable for UserManager
{
fn resolve(
@@ -358,45 +377,62 @@ mod tests
where
Self: Sized,
{
- Ok(TransientPtr::new(Self {}))
+ Ok(TransientPtr::new(Self::new()))
}
}
- let mut di_container: DIContainer = DIContainer::new();
-
- assert_eq!(di_container.bindings.count(), 0);
+ pub trait INumber
+ {
+ fn get(&self) -> i32;
- di_container.bind::<dyn IUserManager>().to::<UserManager>();
+ fn set(&mut self, number: i32);
+ }
- assert_eq!(di_container.bindings.count(), 1);
- }
+ impl PartialEq for dyn INumber
+ {
+ fn eq(&self, other: &Self) -> bool
+ {
+ self.get() == other.get()
+ }
+ }
- #[test]
- fn can_bind_to_singleton() -> error_stack::Result<(), BindingBuilderError>
- {
- trait IUserManager
+ impl Debug for dyn INumber
{
- fn add_user(&self, user_id: i128);
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
+ {
+ f.write_str(format!("{}", self.get()).as_str())
+ }
+ }
- fn remove_user(&self, user_id: i128);
+ pub struct Number
+ {
+ pub num: i32,
}
- struct UserManager {}
+ impl Number
+ {
+ pub fn new() -> Self
+ {
+ Self { num: 0 }
+ }
+ }
- impl IUserManager for UserManager
+ impl INumber for Number
{
- fn add_user(&self, _user_id: i128)
+ fn get(&self) -> i32
{
- // ...
+ self.num
}
- fn remove_user(&self, _user_id: i128)
+ fn set(&mut self, number: i32)
{
- // ...
+ self.num = number;
}
}
- impl Injectable for UserManager
+ declare_interface!(Number -> INumber);
+
+ impl Injectable for Number
{
fn resolve(
_di_container: &DIContainer,
@@ -408,67 +444,55 @@ mod tests
where
Self: Sized,
{
- Ok(TransientPtr::new(Self {}))
+ Ok(TransientPtr::new(Self::new()))
}
}
+ }
+ #[test]
+ fn can_bind_to()
+ {
let mut di_container: DIContainer = DIContainer::new();
assert_eq!(di_container.bindings.count(), 0);
di_container
- .bind::<dyn IUserManager>()
- .to_singleton::<UserManager>()?;
+ .bind::<dyn subjects::IUserManager>()
+ .to::<subjects::UserManager>();
assert_eq!(di_container.bindings.count(), 1);
-
- Ok(())
}
#[test]
- #[cfg(feature = "factory")]
- fn can_bind_to_factory()
+ fn can_bind_to_singleton() -> error_stack::Result<(), BindingBuilderError>
{
- trait IUserManager
- {
- fn add_user(&self, user_id: i128);
-
- fn remove_user(&self, user_id: i128);
- }
+ let mut di_container: DIContainer = DIContainer::new();
- struct UserManager {}
+ assert_eq!(di_container.bindings.count(), 0);
- impl UserManager
- {
- fn new() -> Self
- {
- Self {}
- }
- }
+ di_container
+ .bind::<dyn subjects::IUserManager>()
+ .to_singleton::<subjects::UserManager>()?;
- impl IUserManager for UserManager
- {
- fn add_user(&self, _user_id: i128)
- {
- // ...
- }
+ assert_eq!(di_container.bindings.count(), 1);
- fn remove_user(&self, _user_id: i128)
- {
- // ...
- }
- }
+ Ok(())
+ }
+ #[test]
+ #[cfg(feature = "factory")]
+ fn can_bind_to_factory()
+ {
type IUserManagerFactory =
- dyn crate::interfaces::factory::IFactory<(), dyn IUserManager>;
+ dyn crate::interfaces::factory::IFactory<(), dyn subjects::IUserManager>;
let mut di_container: DIContainer = DIContainer::new();
assert_eq!(di_container.bindings.count(), 0);
di_container.bind::<IUserManagerFactory>().to_factory(&|| {
- let user_manager: TransientPtr<dyn IUserManager> =
- TransientPtr::new(UserManager::new());
+ let user_manager: TransientPtr<dyn subjects::IUserManager> =
+ TransientPtr::new(subjects::UserManager::new());
user_manager
});
@@ -479,40 +503,6 @@ mod tests
#[test]
fn can_get() -> error_stack::Result<(), DIContainerError>
{
- trait IUserManager
- {
- fn add_user(&self, user_id: i128);
-
- fn remove_user(&self, user_id: i128);
- }
-
- struct UserManager {}
-
- use crate as syrette;
- use crate::injectable;
-
- #[injectable(IUserManager)]
- impl UserManager
- {
- fn new() -> Self
- {
- Self {}
- }
- }
-
- impl IUserManager for UserManager
- {
- fn add_user(&self, _user_id: i128)
- {
- // ...
- }
-
- fn remove_user(&self, _user_id: i128)
- {
- // ...
- }
- }
-
mock! {
Provider {}
@@ -531,14 +521,16 @@ mod tests
let mut mock_provider = MockProvider::new();
mock_provider.expect_provide().returning(|_, _| {
- Ok(Providable::Transient(TransientPtr::new(UserManager::new())))
+ Ok(Providable::Transient(TransientPtr::new(
+ subjects::UserManager::new(),
+ )))
});
di_container
.bindings
- .set::<dyn IUserManager>(Box::new(mock_provider));
+ .set::<dyn subjects::IUserManager>(Box::new(mock_provider));
- di_container.get::<dyn IUserManager>()?;
+ di_container.get::<dyn subjects::IUserManager>()?;
Ok(())
}
@@ -546,59 +538,6 @@ mod tests
#[test]
fn can_get_singleton() -> error_stack::Result<(), DIContainerError>
{
- trait INumber
- {
- fn get(&self) -> i32;
-
- fn set(&mut self, number: i32);
- }
-
- impl PartialEq for dyn INumber
- {
- fn eq(&self, other: &Self) -> bool
- {
- self.get() == other.get()
- }
- }
-
- impl Debug for dyn INumber
- {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
- {
- f.write_str(format!("{}", self.get()).as_str())
- }
- }
-
- struct Number
- {
- num: i32,
- }
-
- use crate as syrette;
- use crate::injectable;
-
- #[injectable(INumber)]
- impl Number
- {
- fn new() -> Self
- {
- Self { num: 0 }
- }
- }
-
- impl INumber for Number
- {
- fn get(&self) -> i32
- {
- self.num
- }
-
- fn set(&mut self, number: i32)
- {
- self.num = number;
- }
- }
-
mock! {
Provider {}
@@ -616,9 +555,9 @@ mod tests
let mut mock_provider = MockProvider::new();
- let mut singleton = SingletonPtr::new(Number::new());
+ let mut singleton = SingletonPtr::new(subjects::Number::new());
- SingletonPtr::get_mut(&mut singleton).unwrap().set(2820);
+ SingletonPtr::get_mut(&mut singleton).unwrap().num = 2820;
mock_provider
.expect_provide()
@@ -626,13 +565,13 @@ mod tests
di_container
.bindings
- .set::<dyn INumber>(Box::new(mock_provider));
+ .set::<dyn subjects::INumber>(Box::new(mock_provider));
- let first_number_rc = di_container.get_singleton::<dyn INumber>()?;
+ let first_number_rc = di_container.get_singleton::<dyn subjects::INumber>()?;
assert_eq!(first_number_rc.get(), 2820);
- let second_number_rc = di_container.get_singleton::<dyn INumber>()?;
+ let second_number_rc = di_container.get_singleton::<dyn subjects::INumber>()?;
assert_eq!(first_number_rc.as_ref(), second_number_rc.as_ref());
@@ -702,7 +641,7 @@ mod tests
let mut mock_provider = MockProvider::new();
- mock_provider.expect_provide().returning(|_| {
+ mock_provider.expect_provide().returning(|_, _| {
Ok(Providable::Factory(crate::ptr::FactoryPtr::new(
CastableFactory::new(&|users| {
let user_manager: TransientPtr<dyn IUserManager> =