From 8c66b98bca6ed0a2990903fe8e0ea72def5c7be8 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 21 Aug 2022 14:19:07 +0200 Subject: refactor!: change errors to be more sane BREAKING CHANGE: Major improvements have been made to error types and the error_stack crate is no longer used --- examples/unbound/animal_store.rs | 34 +++++++++++++++++++++++++++++ examples/unbound/animals/human.rs | 20 +++++++++-------- examples/unbound/bootstrap.rs | 7 ++++++ examples/unbound/interfaces/animal_store.rs | 11 ++++++++++ examples/unbound/interfaces/mod.rs | 1 + examples/unbound/main.rs | 9 ++++++-- 6 files changed, 71 insertions(+), 11 deletions(-) create mode 100644 examples/unbound/animal_store.rs create mode 100644 examples/unbound/interfaces/animal_store.rs (limited to 'examples/unbound') diff --git a/examples/unbound/animal_store.rs b/examples/unbound/animal_store.rs new file mode 100644 index 0000000..b2127e4 --- /dev/null +++ b/examples/unbound/animal_store.rs @@ -0,0 +1,34 @@ +use syrette::injectable; +use syrette::ptr::{SingletonPtr, TransientPtr}; + +use crate::interfaces::animal_store::IAnimalStore; +use crate::interfaces::cat::ICat; +use crate::interfaces::dog::IDog; + +pub struct AnimalStore +{ + dog: SingletonPtr, + cat: TransientPtr, +} + +#[injectable] +impl AnimalStore +{ + fn new(dog: SingletonPtr, cat: TransientPtr) -> Self + { + Self { dog, cat } + } +} + +impl IAnimalStore for AnimalStore +{ + fn get_dog(&self) -> SingletonPtr + { + self.dog.clone() + } + + fn get_cat(&self) -> &TransientPtr + { + &self.cat + } +} diff --git a/examples/unbound/animals/human.rs b/examples/unbound/animals/human.rs index d9b848b..56764d3 100644 --- a/examples/unbound/animals/human.rs +++ b/examples/unbound/animals/human.rs @@ -1,22 +1,20 @@ use syrette::injectable; -use syrette::ptr::{SingletonPtr, TransientPtr}; +use syrette::ptr::TransientPtr; -use crate::interfaces::cat::ICat; -use crate::interfaces::dog::IDog; +use crate::interfaces::animal_store::IAnimalStore; use crate::interfaces::human::IHuman; pub struct Human { - dog: SingletonPtr, - cat: TransientPtr, + animal_store: TransientPtr, } #[injectable(IHuman)] impl Human { - pub fn new(dog: SingletonPtr, cat: TransientPtr) -> Self + pub fn new(animal_store: TransientPtr) -> Self { - Self { dog, cat } + Self { animal_store } } } @@ -24,12 +22,16 @@ impl IHuman for Human { fn make_pets_make_sounds(&self) { + let dog = self.animal_store.get_dog(); + println!("Hi doggy!"); - self.dog.woof(); + dog.woof(); + + let cat = self.animal_store.get_cat(); println!("Hi kitty!"); - self.cat.meow(); + cat.meow(); } } diff --git a/examples/unbound/bootstrap.rs b/examples/unbound/bootstrap.rs index 7835619..dc8468c 100644 --- a/examples/unbound/bootstrap.rs +++ b/examples/unbound/bootstrap.rs @@ -1,10 +1,12 @@ use syrette::DIContainer; // Concrete implementations +use crate::animal_store::AnimalStore; use crate::animals::dog::Dog; use crate::animals::human::Human; // // Interfaces +use crate::interfaces::animal_store::IAnimalStore; use crate::interfaces::dog::IDog; use crate::interfaces::human::IHuman; @@ -19,5 +21,10 @@ pub fn bootstrap() -> DIContainer di_container.bind::().to::().unwrap(); + di_container + .bind::() + .to::() + .unwrap(); + di_container } diff --git a/examples/unbound/interfaces/animal_store.rs b/examples/unbound/interfaces/animal_store.rs new file mode 100644 index 0000000..dc8a4c3 --- /dev/null +++ b/examples/unbound/interfaces/animal_store.rs @@ -0,0 +1,11 @@ +use syrette::ptr::{SingletonPtr, TransientPtr}; + +use crate::interfaces::cat::ICat; +use crate::interfaces::dog::IDog; + +pub trait IAnimalStore +{ + fn get_dog(&self) -> SingletonPtr; + + fn get_cat(&self) -> &TransientPtr; +} diff --git a/examples/unbound/interfaces/mod.rs b/examples/unbound/interfaces/mod.rs index 5444978..bd9f848 100644 --- a/examples/unbound/interfaces/mod.rs +++ b/examples/unbound/interfaces/mod.rs @@ -1,3 +1,4 @@ +pub mod animal_store; pub mod cat; pub mod dog; pub mod human; diff --git a/examples/unbound/main.rs b/examples/unbound/main.rs index 3a937c3..47629e4 100644 --- a/examples/unbound/main.rs +++ b/examples/unbound/main.rs @@ -2,6 +2,9 @@ #![deny(clippy::pedantic)] #![allow(clippy::module_name_repetitions)] +use std::error::Error; + +mod animal_store; mod animals; mod bootstrap; mod interfaces; @@ -10,7 +13,7 @@ use bootstrap::bootstrap; use interfaces::dog::IDog; use interfaces::human::IHuman; -fn main() +fn main() -> Result<(), Box> { println!("Hello, world!"); @@ -20,7 +23,9 @@ fn main() dog.woof(); - let human = di_container.get::().unwrap(); + let human = di_container.get::()?; human.make_pets_make_sounds(); + + Ok(()) } -- cgit v1.2.3-18-g5258