diff options
Diffstat (limited to 'src/transform.rs')
-rw-r--r-- | src/transform.rs | 37 |
1 files changed, 18 insertions, 19 deletions
diff --git a/src/transform.rs b/src/transform.rs index 4625f4c..47e27a6 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -15,7 +15,9 @@ use crate::{ }; #[derive(Default)] -pub struct AssocTypeConversions<'a>(pub HashMap<Ident, DestType<'a>>); +pub struct TypeConverter<'a> { + pub assoc_type_conversions: HashMap<Ident, DestType<'a>>, +} pub enum TransformError { UnconvertibleAssocType(Span), @@ -39,8 +41,8 @@ fn is_supported_collection_map(ident: &Ident) -> bool { ident == "HashMap" || ident == "BTreeMap" } -impl AssocTypeConversions<'_> { - pub fn parse_type_path(&self, type_: &mut Type) -> Result<TypeTransform, TransformError> { +impl TypeConverter<'_> { + pub fn convert_type(&self, type_: &mut Type) -> Result<TypeTransform, TransformError> { if !iter_type(type_).any(match_assoc_type) { return Ok(TypeTransform::NoOp); } @@ -69,8 +71,7 @@ impl AssocTypeConversions<'_> { if binding.ident == "Item" && iter_type(&binding.ty).any(match_assoc_type) { - let inner = - self.parse_type_path(&mut binding.ty)?; + let inner = self.convert_type(&mut binding.ty)?; let box_type = BoxType { inner: quote! {#elem}, placeholder_lifetime: true, @@ -96,7 +97,7 @@ impl AssocTypeConversions<'_> { if path.segments.len() == 2 { let ident = &path.segments.last().unwrap().ident; let dest_type = self - .0 + .assoc_type_conversions .get(ident) .ok_or_else(|| TransformError::UnconvertibleAssocType(ident.span()))?; *type_ = dest_type.get_dest(); @@ -113,13 +114,11 @@ impl AssocTypeConversions<'_> { if (last_seg.ident == "Option" && path_len == 1) || last_seg.ident == "Result" { - return Ok(TypeTransform::Map( - self.parse_type_path(arg)?.into(), - )); + return Ok(TypeTransform::Map(self.convert_type(arg)?.into())); } else if is_supported_collection(&last_seg.ident) && path_len == 1 { return Ok(TypeTransform::Collection( - self.parse_type_path(arg)?.into(), + self.convert_type(arg)?.into(), )); } } @@ -134,13 +133,13 @@ impl AssocTypeConversions<'_> { { if last_seg.ident == "Result" { return Ok(TypeTransform::Result( - self.parse_type_path(arg1)?.into(), - self.parse_type_path(arg2)?.into(), + self.convert_type(arg1)?.into(), + self.convert_type(arg2)?.into(), )); } else if is_supported_collection_map(&last_seg.ident) { return Ok(TypeTransform::CollectionMap( - self.parse_type_path(arg1)?.into(), - self.parse_type_path(arg2)?.into(), + self.convert_type(arg1)?.into(), + self.convert_type(arg2)?.into(), )); } } @@ -158,7 +157,7 @@ impl AssocTypeConversions<'_> { pub fn dynamize_function_bounds( generics: &mut Generics, - assoc_type_conversions: &AssocTypeConversions, + type_converter: &TypeConverter, ) -> Result<HashMap<Ident, Vec<TypeTransform>>, (Span, MethodParseError)> { let mut type_param_transforms = HashMap::new(); @@ -167,7 +166,7 @@ pub fn dynamize_function_bounds( if let TypeParamBound::Trait(bound) = bound { dynamize_trait_bound( bound, - assoc_type_conversions, + type_converter, &type_param.ident, &mut type_param_transforms, )?; @@ -184,7 +183,7 @@ pub fn dynamize_function_bounds( if let TypeParamBound::Trait(bound) = bound { dynamize_trait_bound( bound, - assoc_type_conversions, + type_converter, ident, &mut type_param_transforms, )?; @@ -226,7 +225,7 @@ pub fn dynamize_function_bounds( fn dynamize_trait_bound( bound: &mut TraitBound, - assoc_type_conversions: &AssocTypeConversions, + type_converter: &TypeConverter, type_ident: &Ident, type_param_transforms: &mut HashMap<Ident, Vec<TypeTransform>>, ) -> Result<(), (Span, MethodParseError)> { @@ -237,7 +236,7 @@ fn dynamize_trait_bound( if segment.ident == "Fn" || segment.ident == "FnOnce" || segment.ident == "FnMut" { let mut transforms = Vec::new(); for input_type in &mut args.inputs { - match assoc_type_conversions.parse_type_path(input_type) { + match type_converter.convert_type(input_type) { Ok(ret_type) => { transforms.push(ret_type); } |