mod data;
mod parse;
pub mod operation;
pub use parse::parse;
pub use parse::try_parse;
#[derive(Clone, Debug)]
pub enum Doctype {
Html,
Xml { version: String, encoding: String },
}
#[derive(Debug, Clone)]
pub enum Node {
Element(Element),
Text(String),
Comment(String),
Doctype(Doctype),
}
impl Node {
pub fn is_element(&self) -> bool {
matches!(self, Node::Element { .. })
}
#[deprecated(note = "Please use `is_element` instead")]
pub fn into_element(self) -> Element {
match self {
Node::Element(element) => element,
_ => panic!("{:?} is not an element", self),
}
}
pub fn as_element(&self) -> Option<&Element> {
match self {
Node::Element(element) => Some(element),
_ => None,
}
}
pub fn as_element_mut(&mut self) -> Option<&mut Element> {
match self {
Node::Element(element) => Some(element),
_ => None,
}
}
pub fn new_element(name: &str, attrs: Vec<(&str, &str)>, children: Vec<Node>) -> Node {
Element {
name: name.to_string(),
attrs: attrs
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect(),
children,
}
.into_node()
}
}
#[derive(Debug, Clone)]
pub struct Element {
pub name: String,
pub attrs: Vec<(String, String)>,
pub children: Vec<Node>,
}
impl Element {
pub fn new(name: &str, attrs: Vec<(&str, &str)>, children: Vec<Node>) -> Self {
Self {
name: name.to_string(),
attrs: attrs
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect(),
children,
}
}
}
impl Element {
pub fn into_node(self) -> Node {
Node::Element(self)
}
}
impl From<Element> for Node {
fn from(element: Element) -> Self {
Node::Element(element)
}
}