Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add serialization impls for bool #4

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions xml_struct/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W>(&self, writer: &mut Writer<W>) -> 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<T>` as content nodes.
///
/// `Some(t)` is serialized identically to `t`, while `None` produces no output.
Expand Down Expand Up @@ -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<T>` as an XML attribute value.
///
/// `Some(t)` is serialized identically to `t`, while `None` produces no output.
Expand Down
93 changes: 91 additions & 2 deletions xml_struct/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -49,7 +52,7 @@ fn string_as_element() {
}

#[test]
fn int() {
fn int_as_content_node() {
let content: i8 = 17;
let expected = format!("{content}");

Expand Down Expand Up @@ -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</{name}>");

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</{name}>");

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"
);
}
Loading