implement ToAttributeValue on integral types, bump version

lumi created

Change summary

Cargo.toml     |  2 +-
src/convert.rs | 45 ++++++++++++++++++++++++++++-----------------
2 files changed, 29 insertions(+), 18 deletions(-)

Detailed changes

Cargo.toml 🔗

@@ -1,6 +1,6 @@
 [package]
 name = "minidom"
-version = "0.4.2"
+version = "0.4.3"
 authors = ["lumi <lumi@pew.im>", "Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>", "Bastien Orivel <eijebong+minidom@bananium.fr>"]
 description = "A small, simple DOM implementation on top of xml-rs."
 homepage = "https://gitlab.com/lumi/minidom-rs"

src/convert.rs 🔗

@@ -87,29 +87,23 @@ pub trait IntoAttributeValue {
     fn into_attribute_value(self) -> Option<String>;
 }
 
-impl IntoAttributeValue for usize {
-    fn into_attribute_value(self) -> Option<String> {
-        Some(format!("{}", self))
-    }
-}
-
-impl IntoAttributeValue for u32 {
-    fn into_attribute_value(self) -> Option<String> {
-        Some(format!("{}", self))
+macro_rules! impl_into_attribute_value {
+    ($t:ty) => {
+        impl IntoAttributeValue for $t {
+            fn into_attribute_value(self) -> Option<String> {
+                Some(format!("{}", self))
+            }
+        }
     }
 }
 
-impl IntoAttributeValue for u16 {
-    fn into_attribute_value(self) -> Option<String> {
-        Some(format!("{}", self))
+macro_rules! impl_into_attribute_values {
+    ($($t:ty),*) => {
+        $(impl_into_attribute_value!($t);)*
     }
 }
 
-impl IntoAttributeValue for u8 {
-    fn into_attribute_value(self) -> Option<String> {
-        Some(format!("{}", self))
-    }
-}
+impl_into_attribute_values!(usize, u64, u32, u16, u8, isize, i64, i32, i16, i8);
 
 impl IntoAttributeValue for String {
     fn into_attribute_value(self) -> Option<String> {
@@ -134,3 +128,20 @@ impl<T: IntoAttributeValue> IntoAttributeValue for Option<T> {
         self.and_then(|t| t.into_attribute_value())
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::IntoAttributeValue;
+
+    #[test]
+    fn test_into_attribute_value_on_ints() {
+        assert_eq!(16u8.into_attribute_value().unwrap()    , "16");
+        assert_eq!(17u16.into_attribute_value().unwrap()   , "17");
+        assert_eq!(18u32.into_attribute_value().unwrap()   , "18");
+        assert_eq!(19u64.into_attribute_value().unwrap()   , "19");
+        assert_eq!(   16i8.into_attribute_value().unwrap() , "16");
+        assert_eq!((-17i16).into_attribute_value().unwrap(), "-17");
+        assert_eq!(   18i32.into_attribute_value().unwrap(), "18");
+        assert_eq!((-19i64).into_attribute_value().unwrap(), "-19");
+    }
+}