From 0b4232d343e2214ead8fa62583bff2e948173ddf Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 20 Aug 2023 17:01:12 +0200 Subject: feat: expose DI container get_bound methods to public API --- macros/src/injectable/implementation.rs | 34 +++++++++---- src/di_container/asynchronous/mod.rs | 85 +++++++++++++++++++++++++-------- src/di_container/binding_storage.rs | 11 +++-- src/di_container/blocking/mod.rs | 66 +++++++++++++++++++++---- src/di_container/mod.rs | 33 +++++++++++++ src/errors/di_container.rs | 4 +- src/test_utils.rs | 9 ++-- 7 files changed, 194 insertions(+), 48 deletions(-) diff --git a/macros/src/injectable/implementation.rs b/macros/src/injectable/implementation.rs index bf168a4..9e97f45 100644 --- a/macros/src/injectable/implementation.rs +++ b/macros/src/injectable/implementation.rs @@ -420,14 +420,18 @@ impl InjectableImpl let method_call = parse_str::( format!( - "{}.get_bound::<{}>({}.clone(), {})", + concat!( + "{}.get_bound::<{}>({}.clone(), ", + "syrette::di_container::BindingOptions::new(){})" + ), DI_CONTAINER_VAR_NAME, dep_interface_str, DEPENDENCY_HISTORY_VAR_NAME, - dependency.get_name().as_ref().map_or_else( - || "None".to_string(), - |name| format!("Some(\"{}\")", name.value()) - ) + dependency + .get_name() + .as_ref() + .map(|name| format!(".name(\"{}\")", name.value())) + .unwrap_or_default() ) .as_str(), )?; @@ -900,7 +904,10 @@ mod tests parse2::(output)?, parse2::(quote! { #di_container_var_ident - .get_bound::(#dep_history_var_ident.clone(), None) + .get_bound::( + #dep_history_var_ident.clone(), + syrette::di_container::BindingOptions::new() + ) .map_err(|err| InjectableError::ResolveFailed { reason: Box::new(err), affected: self_type_name @@ -948,7 +955,10 @@ mod tests parse2::(output)?, parse2::(quote! { #di_container_var_ident - .get_bound::(#dep_history_var_ident.clone(), Some("special")) + .get_bound::( + #dep_history_var_ident.clone(), + syrette::di_container::BindingOptions::new().name("special") + ) .map_err(|err| InjectableError::ResolveFailed { reason: Box::new(err), affected: self_type_name @@ -994,7 +1004,10 @@ mod tests parse2::(output)?, parse2::(quote! { #di_container_var_ident - .get_bound::(#dep_history_var_ident.clone(), None) + .get_bound::( + #dep_history_var_ident.clone(), + syrette::di_container::BindingOptions::new() + ) .await .map_err(|err| InjectableError::AsyncResolveFailed { reason: Box::new(err), @@ -1044,7 +1057,10 @@ mod tests parse2::(output)?, parse2::(quote! { #di_container_var_ident - .get_bound::(#dep_history_var_ident.clone(), Some("foobar")) + .get_bound::( + #dep_history_var_ident.clone(), + syrette::di_container::BindingOptions::new().name("foobar") + ) .await .map_err(|err| InjectableError::AsyncResolveFailed { reason: Box::new(err), diff --git a/src/di_container/asynchronous/mod.rs b/src/di_container/asynchronous/mod.rs index 2939ddd..e5f7f5d 100644 --- a/src/di_container/asynchronous/mod.rs +++ b/src/di_container/asynchronous/mod.rs @@ -59,6 +59,7 @@ use async_trait::async_trait; use crate::di_container::asynchronous::binding::builder::AsyncBindingBuilder; use crate::di_container::binding_storage::DIContainerBindingStorage; +use crate::di_container::BindingOptions; use crate::errors::async_di_container::AsyncDIContainerError; use crate::future::BoxFuture; use crate::private::cast::arc::CastArc; @@ -116,14 +117,50 @@ pub trait IAsyncDIContainer: 'a: 'b, Self: 'b; - #[doc(hidden)] - async fn get_bound( - self: &Arc, + /// Returns the type bound with `Interface` where the binding has the specified + /// options. + /// + /// `dependency_history` is passed to the bound type when it is being resolved. + /// + /// # Errors + /// Will return `Err` if: + /// - No binding for `Interface` exists + /// - Resolving the binding for `Interface` fails + /// - Casting the binding for `Interface` fails + /// + /// # Examples + /// ``` + /// # use syrette::di_container::asynchronous::AsyncDIContainer; + /// # use syrette::di_container::asynchronous::IAsyncDIContainer; + /// # use syrette::dependency_history::DependencyHistory; + /// # use syrette::di_container::BindingOptions; + /// # + /// # struct EventHandler {} + /// # struct Button {} + /// # + /// # Box::pin(async { + /// # let di_container = AsyncDIContainer::new(); + /// # + /// let mut dependency_history = DependencyHistory::new(); + /// + /// dependency_history.push::(); + /// + /// di_container + /// .get_bound::