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 ++++++-- examples/with-3rd-party/bootstrap.rs | 5 +++-- examples/with-3rd-party/main.rs | 25 ++++----------------- 8 files changed, 78 insertions(+), 34 deletions(-) create mode 100644 examples/unbound/animal_store.rs create mode 100644 examples/unbound/interfaces/animal_store.rs (limited to 'examples') 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(()) } diff --git a/examples/with-3rd-party/bootstrap.rs b/examples/with-3rd-party/bootstrap.rs index 1ab3192..49de7fa 100644 --- a/examples/with-3rd-party/bootstrap.rs +++ b/examples/with-3rd-party/bootstrap.rs @@ -1,4 +1,5 @@ -use syrette::errors::di_container::BindingBuilderError; +use std::error::Error; + use syrette::ptr::TransientPtr; use syrette::{declare_default_factory, DIContainer}; use third_party_lib::Shuriken; @@ -11,7 +12,7 @@ use crate::ninja::Ninja; declare_default_factory!(Shuriken); -pub fn bootstrap() -> error_stack::Result +pub fn bootstrap() -> Result> { let mut di_container: DIContainer = DIContainer::new(); diff --git a/examples/with-3rd-party/main.rs b/examples/with-3rd-party/main.rs index f615ff5..dd4c21f 100644 --- a/examples/with-3rd-party/main.rs +++ b/examples/with-3rd-party/main.rs @@ -2,39 +2,22 @@ #![deny(clippy::pedantic)] #![allow(clippy::module_name_repetitions)] -use std::fmt::Display; +use std::error::Error; mod bootstrap; mod interfaces; mod ninja; -use error_stack::{Context, ResultExt}; - use crate::bootstrap::bootstrap; use crate::interfaces::ninja::INinja; -#[derive(Debug)] -struct ApplicationError; - -impl Display for ApplicationError -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result - { - f.write_str("An application error has occurred") - } -} - -impl Context for ApplicationError {} - -fn main() -> error_stack::Result<(), ApplicationError> +fn main() -> Result<(), Box> { println!("Hello, world!"); - let di_container = bootstrap().change_context(ApplicationError)?; + let di_container = bootstrap()?; - let ninja = di_container - .get::() - .change_context(ApplicationError)?; + let ninja = di_container.get::()?; ninja.throw_shuriken(); -- cgit v1.2.3-18-g5258