code_gen/rust/common/
with_comments.rs

1use crate::rust::CommentType;
2use crate::CodeBuffer;
3
4/// An element with comment lines.
5pub trait WithComments: Sized {
6    /// Gets the comment lines.
7    fn comments(&self) -> &[String];
8
9    /// Adds the `comment` line.
10    fn with_comment<S>(mut self, comment: S) -> Self
11    where
12        S: Into<String>,
13    {
14        self.add_comment(comment);
15        self
16    }
17
18    /// Adds the `comment` line.
19    fn add_comment<S>(&mut self, comment: S)
20    where
21        S: Into<String>;
22
23    /// Writes the comment lines.
24    fn write_comments(&self, comment_type: CommentType, b: &mut CodeBuffer, level: usize) {
25        for line in self.comments() {
26            comment_type.write_line(b, level, line.as_str());
27        }
28    }
29}