use std::fmt::Debug;
use crate::Document;
use super::Element;
pub struct ElementDebug<'element, 'doc> {
pub(crate) element: &'element Element,
pub(crate) doc: &'doc Document,
}
impl<'element, 'doc> ElementDebug<'element, 'doc> {
pub fn new(element: &'element Element, doc: &'doc Document) -> Self {
Self { element, doc }
}
}
impl Debug for ElementDebug<'_, '_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self { element, doc } = self;
let name = element.name(doc);
let attributes = element.attributes(doc);
let children = element.children(doc);
if children.is_empty() {
return f
.debug_struct("Element")
.field("name", &name)
.field("attributes", &attributes)
.finish();
}
if children.len() == 1 && children[0].is_text() {
let text = children[0].text_content(doc);
return f
.debug_struct("Element")
.field("name", &name)
.field("attributes", &attributes)
.field("text", &text)
.finish();
}
let children: Vec<_> = element
.children(doc)
.iter()
.map(|child| child.debug(doc))
.collect();
f.debug_struct("Element")
.field("name", &name)
.field("attributes", &attributes)
.field("children", &children)
.finish()
}
}