diff options
author | Martin Fischer <martin@push-f.com> | 2021-11-25 13:48:26 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-11-25 13:48:46 +0100 |
commit | 8046190e8f537407210fb87acbfe96d4084d58da (patch) | |
tree | 34c093627f5fd75db82cb8fb48402b82ace7d803 /src/transform.rs | |
parent | 74a29b912aaa14dbeb7e5c51ae11c61e733ef645 (diff) |
support Self qualified as same trait
Diffstat (limited to 'src/transform.rs')
-rw-r--r-- | src/transform.rs | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/src/transform.rs b/src/transform.rs index eed48c4..75138c9 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -14,10 +14,10 @@ use crate::{ trait_sig::{MethodError, TypeTransform}, }; -#[derive(Default)] pub struct TypeConverter<'a> { pub assoc_type_conversions: HashMap<Ident, DestType<'a>>, pub collections: HashMap<Ident, usize>, + pub trait_ident: Ident, } #[derive(Debug)] @@ -27,7 +27,7 @@ pub enum TransformError { ExpectedAtLeastNTypes(usize), AssocTypeAfterFirstNTypes(usize, Ident), QualifiedAssociatedType, - QualifiedSelfAssociatedType, + SelfQualifiedAsOtherTrait, } impl TypeConverter<'_> { @@ -115,7 +115,8 @@ impl TypeConverter<'_> { } if let Type::Path(TypePath { - qself: Some(qself), .. + path, + qself: Some(qself), }) = type_ { if let Type::Path(self_path) = qself.ty.as_ref() { @@ -123,10 +124,22 @@ impl TypeConverter<'_> { if !self_path.path.is_ident("Self") { return Err((self_path.span(), TransformError::QualifiedAssociatedType)); } - return Err(( - self_path.span(), - TransformError::QualifiedSelfAssociatedType, - )); + + if qself.position == 1 + && path.segments.len() == 2 + && path.segments[0].arguments.is_empty() + && path.segments[0].ident == self.trait_ident + { + let ident = &path.segments[1].ident; + let dest_type = + self.assoc_type_conversions.get(ident).ok_or_else(|| { + (ident.span(), TransformError::AssocTypeWithoutDestType) + })?; + *type_ = dest_type.get_dest(); + return Ok(dest_type.type_transformation()); + } + + return Err((path.span(), TransformError::SelfQualifiedAsOtherTrait)); } } } else if let Type::Path(TypePath { path, qself: None }) = type_ { |