diff --git a/xml_struct/src/impls.rs b/xml_struct/src/impls.rs index 77d3ff5..efb6179 100644 --- a/xml_struct/src/impls.rs +++ b/xml_struct/src/impls.rs @@ -63,6 +63,23 @@ impl XmlSerialize for &str { } } +/// Serializes a boolean as a text content node. +/// +/// `true` is serialized as the string value "true", while `false` is serialized +/// as the string value "false". +impl XmlSerialize for bool { + fn serialize_child_nodes(&self, writer: &mut Writer) -> Result<(), Error> + where + W: std::io::Write, + { + let content = if *self { "true" } else { "false" }; + + writer.write_event(Event::Text(BytesText::new(content)))?; + + Ok(()) + } +} + /// Serializes the contents of an `Option` as content nodes. /// /// `Some(t)` is serialized identically to `t`, while `None` produces no output. @@ -146,6 +163,18 @@ impl XmlSerializeAttr for &str { } } +/// Serializes a boolean as an XML attribute value. +/// +/// `true` is serialized as the string value "true", while `false` is serialized +/// as the string value "false". +impl XmlSerializeAttr for bool { + fn serialize_as_attribute(&self, start_tag: &mut quick_xml::events::BytesStart, name: &str) { + let content = if *self { "true" } else { "false" }; + + start_tag.push_attribute((name, content)); + } +} + /// Serializes the contents of an `Option` as an XML attribute value. /// /// `Some(t)` is serialized identically to `t`, while `None` produces no output. diff --git a/xml_struct/src/tests.rs b/xml_struct/src/tests.rs index 69cf8eb..83a8b02 100644 --- a/xml_struct/src/tests.rs +++ b/xml_struct/src/tests.rs @@ -4,10 +4,13 @@ #![cfg(test)] +use quick_xml::events::{attributes::Attribute, BytesStart}; use xml_struct_tests::{serialize_value_as_element, serialize_value_children}; +use crate::XmlSerializeAttr; + #[test] -fn string() { +fn string_as_content_node() { let content = String::from("some arbitrary content"); let expected = content.clone(); @@ -49,7 +52,7 @@ fn string_as_element() { } #[test] -fn int() { +fn int_as_content_node() { let content: i8 = 17; let expected = format!("{content}"); @@ -173,3 +176,89 @@ fn int_as_element() { "Serializing `u64` should result in bare text content" ); } + +#[test] +fn bool_as_content_node() { + let content = true; + let expected = "true"; + + let actual = serialize_value_children(content).expect("Failed to serialize value"); + + assert_eq!( + actual, expected, + "`true` should be serialized as string value 'true'" + ); + + let content = false; + let expected = "false"; + + let actual = serialize_value_children(content).expect("Failed to serialize value"); + + assert_eq!( + actual, expected, + "`false` should be serialized as string value 'false'" + ); +} + +#[test] +fn bool_as_attribute_value() { + let element_name = "foo"; + let attr_name = "bar"; + + let content = true; + let expected = vec![Attribute::from((attr_name, "true"))]; + + let mut start = BytesStart::new(element_name); + content.serialize_as_attribute(&mut start, &attr_name); + + let actual: Vec<_> = start + .attributes() + .map(|result| result.expect("Failed to get attribute value")) + .collect(); + + assert_eq!( + actual, expected, + "`true` should be serialized as string value 'true'" + ); + + let content = false; + let expected = vec![Attribute::from((attr_name, "false"))]; + + let mut start = BytesStart::new(element_name); + content.serialize_as_attribute(&mut start, &attr_name); + + let actual: Vec<_> = start + .attributes() + .map(|result| result.expect("Failed to get attribute value")) + .collect(); + + assert_eq!( + actual, expected, + "`false` should be serialized as string value 'false'" + ); +} + +#[test] +fn bool_as_element() { + let name = "george"; + + let content = true; + let expected = format!("<{name}>true"); + + let actual = serialize_value_as_element(content, name).expect("Failed to serialize value"); + + assert_eq!( + actual, expected, + "Serializing `bool` should result in bare text content" + ); + + let content = false; + let expected = format!("<{name}>false"); + + let actual = serialize_value_as_element(content, name).expect("Failed to serialize value"); + + assert_eq!( + actual, expected, + "Serializing `bool` should result in bare text content" + ); +}