use crate::alloc_prelude::*;
#[cfg(not(feature = "small_nodes"))]
pub type NodeId = u32;
#[cfg(feature = "small_nodes")]
pub type NodeId = u16;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Attribute {
pub name: Box<str>,
pub value: Box<str>,
}
impl Attribute {
pub fn new(name: impl Into<Box<str>>, value: impl Into<Box<str>>) -> Self {
Self {
name: name.into(),
value: value.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeclarationData {
pub version: Box<str>,
pub encoding: Option<Box<str>>,
pub standalone: Option<bool>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DocTypeData {
pub name: Box<str>,
pub public_id: Option<Box<str>>,
pub system_id: Option<Box<str>>,
pub internal_subset: Option<Box<str>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NodeKind {
Prolog,
Declaration(Box<DeclarationData>),
Root,
Element {
name: Box<str>,
attributes: Vec<Attribute>,
},
Text(Box<str>),
CData(Box<str>),
Comment(Box<str>),
ProcessingInstruction { target: Box<str>, data: Box<str> },
DocTypeDefinition(Box<DocTypeData>),
EntityReference(Box<str>),
}
impl NodeKind {
pub fn name(&self) -> &str {
match self {
NodeKind::Element { name, .. } => name,
NodeKind::ProcessingInstruction { target, .. } => target,
NodeKind::DocTypeDefinition(dt) => &dt.name,
NodeKind::EntityReference(name) => name,
NodeKind::Declaration(_) => "xml",
NodeKind::CData(_) => "#cdata",
NodeKind::Comment(_) => "#comment",
NodeKind::Text(_) => "#text",
NodeKind::Prolog => "#prolog",
NodeKind::Root => "#document",
}
}
pub fn get_attribute<'a>(&'a self, target: &str) -> Option<&'a str> {
if let NodeKind::Element { attributes, .. } = self {
attributes
.iter()
.find(|a| &*a.name == target)
.map(|a| &*a.value)
} else {
None
}
}
pub fn has_attribute(&self, target: &str) -> bool {
self.get_attribute(target).is_some()
}
pub fn set_attribute(&mut self, name: impl Into<Box<str>>, value: impl Into<Box<str>>) -> bool {
if let NodeKind::Element { attributes, .. } = self {
let name_box = name.into();
if let Some(attr) = attributes.iter_mut().find(|a| a.name == name_box) {
attr.value = value.into();
} else {
attributes.push(Attribute::new(name_box, value));
}
true
} else {
false
}
}
pub fn remove_attribute(&mut self, target: &str) -> bool {
if let NodeKind::Element { attributes, .. } = self {
if let Some(pos) = attributes.iter().position(|a| &*a.name == target) {
attributes.remove(pos);
return true;
}
}
false
}
pub fn is_element(&self) -> bool {
matches!(self, NodeKind::Element { .. })
}
pub fn is_text(&self) -> bool {
matches!(self, NodeKind::Text(_))
}
pub fn is_comment(&self) -> bool {
matches!(self, NodeKind::Comment(_))
}
pub fn is_cdata(&self) -> bool {
matches!(self, NodeKind::CData(_))
}
pub fn is_processing_instruction(&self) -> bool {
matches!(self, NodeKind::ProcessingInstruction { .. })
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NodeData {
pub id: NodeId,
pub parent: Option<NodeId>,
pub children: Vec<NodeId>,
pub kind: NodeKind,
}
impl NodeData {
pub fn new(id: NodeId, kind: NodeKind) -> Self {
Self {
id,
parent: None,
children: Vec::new(),
kind,
}
}
}