pythonic/
comment.rs

1use std::fmt::{Display, Formatter, Result};
2
3#[derive(Debug, Clone)]
4pub struct Comment {
5    pub comment: String,
6    pub docstring: bool,
7}
8
9impl Comment {
10    pub fn new<T: Display>(comment: T) -> Comment {
11        Comment {
12            comment: comment.to_string(),
13            docstring: false,
14        }
15    }
16
17    pub fn docstring<T: Display>(comment: T) -> Comment {
18        Comment {
19            comment: comment.to_string(),
20            docstring: true,
21        }
22    }
23}
24
25impl Display for Comment {
26    fn fmt(&self, f: &mut Formatter) -> Result {
27        let comment_len = if self.docstring {
28            ""
29        } else {
30            "// "
31        };
32
33        write!(f, "{}{}", comment_len, self.comment)
34    }
35}