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.
This commit is contained in:
Emmanuel Gil Peyrot 2019-02-27 18:04:16 +01:00
parent f68826057b
commit 1496819546

View file

@ -1,9 +1,10 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::cell::RefCell; use std::cell::RefCell;
use std::fmt;
use std::rc::Rc; use std::rc::Rc;
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, PartialEq, Eq)]
pub struct NamespaceSet { pub struct NamespaceSet {
parent: RefCell<Option<Rc<NamespaceSet>>>, parent: RefCell<Option<Rc<NamespaceSet>>>,
namespaces: BTreeMap<Option<String>, String>, namespaces: BTreeMap<Option<String>, 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 { impl NamespaceSet {
pub fn declared_ns(&self) -> &BTreeMap<Option<String>, String> { pub fn declared_ns(&self) -> &BTreeMap<Option<String>, String> {
&self.namespaces &self.namespaces
@ -107,7 +121,6 @@ impl From<(String, String)> for NamespaceSet {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::rc::Rc;
#[test] #[test]
fn get_has() { 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)))");
}
} }