use serde::{Deserialize, Serialize};
use tracing::instrument;
use crate::utils::HashMap;
use crate::{Document, Element, NodeBreakdown};
impl Element {
#[instrument]
pub fn breakdown(&self, doc: &Document) -> ElementBreakdown {
ElementBreakdown::new(*self, doc)
}
}
#[instrument]
fn get_children(element: Element, doc: &Document) -> Vec<NodeBreakdown> {
element
.children(doc)
.iter()
.map(|child| {
let node = child.clone();
NodeBreakdown::new(node, doc)
})
.collect()
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ElementBreakdown {
pub name: String,
pub attributes: HashMap<String, String>,
pub namespace_decls: HashMap<String, String>,
pub children: Vec<NodeBreakdown>,
pub is_root_element: bool,
}
impl ElementBreakdown {
#[instrument]
pub fn new(element: Element, doc: &Document) -> Self {
let name = element.name(doc).to_owned();
let attributes = element.attributes(doc).clone();
let namespace_decls = element.namespace_decls(doc).clone();
let children = get_children(element, doc);
Self {
name,
attributes,
namespace_decls,
children,
is_root_element: element.is_root(doc),
}
}
}