kotlin_poet_rs/io/
render.rs

1use crate::spec::CodeBlock;
2
3/// Responsible for rendering Kotlin code.
4/// Normally you need to just override [RenderKotlin::render].
5pub trait RenderKotlin {
6    /// Renders Kotlin code into [CodeBlock].
7    ///
8    /// Usually it is good idea to avoid creating new [CodeBlock]s inside of this method implementation.
9    /// Implementers should instead try to push their content into [block] parameter.
10    ///
11    /// When you need to render another [RenderKotlin] object, use [CodeBlock::push_renderable] method.
12    /// It will call [RenderKotlin::render_into] on the object and push its content into [block].
13    ///
14    /// # Implementation example
15    /// ```rust
16    /// use kotlin_poet_rs::io::RenderKotlin;
17    /// use kotlin_poet_rs::spec::{CodeBlock, Name};
18    /// use kotlin_poet_rs::tokens;
19    ///
20    /// pub struct Argument {
21    ///     pub name: Option<Name>,
22    ///     pub value: CodeBlock,
23    /// }
24    ///
25    /// impl RenderKotlin for Argument {
26    ///     fn render_into(&self, block: &mut CodeBlock) {
27    ///         if let Some(name) = &self.name {
28    ///             block.push_renderable(name);
29    ///             block.push_space();
30    ///             block.push_atom(tokens::ASSIGN);
31    ///             block.push_space();
32    ///         }
33    ///         block.push_atom(self.value.to_string().as_str());
34    ///     }
35    /// }
36    /// ```
37    fn render_into(&self, block: &mut CodeBlock);
38
39    /// Shortcut method for converting [RenderKotlin::render_into] output into [String].
40    fn render_string(&self) -> String {
41        let mut block = CodeBlock::empty();
42        self.render_into(&mut block);
43        block.to_string()
44    }
45}