]> git.lizzy.rs Git - enumset.git/commitdiff
Unify some redundant code between the variant count checks for repr/serialize_repr
authorAlissa Rao <lymia@lymiahugs.com>
Tue, 12 Apr 2022 10:03:32 +0000 (03:03 -0700)
committerAlissa Rao <lymia@lymiahugs.com>
Tue, 12 Apr 2022 10:03:32 +0000 (03:03 -0700)
enumset/tests/compile-fail/variants.stderr
enumset_derive/src/lib.rs

index 0425ae533a8c6bf2d9deb2fb09a86ed190f00a4f..51dbd146fefe829b9c6d6c7f48bdf4d3d6f62115 100644 (file)
@@ -61,8 +61,8 @@ error[E0277]: the trait bound `OkayEnumButCantUseFromRepr: EnumSetTypeWithRepr`
 note: required by a bound in `enumset::EnumSet::<T>::from_repr`
    --> src/lib.rs
     |
-    |         T: EnumSetTypeWithRepr,
-    |            ^^^^^^^^^^^^^^^^^^^ required by this bound in `enumset::EnumSet::<T>::from_repr`
+    |     where T: EnumSetTypeWithRepr {
+    |              ^^^^^^^^^^^^^^^^^^^ required by this bound in `enumset::EnumSet::<T>::from_repr`
 
 error[E0277]: the trait bound `OkayEnumButCantUseFromRepr: EnumSetTypeWithRepr` is not satisfied
   --> tests/compile-fail/variants.rs:64:54
index 5e1afa05b1bd2ab5c471d22924b3f3eaee3fa219..cef5a39bdb196ac2a05e566c3e5e42da50732270 100644 (file)
@@ -185,39 +185,32 @@ impl EnumSetInfo {
     }
     /// Validate the enumset type.
     fn validate(&self) -> Result<()> {
-        // Check if all bits of the bitset can fit in the serialization representation.
-        if let Some(explicit_serde_repr) = &self.explicit_serde_repr {
-            let is_overflowed = match explicit_serde_repr.to_string().as_str() {
-                "u8" => self.max_discrim >= 8,
-                "u16" => self.max_discrim >= 16,
-                "u32" => self.max_discrim >= 32,
-                "u64" => self.max_discrim >= 64,
-                "u128" => self.max_discrim >= 128,
+        fn do_check(ty: &str, max_discrim: u32, what: &str) -> Result<()> {
+            let is_overflowed = match ty {
+                "u8" => max_discrim >= 8,
+                "u16" => max_discrim >= 16,
+                "u32" => max_discrim >= 32,
+                "u64" => max_discrim >= 64,
+                "u128" => max_discrim >= 128,
                 _ => error(
                     Span::call_site(),
-                    "Only `u8`, `u16`, `u32`, `u64` and `u128` are supported for serde_repr.",
+                    format!("Only `u8`, `u16`, `u32`, `u64` and `u128` are supported for {what}."),
                 )?,
             };
             if is_overflowed {
-                error(Span::call_site(), "serialize_repr cannot be smaller than bitset.")?;
+                error(Span::call_site(), format!("{what} cannot be smaller than bitset."))?;
             }
+            Ok(())
+        }
+
+        // Check if all bits of the bitset can fit in the serialization representation.
+        if let Some(explicit_serde_repr) = &self.explicit_serde_repr {
+            do_check(&explicit_serde_repr.to_string(), self.max_discrim, "serialize_repr")?;
         }
+
         // Check if all bits of the bitset can fit in the memory representation, if one was given.
         if let Some(explicit_mem_repr) = &self.explicit_mem_repr {
-            let is_overflowed = match explicit_mem_repr.to_string().as_str() {
-                "u8" => self.max_discrim >= 8,
-                "u16" => self.max_discrim >= 16,
-                "u32" => self.max_discrim >= 32,
-                "u64" => self.max_discrim >= 64,
-                "u128" => self.max_discrim >= 128,
-                _ => error(
-                    Span::call_site(),
-                    "Only `u8`, `u16`, `u32`, `u64` and `u128` are supported for repr.",
-                )?,
-            };
-            if is_overflowed {
-                error(Span::call_site(), "repr cannot be smaller than bitset.")?;
-            }
+            do_check(&explicit_mem_repr.to_string(), self.max_discrim, "repr")?;
         }
         Ok(())
     }