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
use std::fmt::{Display, Formatter, Result}; #[derive(Debug, Clone)] pub struct Comment { pub comment: String, pub docstring: bool, } impl Comment { pub fn new<T: Display>(comment: T) -> Comment { Comment { comment: comment.to_string(), docstring: false, } } pub fn docstring<T: Display>(comment: T) -> Comment { Comment { comment: comment.to_string(), docstring: true, } } } impl Display for Comment { fn fmt(&self, f: &mut Formatter) -> Result { let comment_len = if self.docstring { "" } else { "// " }; write!(f, "{}{}", comment_len, self.comment) } }