diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-22 11:42:07 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-11-22 11:42:07 +0100 |
commit | 7384c11750e9971770683861f7cd5f719c487ce9 (patch) | |
tree | 91bceba6b77ff84b04363a6976284df3023e8e9d /src | |
parent | 5c6f0d3510517d867c27e9a658e5015fb9b3d5ef (diff) |
refactor: make MethodError wrap TransformError
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 5 | ||||
-rw-r--r-- | src/parse_trait_sig.rs | 34 | ||||
-rw-r--r-- | src/transform.rs | 8 |
3 files changed, 25 insertions, 22 deletions
@@ -44,6 +44,7 @@ use crate::parse_trait_sig::SignatureChanges; use crate::parse_trait_sig::TypeTransform; use crate::syn_utils::iter_path; use crate::syn_utils::trait_bounds; +use crate::transform::TransformError; use crate::transform::TypeConverter; mod parse_assoc_type; @@ -95,7 +96,7 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream { let mut signature = method.sig.clone(); match parse_trait_signature(&mut signature, &type_converter) { Err((_, MethodError::NonDispatchableMethod)) => continue, - Err((span, MethodError::AssocTypeWithoutDestType)) => { + Err((span, MethodError::Transform(TransformError::AssocTypeWithoutDestType))) => { return abort!( span, "associated type is either undefined or doesn't have a trait bound" @@ -107,7 +108,7 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream { "dynamize does not support associated types in parameter types" ) } - Err((span, MethodError::UnsupportedType)) => { + Err((span, MethodError::Transform(TransformError::UnsupportedType))) => { return abort!(span, "dynamize does not know how to convert this type") } Err((span, MethodError::UnconvertedAssocType)) => { diff --git a/src/parse_trait_sig.rs b/src/parse_trait_sig.rs index aff4fe9..dfe3ce7 100644 --- a/src/parse_trait_sig.rs +++ b/src/parse_trait_sig.rs @@ -28,13 +28,17 @@ pub enum MethodError { AssocTypeInInputs, ImplTraitInInputs, - // TODO: wrap TransformError - AssocTypeWithoutDestType, - UnsupportedType, + Transform(TransformError), UnconvertedAssocType, } +impl From<TransformError> for MethodError { + fn from(err: TransformError) -> Self { + Self::Transform(err) + } +} + fn filter_map_impl_trait(item: TypeOrPath) -> Option<&TypeImplTrait> { match item { TypeOrPath::Type(Type::ImplTrait(impltrait)) => Some(impltrait), @@ -72,11 +76,8 @@ pub fn parse_trait_signature( let return_type = match &mut signature.output { ReturnType::Type(_, og_type) => match type_converter.convert_type(og_type) { Ok(ret_type) => ret_type, - Err((span, TransformError::AssocTypeWithoutDestType)) => { - return Err((span, MethodError::AssocTypeWithoutDestType)); - } - Err((span, TransformError::UnsupportedType)) => { - return Err((span, MethodError::UnsupportedType)); + Err((span, err)) => { + return Err((span, err.into())); } }, ReturnType::Default => TypeTransform::NoOp, @@ -144,7 +145,7 @@ mod tests { use crate::{ parse_assoc_type::DestType, parse_trait_sig::{parse_trait_signature, MethodError, SignatureChanges, TypeTransform}, - transform::TypeConverter, + transform::{TransformError, TypeConverter}, }; #[test] @@ -194,7 +195,10 @@ mod tests { assert!(matches!( parse_trait_signature(&mut type1.sig, &Default::default()), - Err((_, MethodError::AssocTypeWithoutDestType)) + Err(( + _, + MethodError::Transform(TransformError::AssocTypeWithoutDestType) + )) )); } @@ -259,7 +263,7 @@ mod tests { assert!(matches!( parse_trait_signature(&mut type1.sig, &Default::default()), - Err((_, MethodError::UnsupportedType)) + Err((_, MethodError::Transform(TransformError::UnsupportedType))) )); } @@ -272,7 +276,7 @@ mod tests { assert!(matches!( parse_trait_signature(&mut type1.sig, &Default::default()), - Err((_, MethodError::UnsupportedType)) + Err((_, MethodError::Transform(TransformError::UnsupportedType))) )); } @@ -285,7 +289,7 @@ mod tests { assert!(matches!( parse_trait_signature(&mut type1.sig, &Default::default()), - Err((_, MethodError::UnsupportedType)) + Err((_, MethodError::Transform(TransformError::UnsupportedType))) )); } @@ -298,7 +302,7 @@ mod tests { assert!(matches!( parse_trait_signature(&mut type1.sig, &Default::default()), - Err((_, MethodError::UnsupportedType)) + Err((_, MethodError::Transform(TransformError::UnsupportedType))) )); } @@ -350,7 +354,7 @@ mod tests { assert!(matches!( parse_trait_signature(&mut type1.sig, &Default::default()), - Err((_, MethodError::UnsupportedType)) + Err((_, MethodError::Transform(TransformError::UnsupportedType))) )); } } diff --git a/src/transform.rs b/src/transform.rs index 13cf519..4649037 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -19,6 +19,7 @@ pub struct TypeConverter<'a> { pub assoc_type_conversions: HashMap<Ident, DestType<'a>>, } +#[derive(Debug)] pub enum TransformError { AssocTypeWithoutDestType, UnsupportedType, @@ -237,11 +238,8 @@ fn dynamize_trait_bound( Ok(ret_type) => { transforms.push(ret_type); } - Err((span, TransformError::AssocTypeWithoutDestType)) => { - return Err((span, MethodError::AssocTypeWithoutDestType)); - } - Err((span, TransformError::UnsupportedType)) => { - return Err((span, MethodError::UnsupportedType)); + Err((span, err)) => { + return Err((span, err.into())); } } } |