aboutsummaryrefslogtreecommitdiff
path: root/macros/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-08-25 20:21:49 +0200
committerHampusM <hampus@hampusmat.com>2022-08-27 14:28:23 +0200
commit1c46b68581213ca8ae6200daa32f626b5389b4b0 (patch)
tree8da649471db7893a2347a2df383324b84ac226f0 /macros/src
parent8e862c7998d0b59c71d20cbcbbc57031f734b6fa (diff)
refactor!: make DI container have single get function
BREAKING CHANGE: The DI container get_singleton & get_factory functions have been replaced by the get function now returning a enum
Diffstat (limited to 'macros/src')
-rw-r--r--macros/src/injectable_impl.rs83
1 files changed, 42 insertions, 41 deletions
diff --git a/macros/src/injectable_impl.rs b/macros/src/injectable_impl.rs
index 62bf057..d74acb3 100644
--- a/macros/src/injectable_impl.rs
+++ b/macros/src/injectable_impl.rs
@@ -103,12 +103,7 @@ impl InjectableImpl
#maybe_prevent_circular_deps
return Ok(syrette::ptr::TransientPtr::new(Self::new(
- #(#get_dep_method_calls
- .map_err(|err| InjectableError::ResolveFailed {
- reason: Box::new(err),
- affected: self_type_name
- })?
- ),*
+ #(#get_dep_method_calls),*
)));
}
}
@@ -117,53 +112,59 @@ impl InjectableImpl
fn create_get_dep_method_calls(
dependency_types: &[DependencyType],
- ) -> Vec<ExprMethodCall>
+ ) -> Vec<proc_macro2::TokenStream>
{
dependency_types
.iter()
.filter_map(|dep_type| match &dep_type.interface {
- Type::TraitObject(dep_type_trait) => parse_str::<ExprMethodCall>(
- format!(
- "{}.get_{}::<{}>({}.clone())",
- DI_CONTAINER_VAR_NAME,
- if dep_type.ptr == "SingletonPtr" {
- "singleton_with_history"
- } else {
- "with_history"
- },
- dep_type_trait.to_token_stream(),
- DEPENDENCY_HISTORY_VAR_NAME
+ Type::TraitObject(dep_type_trait) => {
+ let method_call = parse_str::<ExprMethodCall>(
+ format!(
+ "{}.get_bound::<{}>({}.clone())",
+ DI_CONTAINER_VAR_NAME,
+ dep_type_trait.to_token_stream(),
+ DEPENDENCY_HISTORY_VAR_NAME
+ )
+ .as_str(),
)
- .as_str(),
- )
- .ok(),
+ .ok()?;
+
+ Some((method_call, dep_type))
+
+ /*
+ */
+ }
Type::Path(dep_type_path) => {
let dep_type_path_str = Self::path_to_string(&dep_type_path.path);
- if dep_type_path_str.ends_with("Factory") {
- parse_str(
- format!(
- "{}.get_factory::<{}>()",
- DI_CONTAINER_VAR_NAME, dep_type_path_str
- )
- .as_str(),
- )
- .ok()
- } else {
- parse_str(
- format!(
- "{}.get_with_history::<{}>({}.clone())",
- DI_CONTAINER_VAR_NAME,
- dep_type_path_str,
- DEPENDENCY_HISTORY_VAR_NAME
- )
- .as_str(),
+ let method_call: ExprMethodCall = parse_str(
+ format!(
+ "{}.get_bound::<{}>({}.clone())",
+ DI_CONTAINER_VAR_NAME,
+ dep_type_path_str,
+ DEPENDENCY_HISTORY_VAR_NAME
)
- .ok()
- }
+ .as_str(),
+ )
+ .ok()?;
+
+ Some((method_call, dep_type))
}
&_ => None,
})
+ .map(|(method_call, dep_type)| {
+ let ptr_name = dep_type.ptr.to_string();
+
+ let to_ptr =
+ format_ident!("{}", ptr_name.replace("Ptr", "").to_lowercase());
+
+ quote! {
+ #method_call.map_err(|err| InjectableError::ResolveFailed {
+ reason: Box::new(err),
+ affected: self_type_name
+ })?.#to_ptr().unwrap()
+ }
+ })
.collect()
}