kotlin_poet_rs/spec/
comment.rs

1use std::str::FromStr;
2use crate::io::RenderKotlin;
3use crate::spec::CodeBlock;
4use crate::tokens;
5use crate::util::yolo_from_str;
6
7/// Represents a 'normal', non documentation comment in Kotlin.
8///
9/// Entities that support comments should usually store them as [Vec<Comment>] and aggregate multiple comments.
10///
11/// # Example
12/// ```rust
13/// use kotlin_poet_rs::spec::Comment;
14/// use kotlin_poet_rs::io::RenderKotlin;
15///
16/// let comment = Comment::new()
17///     .append("Hello, World!");
18///
19/// assert_eq!(
20///    "// Hello, World!",
21///    comment.render_string()
22/// )
23#[derive(Debug, Clone)]
24pub struct Comment {
25    content: String,
26    is_block_render: bool,
27}
28
29impl Comment {
30    /// Creates new, empty comment
31    pub fn new() -> Comment {
32        Comment {
33            content: String::new(),
34            is_block_render: false,
35        }
36    }
37
38    /// Appends content to the comment.
39    /// If content contains new line will be automatically converted to block comment
40    pub fn append(mut self, content: &str) -> Comment {
41        self.content.push_str(content);
42        if self.is_block_render { return self; }
43        self.is_block_render = self.content.contains(tokens::NEW_LINE);
44        self
45    }
46}
47
48yolo_from_str!(Comment);
49impl FromStr for Comment {
50    type Err = ();
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        Ok(Comment::new().append(s))
54    }
55}
56
57impl RenderKotlin for Comment {
58    fn render_into(&self, block: &mut CodeBlock) {
59        if self.is_block_render {
60            block.push_static_atom(tokens::BLOCK_COMMENT_START);
61            block.push_new_line();
62            let split = self.content.split(tokens::NEW_LINE)
63                .enumerate().collect::<Vec<_>>();
64            let split_len = split.len();
65            for (idx, line) in split {
66                if idx == split_len - 1 && line.is_empty() {
67                    break;
68                }
69
70                block.push_static_atom(tokens::BLOCK_COMMENT_MIDDLE);
71                block.push_space();
72                block.push_atom(line);
73                block.push_new_line();
74            }
75            block.push_static_atom(tokens::BLOCK_COMMENT_END)
76        } else {
77            block.push_static_atom(tokens::INLINE_COMMENT_START);
78            block.push_space();
79            block.push_atom(self.content.as_str());
80        }
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use crate::io::RenderKotlin;
87
88    #[test]
89    fn test_comment_normal() {
90        let comment = super::Comment::new()
91            .append("Hello, ")
92            .append("World!");
93        assert_eq!(
94            comment.render_string(),
95            "// Hello, World!"
96        );
97    }
98
99    #[test]
100    fn test_comment_block() {
101        let comment = super::Comment::new()
102            .append("Hello\n")
103            .append("World\n");
104
105        assert_eq!(
106            comment.render_string(),
107            "/*\n * Hello\n * World\n */"
108        )
109    }
110}