hocon_rs/raw/
comment.rs

1use derive_more::{Constructor, Deref, DerefMut};
2use std::fmt::{Display, Formatter};
3
4#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
5pub enum CommentType {
6    DoubleSlash,
7    Hash,
8}
9
10impl Display for CommentType {
11    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
12        match self {
13            CommentType::DoubleSlash => write!(f, "//"),
14            CommentType::Hash => write!(f, "#"),
15        }
16    }
17}
18
19#[derive(Debug, Clone, Eq, PartialEq, Hash, Constructor, Deref, DerefMut)]
20pub struct Comment {
21    #[deref]
22    #[deref_mut]
23    pub content: String,
24    pub ty: CommentType,
25}
26
27impl Comment {
28    pub fn double_slash(comment: impl Into<String>) -> Comment {
29        Comment::new(comment.into(), CommentType::DoubleSlash)
30    }
31
32    pub fn hash(comment: impl Into<String>) -> Comment {
33        Comment::new(comment.into(), CommentType::Hash)
34    }
35}
36
37impl Display for Comment {
38    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
39        write!(f, "{}{}", self.ty, self.content)
40    }
41}
42
43impl From<&str> for Comment {
44    fn from(val: &str) -> Self {
45        Comment::new(String::from(val), CommentType::DoubleSlash)
46    }
47}
48
49impl From<String> for Comment {
50    fn from(val: String) -> Self {
51        Comment::new(val, CommentType::Hash)
52    }
53}