diff options
Diffstat (limited to 'example/src')
-rw-r--r-- | example/src/animals/cat.rs | 22 | ||||
-rw-r--r-- | example/src/animals/cow.rs | 24 | ||||
-rw-r--r-- | example/src/animals/dog.rs | 22 | ||||
-rw-r--r-- | example/src/animals/human.rs | 49 | ||||
-rw-r--r-- | example/src/animals/mod.rs | 4 | ||||
-rw-r--r-- | example/src/bootstrap.rs | 29 | ||||
-rw-r--r-- | example/src/interfaces/cat.rs | 4 | ||||
-rw-r--r-- | example/src/interfaces/cow.rs | 10 | ||||
-rw-r--r-- | example/src/interfaces/dog.rs | 4 | ||||
-rw-r--r-- | example/src/interfaces/human.rs | 4 | ||||
-rw-r--r-- | example/src/interfaces/mod.rs | 4 | ||||
-rw-r--r-- | example/src/main.rs | 145 |
12 files changed, 184 insertions, 137 deletions
diff --git a/example/src/animals/cat.rs b/example/src/animals/cat.rs new file mode 100644 index 0000000..153ad4b --- /dev/null +++ b/example/src/animals/cat.rs @@ -0,0 +1,22 @@ +use syrette::injectable; + +use crate::interfaces::cat::ICat; + +pub struct Cat {} + +#[injectable(ICat)] +impl Cat +{ + pub fn new() -> Self + { + Self {} + } +} + +impl ICat for Cat +{ + fn meow(&self) + { + println!("Meow!"); + } +} diff --git a/example/src/animals/cow.rs b/example/src/animals/cow.rs new file mode 100644 index 0000000..a75d750 --- /dev/null +++ b/example/src/animals/cow.rs @@ -0,0 +1,24 @@ +use crate::interfaces::cow::ICow; + +pub struct Cow +{ + moo_cnt: i32, +} + +impl Cow +{ + pub fn new(moo_cnt: i32) -> Self + { + Self { moo_cnt } + } +} + +impl ICow for Cow +{ + fn moo(&self) + { + for _ in 0..self.moo_cnt { + println!("Moo"); + } + } +} diff --git a/example/src/animals/dog.rs b/example/src/animals/dog.rs new file mode 100644 index 0000000..84959c0 --- /dev/null +++ b/example/src/animals/dog.rs @@ -0,0 +1,22 @@ +use syrette::injectable; + +use crate::interfaces::dog::IDog; + +pub struct Dog {} + +#[injectable(IDog)] +impl Dog +{ + pub fn new() -> Self + { + Self {} + } +} + +impl IDog for Dog +{ + fn woof(&self) + { + println!("Woof!"); + } +} diff --git a/example/src/animals/human.rs b/example/src/animals/human.rs new file mode 100644 index 0000000..5bd2f8f --- /dev/null +++ b/example/src/animals/human.rs @@ -0,0 +1,49 @@ +use syrette::injectable; +use syrette::ptr::{FactoryPtr, InterfacePtr}; + +use crate::interfaces::cat::ICat; +use crate::interfaces::cow::{CowFactory, ICow}; +use crate::interfaces::dog::IDog; +use crate::interfaces::human::IHuman; + +pub struct Human +{ + dog: InterfacePtr<dyn IDog>, + cat: InterfacePtr<dyn ICat>, + cow_factory: FactoryPtr<CowFactory>, +} + +#[injectable(IHuman)] +impl Human +{ + pub fn new( + dog: InterfacePtr<dyn IDog>, + cat: InterfacePtr<dyn ICat>, + cow_factory: FactoryPtr<CowFactory>, + ) -> Self + { + Self { + dog, + cat, + cow_factory, + } + } +} + +impl IHuman for Human +{ + fn make_pets_make_sounds(&self) + { + println!("Hi doggy!"); + + self.dog.woof(); + + println!("Hi kitty!"); + + self.cat.meow(); + + let cow: Box<dyn ICow> = (self.cow_factory)(3); + + cow.moo(); + } +} diff --git a/example/src/animals/mod.rs b/example/src/animals/mod.rs new file mode 100644 index 0000000..6511d17 --- /dev/null +++ b/example/src/animals/mod.rs @@ -0,0 +1,4 @@ +pub mod cat; +pub mod cow; +pub mod dog; +pub mod human; diff --git a/example/src/bootstrap.rs b/example/src/bootstrap.rs new file mode 100644 index 0000000..a1a7b05 --- /dev/null +++ b/example/src/bootstrap.rs @@ -0,0 +1,29 @@ +use syrette::DIContainer; + +// Concrete implementations +use crate::animals::cat::Cat; +use crate::animals::cow::Cow; +use crate::animals::dog::Dog; +use crate::animals::human::Human; +// +// Interfaces +use crate::interfaces::cat::ICat; +use crate::interfaces::cow::{CowFactory, ICow}; +use crate::interfaces::dog::IDog; +use crate::interfaces::human::IHuman; + +pub fn bootstrap() -> DIContainer +{ + let mut di_container: DIContainer = DIContainer::new(); + + di_container.bind::<dyn IDog>().to::<Dog>(); + di_container.bind::<dyn ICat>().to::<Cat>(); + di_container.bind::<dyn IHuman>().to::<Human>(); + + di_container.bind::<CowFactory>().to_factory(&|moo_cnt| { + let cow: Box<dyn ICow> = Box::new(Cow::new(moo_cnt)); + cow + }); + + di_container +} diff --git a/example/src/interfaces/cat.rs b/example/src/interfaces/cat.rs new file mode 100644 index 0000000..8650802 --- /dev/null +++ b/example/src/interfaces/cat.rs @@ -0,0 +1,4 @@ +pub trait ICat +{ + fn meow(&self); +} diff --git a/example/src/interfaces/cow.rs b/example/src/interfaces/cow.rs new file mode 100644 index 0000000..59ce7b1 --- /dev/null +++ b/example/src/interfaces/cow.rs @@ -0,0 +1,10 @@ +use syrette::factory; +use syrette::interfaces::factory::IFactory; + +pub trait ICow +{ + fn moo(&self); +} + +#[factory] +pub type CowFactory = dyn IFactory<(i32,), dyn ICow>; diff --git a/example/src/interfaces/dog.rs b/example/src/interfaces/dog.rs new file mode 100644 index 0000000..4211c27 --- /dev/null +++ b/example/src/interfaces/dog.rs @@ -0,0 +1,4 @@ +pub trait IDog +{ + fn woof(&self); +} diff --git a/example/src/interfaces/human.rs b/example/src/interfaces/human.rs new file mode 100644 index 0000000..dc20194 --- /dev/null +++ b/example/src/interfaces/human.rs @@ -0,0 +1,4 @@ +pub trait IHuman +{ + fn make_pets_make_sounds(&self); +} diff --git a/example/src/interfaces/mod.rs b/example/src/interfaces/mod.rs new file mode 100644 index 0000000..6511d17 --- /dev/null +++ b/example/src/interfaces/mod.rs @@ -0,0 +1,4 @@ +pub mod cat; +pub mod cow; +pub mod dog; +pub mod human; diff --git a/example/src/main.rs b/example/src/main.rs index d79a030..956b79e 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -1,151 +1,22 @@ #![deny(clippy::all)] #![deny(clippy::pedantic)] +#![allow(clippy::module_name_repetitions)] use syrette::errors::di_container::DIContainerError; -use syrette::interfaces::factory::IFactory; -use syrette::ptr::{FactoryPtr, InterfacePtr}; -use syrette::{factory, injectable, DIContainer}; -trait IDog -{ - fn woof(&self); -} - -struct Dog {} - -#[injectable(IDog)] -impl Dog -{ - fn new() -> Self - { - Self {} - } -} - -impl IDog for Dog -{ - fn woof(&self) - { - println!("Woof!"); - } -} - -trait ICat -{ - fn meow(&self); -} - -struct Cat {} - -#[injectable(ICat)] -impl Cat -{ - fn new() -> Self - { - Self {} - } -} - -impl ICat for Cat -{ - fn meow(&self) - { - println!("Meow!"); - } -} - -trait ICow -{ - fn moo(&self); -} - -struct Cow -{ - moo_cnt: i32, -} +mod animals; +mod bootstrap; +mod interfaces; -impl Cow -{ - fn new(moo_cnt: i32) -> Self - { - Self { moo_cnt } - } -} - -impl ICow for Cow -{ - fn moo(&self) - { - for _ in 0..self.moo_cnt { - println!("Moo"); - } - } -} - -#[factory] -type CowFactory = dyn IFactory<(i32,), dyn ICow>; - -trait IHuman -{ - fn make_pets_make_sounds(&self); -} - -struct Human -{ - dog: InterfacePtr<dyn IDog>, - cat: InterfacePtr<dyn ICat>, - cow_factory: FactoryPtr<CowFactory>, -} - -#[injectable(IHuman)] -impl Human -{ - fn new( - dog: InterfacePtr<dyn IDog>, - cat: InterfacePtr<dyn ICat>, - cow_factory: FactoryPtr<CowFactory>, - ) -> Self - { - Self { - dog, - cat, - cow_factory, - } - } -} - -impl IHuman for Human -{ - fn make_pets_make_sounds(&self) - { - println!("Hi doggy!"); - - self.dog.woof(); - - println!("Hi kitty!"); - - self.cat.meow(); - - let cow: Box<dyn ICow> = (self.cow_factory)(3); - - cow.moo(); - } -} +use bootstrap::bootstrap; +use interfaces::dog::IDog; +use interfaces::human::IHuman; fn main() -> error_stack::Result<(), DIContainerError> { println!("Hello, world!"); - let mut di_container: DIContainer = DIContainer::new(); - - di_container.bind::<dyn IDog>().to::<Dog>(); - di_container.bind::<dyn ICat>().to::<Cat>(); - di_container.bind::<dyn IHuman>().to::<Human>(); - - di_container.bind::<CowFactory>().to_factory(&|moo_cnt| { - let cow: Box<dyn ICow> = Box::new(Cow::new(moo_cnt)); - cow - }); + let di_container = bootstrap(); let dog = di_container.get::<dyn IDog>()?; |