From 149681954625097448616ac88c277a5ba77b6638 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 27 Feb 2019 18:04:16 +0100 Subject: [PATCH] Implement a nicer Debug for NamespaceSet The existing one was quite hard to parse visually, this makes it a lot easier to understand what is what. --- src/namespace_set.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/namespace_set.rs b/src/namespace_set.rs index 57d3156..a5401d1 100644 --- a/src/namespace_set.rs +++ b/src/namespace_set.rs @@ -1,9 +1,10 @@ use std::collections::BTreeMap; use std::cell::RefCell; +use std::fmt; use std::rc::Rc; -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq)] pub struct NamespaceSet { parent: RefCell>>, namespaces: BTreeMap, String>, @@ -18,6 +19,19 @@ impl Default for NamespaceSet { } } +impl fmt::Debug for NamespaceSet { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "NamespaceSet(")?; + for (prefix, namespace) in &self.namespaces { + write!(f, "xmlns{}={:?}, ", match prefix { + None => String::new(), + Some(prefix) => format!(":{}", prefix), + }, namespace)?; + } + write!(f, "parent: {:?})", *self.parent.borrow()) + } +} + impl NamespaceSet { pub fn declared_ns(&self) -> &BTreeMap, String> { &self.namespaces @@ -107,7 +121,6 @@ impl From<(String, String)> for NamespaceSet { #[cfg(test)] mod tests { use super::*; - use std::rc::Rc; #[test] fn get_has() { @@ -147,4 +160,11 @@ mod tests { } } + #[test] + fn debug_looks_correct() { + let parent = NamespaceSet::from("http://www.w3.org/2000/svg".to_owned()); + let namespaces = NamespaceSet::from(("xhtml".to_owned(), "http://www.w3.org/1999/xhtml".to_owned())); + namespaces.set_parent(Rc::new(parent)); + assert_eq!(format!("{:?}", namespaces), "NamespaceSet(xmlns:xhtml=\"http://www.w3.org/1999/xhtml\", parent: Some(NamespaceSet(xmlns=\"http://www.w3.org/2000/svg\", parent: None)))"); + } }