aboutsummaryrefslogtreecommitdiff
path: root/src/parse_assoc_type.rs
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-11-19 10:51:57 +0100
committerMartin Fischer <martin@push-f.com>2021-11-19 10:51:57 +0100
commitfb4ba51a742cb7b61c2e0c3059aba9c689b411e8 (patch)
tree426116acc038cef761b2108ae98ba51e4855e8ba /src/parse_assoc_type.rs
parentd8a313dd422c78fb018cfe4249b526fe3e9dc851 (diff)
refactor: make destination type mechanism extensible
Diffstat (limited to 'src/parse_assoc_type.rs')
-rw-r--r--src/parse_assoc_type.rs27
1 files changed, 23 insertions, 4 deletions
diff --git a/src/parse_assoc_type.rs b/src/parse_assoc_type.rs
index 37fd78c..a255455 100644
--- a/src/parse_assoc_type.rs
+++ b/src/parse_assoc_type.rs
@@ -2,6 +2,7 @@ use proc_macro2::Span;
use syn::spanned::Spanned;
use syn::{GenericArgument, Ident, PathArguments, PathSegment, TraitItemType, Type};
+use crate::parse_trait_sig::TypeTransform;
use crate::syn_utils::{find_in_type, trait_bounds};
use crate::AssocTypeMatcher;
@@ -12,9 +13,27 @@ pub enum AssocTypeParseError {
NoIntoBound,
}
+pub enum DestType<'a> {
+ Into(&'a Type),
+}
+
+impl DestType<'_> {
+ pub fn get_dest(&self) -> Type {
+ match self {
+ DestType::Into(ty) => (*ty).clone(),
+ }
+ }
+
+ pub fn type_transformation(&self) -> TypeTransform {
+ match self {
+ DestType::Into(_) => TypeTransform::Into,
+ }
+ }
+}
+
pub fn parse_assoc_type(
assoc_type: &TraitItemType,
-) -> Result<(&Ident, &Type), (Span, AssocTypeParseError)> {
+) -> Result<(&Ident, DestType), (Span, AssocTypeParseError)> {
for bound in trait_bounds(&assoc_type.bounds) {
if let PathSegment {
ident,
@@ -36,7 +55,7 @@ pub fn parse_assoc_type(
));
}
- return Ok((&assoc_type.ident, into_type));
+ return Ok((&assoc_type.ident, DestType::Into(into_type)));
}
}
}
@@ -49,7 +68,7 @@ mod tests {
use quote::quote;
use syn::{TraitItemType, Type};
- use crate::parse_assoc_type::{parse_assoc_type, AssocTypeParseError};
+ use crate::parse_assoc_type::{parse_assoc_type, AssocTypeParseError, DestType};
#[test]
fn ok() {
@@ -60,7 +79,7 @@ mod tests {
assert!(matches!(
parse_assoc_type(&type1),
- Ok((id, Type::Path(path)))
+ Ok((id, DestType::Into(Type::Path(path))))
if id == "A" && path.path.is_ident("String")
));
}