html_node_core/
pretty.rs

1use std::fmt::{self, Display, Formatter};
2
3use crate::Node;
4
5/// A wrapper around [`Node`] that is always pretty printed.
6#[derive(Debug, Default, Clone, PartialEq, Eq)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct Pretty(pub Node);
9
10impl Pretty {
11    /// Extract the inner node.
12    #[must_use]
13    pub fn into_inner(self) -> Node {
14        self.0
15    }
16
17    /// Borrow the inner node.
18    #[must_use]
19    pub const fn as_inner(&self) -> &Node {
20        &self.0
21    }
22}
23
24impl Display for Pretty {
25    /// Format as a pretty printed HTML node.
26    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
27        write!(f, "{:#}", self.0)
28    }
29}
30
31impl<N> From<N> for Pretty
32where
33    N: Into<Node>,
34{
35    /// Create a new pretty wrapper around the given node.
36    fn from(node: N) -> Self {
37        Self(node.into())
38    }
39}