html_node_core/node/
comment.rs

1use std::fmt::{self, Display, Formatter};
2
3/// A comment.
4///
5/// ```html
6/// <!-- I'm a comment! -->
7/// ```
8#[derive(Debug, Clone, PartialEq, Eq)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10pub struct Comment {
11    /// The text of the comment.
12    ///
13    /// ```html
14    /// <!-- comment -->
15    /// ```
16    pub comment: String,
17}
18
19impl Display for Comment {
20    /// Format as an HTML comment.
21    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
22        write!(f, "<!-- {} -->", self.comment)
23    }
24}
25
26impl<C> From<C> for Comment
27where
28    C: Into<String>,
29{
30    /// Create a new comment from anything that can be converted into a string.
31    fn from(comment: C) -> Self {
32        Self {
33            comment: comment.into(),
34        }
35    }
36}