diff options
author | HampusM <hampus@hampusmat.com> | 2022-07-10 13:03:53 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-07-10 13:03:53 +0200 |
commit | 5894e39dd73579bdb949a3eaeb7b7b68f88df4e8 (patch) | |
tree | 311add94c54d7fec182a36081ec120deda483aab | |
parent | 152a7a71101b94e4ddaa221396403d7dd1e76dbe (diff) |
refactor: add dedicated interface & error modules
-rw-r--r-- | example/src/main.rs | 3 | ||||
-rw-r--r-- | syrette/src/di_container.rs | 20 | ||||
-rw-r--r-- | syrette/src/errors/di_container.rs | 16 | ||||
-rw-r--r-- | syrette/src/errors/injectable.rs (renamed from syrette/src/injectable.rs) | 12 | ||||
-rw-r--r-- | syrette/src/errors/mod.rs | 2 | ||||
-rw-r--r-- | syrette/src/interfaces/injectable.rs | 12 | ||||
-rw-r--r-- | syrette/src/interfaces/mod.rs | 1 | ||||
-rw-r--r-- | syrette/src/lib.rs | 6 | ||||
-rw-r--r-- | syrette/src/provider.rs | 3 | ||||
-rw-r--r-- | syrette_macros/src/lib.rs | 6 |
10 files changed, 45 insertions, 36 deletions
diff --git a/example/src/main.rs b/example/src/main.rs index 920d9f0..1f23179 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -1,4 +1,5 @@ -use syrette::{injectable, DIContainer, DIContainerError}; +use syrette::errors::di_container::DIContainerError; +use syrette::{injectable, DIContainer}; trait IDog { diff --git a/syrette/src/di_container.rs b/syrette/src/di_container.rs index f9580ae..32d53f2 100644 --- a/syrette/src/di_container.rs +++ b/syrette/src/di_container.rs @@ -1,13 +1,12 @@ use std::any::{type_name, TypeId}; use std::collections::HashMap; -use std::fmt; -use std::fmt::{Display, Formatter}; use std::marker::PhantomData; use std::rc::Rc; -use error_stack::{Context, Report, ResultExt}; +use error_stack::{Report, ResultExt}; -use crate::injectable::Injectable; +use crate::errors::di_container::DIContainerError; +use crate::interfaces::injectable::Injectable; use crate::libs::intertrait::cast_box::CastBox; use crate::provider::{IInjectableTypeProvider, InjectableTypeProvider}; @@ -47,19 +46,6 @@ where } } -#[derive(Debug)] -pub struct DIContainerError; - -impl Display for DIContainerError -{ - fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result - { - fmt.write_str("A DI container error has occurred") - } -} - -impl Context for DIContainerError {} - /// Dependency injection container. /// /// # Examples diff --git a/syrette/src/errors/di_container.rs b/syrette/src/errors/di_container.rs new file mode 100644 index 0000000..f301df2 --- /dev/null +++ b/syrette/src/errors/di_container.rs @@ -0,0 +1,16 @@ +use error_stack::Context; +use std::fmt; +use std::fmt::{Display, Formatter}; + +#[derive(Debug)] +pub struct DIContainerError; + +impl Display for DIContainerError +{ + fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result + { + fmt.write_str("A DI container error has occurred") + } +} + +impl Context for DIContainerError {} diff --git a/syrette/src/injectable.rs b/syrette/src/errors/injectable.rs index 3cc0d7f..6b0cdc5 100644 --- a/syrette/src/injectable.rs +++ b/syrette/src/errors/injectable.rs @@ -3,9 +3,6 @@ use std::fmt::{Display, Formatter}; use error_stack::Context; -use crate::libs::intertrait::CastFrom; -use crate::DIContainer; - #[derive(Debug)] pub struct ResolveError; @@ -18,12 +15,3 @@ impl Display for ResolveError } impl Context for ResolveError {} - -pub trait Injectable: CastFrom -{ - fn resolve( - di_container: &DIContainer, - ) -> error_stack::Result<Box<Self>, ResolveError> - where - Self: Sized; -} diff --git a/syrette/src/errors/mod.rs b/syrette/src/errors/mod.rs new file mode 100644 index 0000000..b0d50f0 --- /dev/null +++ b/syrette/src/errors/mod.rs @@ -0,0 +1,2 @@ +pub mod di_container; +pub mod injectable; diff --git a/syrette/src/interfaces/injectable.rs b/syrette/src/interfaces/injectable.rs new file mode 100644 index 0000000..e704e02 --- /dev/null +++ b/syrette/src/interfaces/injectable.rs @@ -0,0 +1,12 @@ +use crate::errors::injectable::ResolveError; +use crate::libs::intertrait::CastFrom; +use crate::DIContainer; + +pub trait Injectable: CastFrom +{ + fn resolve( + di_container: &DIContainer, + ) -> error_stack::Result<Box<Self>, ResolveError> + where + Self: Sized; +} diff --git a/syrette/src/interfaces/mod.rs b/syrette/src/interfaces/mod.rs new file mode 100644 index 0000000..31e53af --- /dev/null +++ b/syrette/src/interfaces/mod.rs @@ -0,0 +1 @@ +pub mod injectable; diff --git a/syrette/src/lib.rs b/syrette/src/lib.rs index fb5d03f..7278c37 100644 --- a/syrette/src/lib.rs +++ b/syrette/src/lib.rs @@ -4,7 +4,8 @@ //! //! # Examples //! ``` -//! use syrette::{injectable, DIContainer, DIContainerError}; +//! use syrette::errors::di_container::DIContainerError; +//! use syrette::{injectable, DIContainer}; //! //! trait IDog //! { @@ -115,7 +116,8 @@ //! ``` pub mod di_container; -pub mod injectable; +pub mod errors; +pub mod interfaces; pub use di_container::*; pub use syrette_macros::*; diff --git a/syrette/src/provider.rs b/syrette/src/provider.rs index 090aaac..0d6a1cc 100644 --- a/syrette/src/provider.rs +++ b/syrette/src/provider.rs @@ -2,7 +2,8 @@ use std::marker::PhantomData; extern crate error_stack; -use crate::injectable::{Injectable, ResolveError}; +use crate::errors::injectable::ResolveError; +use crate::interfaces::injectable::Injectable; use crate::DIContainer; pub trait IInjectableTypeProvider diff --git a/syrette_macros/src/lib.rs b/syrette_macros/src/lib.rs index faf338c..1761534 100644 --- a/syrette_macros/src/lib.rs +++ b/syrette_macros/src/lib.rs @@ -205,16 +205,16 @@ pub fn injectable(args_stream: TokenStream, impl_stream: TokenStream) -> TokenSt quote! { #item_impl - impl syrette::injectable::Injectable for #self_type_path { + impl syrette::interfaces::injectable::Injectable for #self_type_path { fn resolve( di_container: &syrette::DIContainer - ) -> error_stack::Result<Box<Self>, syrette::injectable::ResolveError> + ) -> error_stack::Result<Box<Self>, syrette::errors::injectable::ResolveError> { use error_stack::ResultExt; return Ok(Box::new(Self::new( #(di_container.get::<#dependency_types>() - .change_context(syrette::injectable::ResolveError) + .change_context(syrette::errors::injectable::ResolveError) .attach_printable(format!( "Unable to resolve a dependency of {}", std::any::type_name::<#self_type_path>() |