implement ToAttributeValue on integral types, bump version

This commit is contained in:
lumi 2017-05-28 00:25:57 +02:00
parent 4166751828
commit d6a9e6e9ea
2 changed files with 29 additions and 18 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "minidom" 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>"] 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." description = "A small, simple DOM implementation on top of xml-rs."
homepage = "https://gitlab.com/lumi/minidom-rs" homepage = "https://gitlab.com/lumi/minidom-rs"

View file

@ -87,29 +87,23 @@ pub trait IntoAttributeValue {
fn into_attribute_value(self) -> Option<String>; fn into_attribute_value(self) -> Option<String>;
} }
impl IntoAttributeValue for usize { macro_rules! impl_into_attribute_value {
($t:ty) => {
impl IntoAttributeValue for $t {
fn into_attribute_value(self) -> Option<String> { fn into_attribute_value(self) -> Option<String> {
Some(format!("{}", self)) Some(format!("{}", self))
} }
} }
}
}
impl IntoAttributeValue for u32 { macro_rules! impl_into_attribute_values {
fn into_attribute_value(self) -> Option<String> { ($($t:ty),*) => {
Some(format!("{}", self)) $(impl_into_attribute_value!($t);)*
} }
} }
impl IntoAttributeValue for u16 { impl_into_attribute_values!(usize, u64, u32, u16, u8, isize, i64, i32, i16, i8);
fn into_attribute_value(self) -> Option<String> {
Some(format!("{}", self))
}
}
impl IntoAttributeValue for u8 {
fn into_attribute_value(self) -> Option<String> {
Some(format!("{}", self))
}
}
impl IntoAttributeValue for String { impl IntoAttributeValue for String {
fn into_attribute_value(self) -> Option<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()) 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");
}
}