diff options
Diffstat (limited to 'src/transform.rs')
-rw-r--r-- | src/transform.rs | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/src/transform.rs b/src/transform.rs index 47e27a6..fa7877e 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -24,24 +24,26 @@ pub enum TransformError { AssocTypeInUnsupportedType(Span), } -fn is_supported_collection(ident: &Ident) -> bool { - // collections added here must implement IntoIterator & FromIterator - // when adding a type here don't forget to document it in the README - ident == "Vec" - || ident == "VecDeque" - || ident == "LinkedList" - || ident == "HashSet" - || ident == "BinaryHeap" - || ident == "BTreeSet" -} +impl TypeConverter<'_> { + /// Returns true for types that take one generic type parameter T + /// and implement IntoIterator<Item=T> and FromIterator<T> + fn known_as_collection_with_one_type(&self, ident: &Ident) -> bool { + // when adding a type here don't forget to document it in the README + ident == "Vec" + || ident == "VecDeque" + || ident == "LinkedList" + || ident == "HashSet" + || ident == "BinaryHeap" + || ident == "BTreeSet" + } -fn is_supported_collection_map(ident: &Ident) -> bool { - // collections added here must implement IntoIterator & FromIterator - // when adding a type here don't forget to document it in the README - ident == "HashMap" || ident == "BTreeMap" -} + /// Returns true for types that take two generic type parameter K and V + /// and implement IntoIterator<Item=(K, V)> and FromIterator<(K, V)> + fn known_as_collection_with_two_types(&self, ident: &Ident) -> bool { + // when adding a type here don't forget to document it in the README + ident == "HashMap" || ident == "BTreeMap" + } -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); @@ -115,9 +117,10 @@ impl TypeConverter<'_> { || last_seg.ident == "Result" { return Ok(TypeTransform::Map(self.convert_type(arg)?.into())); - } else if is_supported_collection(&last_seg.ident) && path_len == 1 + } else if self.known_as_collection_with_one_type(&last_seg.ident) + && path_len == 1 { - return Ok(TypeTransform::Collection( + return Ok(TypeTransform::CollectionOneType( self.convert_type(arg)?.into(), )); } @@ -136,8 +139,8 @@ impl TypeConverter<'_> { self.convert_type(arg1)?.into(), self.convert_type(arg2)?.into(), )); - } else if is_supported_collection_map(&last_seg.ident) { - return Ok(TypeTransform::CollectionMap( + } else if self.known_as_collection_with_two_types(&last_seg.ident) { + return Ok(TypeTransform::CollectionTwoTypes( self.convert_type(arg1)?.into(), self.convert_type(arg2)?.into(), )); |