aboutsummaryrefslogtreecommitdiff
path: root/src/transform.rs
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-11-22 09:52:00 +0100
committerMartin Fischer <martin@push-f.com>2021-11-22 09:53:46 +0100
commit3d3e2ac6cdce593ab68c78d583f57905523a1591 (patch)
treec4bac6f7dc9f3313c77239e886f890107b912ded /src/transform.rs
parent4101b0d0f188a7814bdfbf59378b0c528faa7147 (diff)
generalize to collection with one type / two types
Diffstat (limited to 'src/transform.rs')
-rw-r--r--src/transform.rs43
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(),
));