svg/node/
comment.rs

1use std::collections::hash_map::DefaultHasher;
2use std::fmt;
3use std::hash::Hash;
4
5use crate::node::{Node, Value};
6
7/// A comment node.
8#[derive(Clone, Debug)]
9pub struct Comment {
10    content: String,
11}
12
13impl Comment {
14    /// Create a node.
15    #[inline]
16    pub fn new<T>(content: T) -> Self
17    where
18        T: Into<String>,
19    {
20        Self {
21            content: content.into(),
22        }
23    }
24}
25
26impl fmt::Display for Comment {
27    #[inline]
28    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
29        write!(formatter, "<!-- {} -->", self.content)
30    }
31}
32
33impl Node for Comment {
34    #[inline]
35    fn append<T>(&mut self, _: T)
36    where
37        T: Into<Box<dyn Node>>,
38    {
39    }
40
41    #[inline]
42    fn assign<T, U>(&mut self, _: T, _: U)
43    where
44        T: Into<String>,
45        U: Into<Value>,
46    {
47    }
48
49    #[inline]
50    fn get_name(&self) -> &str {
51        "comment"
52    }
53}
54
55impl super::NodeDefaultHash for Comment {
56    #[inline]
57    fn default_hash(&self, state: &mut DefaultHasher) {
58        self.content.hash(state);
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::Comment;
65
66    #[test]
67    fn comment_display() {
68        let comment = Comment::new("valid");
69        assert_eq!(comment.to_string(), "<!-- valid -->");
70
71        let comment = Comment::new("invalid -->");
72        assert_eq!(comment.to_string(), "<!-- invalid --> -->");
73    }
74}