From f301f2cb106f121a8d732f185f4f0274d2b78417 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 31 Mar 2017 16:59:20 +0100 Subject: [PATCH 1/2] make Debug on Element return valid XML --- src/element.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/element.rs b/src/element.rs index cedb90e5..50b63cd0 100644 --- a/src/element.rs +++ b/src/element.rs @@ -31,11 +31,9 @@ pub struct Element { impl fmt::Debug for Element { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "<{}", self.name)?; if let Some(ref ns) = self.namespace { - write!(fmt, "<{{{}}}{}", ns, self.name)?; - } - else { - write!(fmt, "<{}", self.name)?; + write!(fmt, " xmlns=\"{}\"", ns)?; } for attr in &self.attributes { write!(fmt, " {}", attr)?; From 9af7d252d7041646a64685ce847b91a60cb3421e Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 31 Mar 2017 17:01:48 +0100 Subject: [PATCH 2/2] auto-close elements with no child --- src/element.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/element.rs b/src/element.rs index 50b63cd0..13c8baf1 100644 --- a/src/element.rs +++ b/src/element.rs @@ -38,18 +38,23 @@ impl fmt::Debug for Element { for attr in &self.attributes { write!(fmt, " {}", attr)?; } - write!(fmt, ">")?; - for child in &self.children { - match *child { - Node::Element(ref e) => { - write!(fmt, "{:?}", e)?; - }, - Node::Text(ref s) => { - write!(fmt, "{}", s)?; - }, - } + if self.children.is_empty() { + write!(fmt, "/>")?; + } + else { + write!(fmt, ">")?; + for child in &self.children { + match *child { + Node::Element(ref e) => { + write!(fmt, "{:?}", e)?; + }, + Node::Text(ref s) => { + write!(fmt, "{}", s)?; + }, + } + } + write!(fmt, "", self.name)?; } - write!(fmt, "", self.name)?; Ok(()) } }