diff options
author | HampusM <hampus@hampusmat.com> | 2022-07-20 14:29:45 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-07-20 14:29:45 +0200 |
commit | 2d1a6b2d432408d74eb57e0bda3f7434617e1070 (patch) | |
tree | 7e21f8126edfdfd9c40b4b51ba5626c6440442d9 /src/provider.rs | |
parent | 7863d9859a5cbce99c3769e4fdb40283115d358d (diff) |
refactor: reorganize folder hierarchy
Diffstat (limited to 'src/provider.rs')
-rw-r--r-- | src/provider.rs | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/provider.rs b/src/provider.rs new file mode 100644 index 0000000..3b7e04c --- /dev/null +++ b/src/provider.rs @@ -0,0 +1,82 @@ +#![allow(clippy::module_name_repetitions)] +use std::marker::PhantomData; + +use crate::castable_factory::AnyFactory; +use crate::errors::injectable::ResolveError; +use crate::interfaces::injectable::Injectable; +use crate::ptr::{FactoryPtr, InterfacePtr}; +use crate::DIContainer; + +extern crate error_stack; + +pub enum Providable +{ + Injectable(InterfacePtr<dyn Injectable>), + Factory(FactoryPtr<dyn AnyFactory>), +} + +pub trait IProvider +{ + fn provide( + &self, + di_container: &DIContainer, + ) -> error_stack::Result<Providable, ResolveError>; +} + +pub struct InjectableTypeProvider<InjectableType> +where + InjectableType: Injectable, +{ + injectable_phantom: PhantomData<InjectableType>, +} + +impl<InjectableType> InjectableTypeProvider<InjectableType> +where + InjectableType: Injectable, +{ + pub fn new() -> Self + { + Self { + injectable_phantom: PhantomData, + } + } +} + +impl<InjectableType> IProvider for InjectableTypeProvider<InjectableType> +where + InjectableType: Injectable, +{ + fn provide( + &self, + di_container: &DIContainer, + ) -> error_stack::Result<Providable, ResolveError> + { + Ok(Providable::Injectable(InjectableType::resolve( + di_container, + )?)) + } +} + +pub struct FactoryProvider +{ + factory: FactoryPtr<dyn AnyFactory>, +} + +impl FactoryProvider +{ + pub fn new(factory: FactoryPtr<dyn AnyFactory>) -> Self + { + Self { factory } + } +} + +impl IProvider for FactoryProvider +{ + fn provide( + &self, + _di_container: &DIContainer, + ) -> error_stack::Result<Providable, ResolveError> + { + Ok(Providable::Factory(self.factory.clone())) + } +} |