aboutsummaryrefslogtreecommitdiff
path: root/example/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'example/src/main.rs')
-rw-r--r--example/src/main.rs145
1 files changed, 8 insertions, 137 deletions
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>()?;