From 1c46b68581213ca8ae6200daa32f626b5389b4b0 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 25 Aug 2022 20:21:49 +0200 Subject: 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 --- macros/src/injectable_impl.rs | 83 ++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 41 deletions(-) (limited to 'macros') 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 + ) -> Vec { dependency_types .iter() .filter_map(|dep_type| match &dep_type.interface { - Type::TraitObject(dep_type_trait) => parse_str::( - 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::( + 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() } -- cgit v1.2.3-18-g5258