diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-22 11:31:48 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-11-22 11:31:48 +0100 |
commit | f4419ca138a5307a705decf70683414296dcf116 (patch) | |
tree | f4bb6cd5f66894d3507333402fa1b8f8e24a7870 /src | |
parent | 6b01f08fa1de6d67ab47ad19493ea683a75fc2ed (diff) |
simplify MethodError variants
Diffstat (limited to 'src')
-rw-r--r-- | src/lib.rs | 18 | ||||
-rw-r--r-- | src/parse_trait_sig.rs | 31 | ||||
-rw-r--r-- | src/transform.rs | 28 |
3 files changed, 34 insertions, 43 deletions
@@ -95,7 +95,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::UnconvertibleAssocType)) => { + Err((span, MethodError::AssocTypeWithoutDestType)) => { return abort!( span, "associated type is either undefined or doesn't have a trait bound" @@ -107,16 +107,12 @@ pub fn dynamize(_attr: TokenStream, input: TokenStream) -> TokenStream { "dynamize does not support associated types in parameter types" ) } - Err(( - span, - MethodError::AssocTypeInUnsupportedReturnType - | MethodError::UnconvertibleAssocTypeInFnInput, - )) => return abort!(span, "dynamize does not know how to convert this type"), - Err(( - span, - MethodError::UnconvertibleAssocTypeInTraitBound - | MethodError::UnconvertibleAssocTypeInWhereClause, - )) => return abort!(span, "dynamize does not support associated types here"), + Err((span, MethodError::UnsupportedType)) => { + return abort!(span, "dynamize does not know how to convert this type") + } + Err((span, MethodError::UnconvertedAssocType)) => { + return abort!(span, "dynamize does not support associated types here") + } Err((span, MethodError::ImplTraitInInputs)) => { return abort!( span, diff --git a/src/parse_trait_sig.rs b/src/parse_trait_sig.rs index 9542a69..825c425 100644 --- a/src/parse_trait_sig.rs +++ b/src/parse_trait_sig.rs @@ -27,11 +27,12 @@ pub enum MethodError { NonDispatchableMethod, AssocTypeInInputs, ImplTraitInInputs, - AssocTypeInUnsupportedReturnType, - UnconvertibleAssocTypeInFnInput, - UnconvertibleAssocTypeInTraitBound, - UnconvertibleAssocTypeInWhereClause, - UnconvertibleAssocType, + + // TODO: wrap TransformError + AssocTypeWithoutDestType, + UnsupportedType, + + UnconvertedAssocType, } fn filter_map_impl_trait(item: TypeOrPath) -> Option<&TypeImplTrait> { @@ -71,11 +72,11 @@ 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(TransformError::UnconvertibleAssocType(span)) => { - return Err((span, MethodError::UnconvertibleAssocType)); + Err(TransformError::AssocTypeWithoutDestType(span)) => { + return Err((span, MethodError::AssocTypeWithoutDestType)); } - Err(TransformError::AssocTypeInUnsupportedType(span)) => { - return Err((span, MethodError::AssocTypeInUnsupportedReturnType)); + Err(TransformError::UnsupportedType(span)) => { + return Err((span, MethodError::UnsupportedType)); } }, ReturnType::Default => TypeTransform::NoOp, @@ -193,7 +194,7 @@ mod tests { assert!(matches!( parse_trait_signature(&mut type1.sig, &Default::default()), - Err((_, MethodError::UnconvertibleAssocType)) + Err((_, MethodError::AssocTypeWithoutDestType)) )); } @@ -258,7 +259,7 @@ mod tests { assert!(matches!( parse_trait_signature(&mut type1.sig, &Default::default()), - Err((_, MethodError::AssocTypeInUnsupportedReturnType)) + Err((_, MethodError::UnsupportedType)) )); } @@ -271,7 +272,7 @@ mod tests { assert!(matches!( parse_trait_signature(&mut type1.sig, &Default::default()), - Err((_, MethodError::AssocTypeInUnsupportedReturnType)) + Err((_, MethodError::UnsupportedType)) )); } @@ -284,7 +285,7 @@ mod tests { assert!(matches!( parse_trait_signature(&mut type1.sig, &Default::default()), - Err((_, MethodError::AssocTypeInUnsupportedReturnType)) + Err((_, MethodError::UnsupportedType)) )); } @@ -297,7 +298,7 @@ mod tests { assert!(matches!( parse_trait_signature(&mut type1.sig, &Default::default()), - Err((_, MethodError::AssocTypeInUnsupportedReturnType)) + Err((_, MethodError::UnsupportedType)) )); } @@ -349,7 +350,7 @@ mod tests { assert!(matches!( parse_trait_signature(&mut type1.sig, &Default::default()), - Err((_, MethodError::UnconvertibleAssocTypeInFnInput)) + Err((_, MethodError::UnsupportedType)) )); } } diff --git a/src/transform.rs b/src/transform.rs index cf18456..7fd3672 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -20,8 +20,8 @@ pub struct TypeConverter<'a> { } pub enum TransformError { - UnconvertibleAssocType(Span), - AssocTypeInUnsupportedType(Span), + AssocTypeWithoutDestType(Span), + UnsupportedType(Span), } impl TypeConverter<'_> { @@ -101,7 +101,7 @@ impl TypeConverter<'_> { let dest_type = self .assoc_type_conversions .get(ident) - .ok_or_else(|| TransformError::UnconvertibleAssocType(ident.span()))?; + .ok_or_else(|| TransformError::AssocTypeWithoutDestType(ident.span()))?; *type_ = dest_type.get_dest(); return Ok(dest_type.type_transformation()); } @@ -154,7 +154,7 @@ impl TypeConverter<'_> { // the type contains an associated type but we // don't know how to deal with it so we abort - Err(TransformError::AssocTypeInUnsupportedType(type_.span())) + Err(TransformError::UnsupportedType(type_.span())) } } @@ -200,10 +200,7 @@ pub fn dynamize_function_bounds( if let Some(assoc_type) = iter_type(&predicate_type.bounded_ty).find_map(filter_map_assoc_paths) { - return Err(( - assoc_type.span(), - MethodError::UnconvertibleAssocTypeInWhereClause, - )); + return Err((assoc_type.span(), MethodError::UnconvertedAssocType)); } // just to provide better error messages @@ -212,10 +209,7 @@ pub fn dynamize_function_bounds( if let Some(assoc_type) = iter_path(&bound.path).find_map(filter_map_assoc_paths) { - return Err(( - assoc_type.span(), - MethodError::UnconvertibleAssocTypeInWhereClause, - )); + return Err((assoc_type.span(), MethodError::UnconvertedAssocType)); } } } @@ -243,11 +237,11 @@ fn dynamize_trait_bound( Ok(ret_type) => { transforms.push(ret_type); } - Err(TransformError::UnconvertibleAssocType(span)) => { - return Err((span, MethodError::UnconvertibleAssocType)); + Err(TransformError::AssocTypeWithoutDestType(span)) => { + return Err((span, MethodError::AssocTypeWithoutDestType)); } - Err(TransformError::AssocTypeInUnsupportedType(span)) => { - return Err((span, MethodError::UnconvertibleAssocTypeInFnInput)); + Err(TransformError::UnsupportedType(span)) => { + return Err((span, MethodError::UnsupportedType)); } } } @@ -261,7 +255,7 @@ fn dynamize_trait_bound( .filter_map(filter_map_assoc_paths) .next() { - return Err((path.span(), MethodError::UnconvertibleAssocTypeInTraitBound)); + return Err((path.span(), MethodError::UnconvertedAssocType)); } Ok(()) } |