to_element: split up trait implementation to have implementations in separate files

This commit is contained in:
Matt Bilker
2018-10-29 15:14:05 +00:00
parent 756384097a
commit a568aa31a4
2 changed files with 20 additions and 4 deletions

7
src/to_element/mod.rs Normal file
View File

@@ -0,0 +1,7 @@
use minidom::Element;
mod node;
pub trait ToElement {
fn to_element(&self) -> Element;
}

View File

@@ -1,12 +1,11 @@
use std::fmt::Write;
use minidom::Element;
use node::Node;
use to_element::ToElement;
use value::Value;
pub trait ToElement {
fn to_element(&self) -> Element;
}
impl ToElement for Node {
fn to_element(&self) -> Element {
let mut elem = Element::bare(self.key());
@@ -15,6 +14,16 @@ impl ToElement for Node {
elem.set_attr("__type", value.standard_type().name);
match value {
Value::Binary(data) => {
elem.set_attr("__size", data.len());
let len = data.len() * 2;
let value = data.into_iter().fold(String::with_capacity(len), |mut val, x| {
write!(val, "{:02x}", x).expect("Failed to append hex char");
val
});
elem.append_text_node(value);
},
Value::String(value) => {
elem.append_text_node(value.as_str());
},