From d6a9e6e9eae67069342a9e98083aafae87cf391c Mon Sep 17 00:00:00 2001 From: lumi Date: Sun, 28 May 2017 00:25:57 +0200 Subject: [PATCH] implement ToAttributeValue on integral types, bump version --- Cargo.toml | 2 +- src/convert.rs | 45 ++++++++++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 39ba3441..0ddb0252 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "minidom" -version = "0.4.2" +version = "0.4.3" authors = ["lumi ", "Emmanuel Gil Peyrot ", "Bastien Orivel "] description = "A small, simple DOM implementation on top of xml-rs." homepage = "https://gitlab.com/lumi/minidom-rs" diff --git a/src/convert.rs b/src/convert.rs index 980f68f7..126688b7 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -87,29 +87,23 @@ pub trait IntoAttributeValue { fn into_attribute_value(self) -> Option; } -impl IntoAttributeValue for usize { - fn into_attribute_value(self) -> Option { - Some(format!("{}", self)) +macro_rules! impl_into_attribute_value { + ($t:ty) => { + impl IntoAttributeValue for $t { + fn into_attribute_value(self) -> Option { + Some(format!("{}", self)) + } + } } } -impl IntoAttributeValue for u32 { - fn into_attribute_value(self) -> Option { - Some(format!("{}", self)) +macro_rules! impl_into_attribute_values { + ($($t:ty),*) => { + $(impl_into_attribute_value!($t);)* } } -impl IntoAttributeValue for u16 { - fn into_attribute_value(self) -> Option { - Some(format!("{}", self)) - } -} - -impl IntoAttributeValue for u8 { - fn into_attribute_value(self) -> Option { - 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 { @@ -134,3 +128,20 @@ impl IntoAttributeValue for Option { 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"); + } +}