1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::fmt::{self, Display, Formatter};

use crate::Node;

/// A wrapper around [`Node`] that is always pretty printed.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Pretty(pub Node);

impl Pretty {
    /// Extract the inner node.
    #[must_use]
    pub fn into_inner(self) -> Node {
        self.0
    }

    /// Borrow the inner node.
    #[must_use]
    pub const fn as_inner(&self) -> &Node {
        &self.0
    }
}

impl Display for Pretty {
    /// Format as a pretty printed HTML node.
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{:#}", self.0)
    }
}

impl<N> From<N> for Pretty
where
    N: Into<Node>,
{
    /// Create a new pretty wrapper around the given node.
    fn from(node: N) -> Self {
        Self(node.into())
    }
}