#![warn(clippy::cargo)]
#![warn(clippy::nursery)]
#![warn(clippy::pedantic)]
#![warn(missing_copy_implementations)]
#![warn(missing_debug_implementations)]
#![warn(missing_docs)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
mod http;
mod node;
#[cfg(feature = "pretty")]
pub mod pretty;
#[cfg(feature = "typed")]
pub mod typed;
use std::fmt::{self, Display, Formatter};
pub use self::node::*;
#[cfg(feature = "typed")]
use self::typed::TypedElement;
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Node {
Comment(Comment),
Doctype(Doctype),
Fragment(Fragment),
Element(Element),
Text(Text),
UnsafeText(UnsafeText),
}
impl Node {
pub const EMPTY: Self = Self::Fragment(Fragment {
children: Vec::new(),
});
#[cfg(feature = "typed")]
pub fn from_typed<E: TypedElement>(element: E, children: Option<Vec<Self>>) -> Self {
element.into_node(children)
}
#[cfg(feature = "pretty")]
#[must_use]
pub fn pretty(self) -> pretty::Pretty {
self.into()
}
}
impl Default for Node {
fn default() -> Self {
Self::EMPTY
}
}
impl Display for Node {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match &self {
Self::Comment(comment) => comment.fmt(f),
Self::Doctype(doctype) => doctype.fmt(f),
Self::Fragment(fragment) => fragment.fmt(f),
Self::Element(element) => element.fmt(f),
Self::Text(text) => text.fmt(f),
Self::UnsafeText(unsafe_text) => unsafe_text.fmt(f),
}
}
}
impl<I, N> From<I> for Node
where
I: IntoIterator<Item = N>,
N: Into<Self>,
{
fn from(iter: I) -> Self {
Self::Fragment(iter.into())
}
}
impl From<Comment> for Node {
fn from(comment: Comment) -> Self {
Self::Comment(comment)
}
}
impl From<Doctype> for Node {
fn from(doctype: Doctype) -> Self {
Self::Doctype(doctype)
}
}
impl From<Fragment> for Node {
fn from(fragment: Fragment) -> Self {
Self::Fragment(fragment)
}
}
impl From<Element> for Node {
fn from(element: Element) -> Self {
Self::Element(element)
}
}
impl From<Text> for Node {
fn from(text: Text) -> Self {
Self::Text(text)
}
}
impl From<UnsafeText> for Node {
fn from(text: UnsafeText) -> Self {
Self::UnsafeText(text)
}
}