code_gen/statement/
with_statements.rs

1use crate::{CodeBuffer, Expression, ExpressionStatement, Literal, Semi, Source, Statement};
2
3/// An element with statements.
4pub trait WithStatements: Sized {
5    /// Gets the statements.
6    fn statements(&self) -> &[Box<dyn Statement>];
7
8    /// Adds the literal statement.
9    fn add_literal<L>(&mut self, literal: L)
10    where
11        L: Into<Literal>,
12    {
13        self.add_expression_statement(literal.into());
14    }
15
16    /// Adds the literal statement.
17    fn with_literal<L>(self, literal: L) -> Self
18    where
19        L: Into<Literal>,
20    {
21        self.with_expression_statement(literal.into())
22    }
23
24    /// Adds the semicolon ended statement.
25    fn add_semi<L>(&mut self, literal: L)
26    where
27        L: Into<Literal>,
28    {
29        self.add_statement(Semi::from(literal.into()))
30    }
31
32    /// Adds the semicolon ended statement.
33    fn with_semi<L>(self, literal: L) -> Self
34    where
35        L: Into<Literal>,
36    {
37        self.with_statement(Semi::from(literal.into()))
38    }
39
40    /// Adds the statement.
41    fn with_statement<S>(self, statement: S) -> Self
42    where
43        S: 'static + Statement,
44    {
45        self.with_boxed_statement(Box::new(statement))
46    }
47
48    /// Adds the statement.
49    fn add_statement<S>(&mut self, statement: S)
50    where
51        S: 'static + Statement,
52    {
53        self.add_boxed_statement(Box::new(statement));
54    }
55
56    /// Adds the boxed statement.
57    fn with_boxed_statement(mut self, statement: Box<dyn Statement>) -> Self {
58        self.add_boxed_statement(statement);
59        self
60    }
61
62    /// Adds the boxed statement.
63    fn add_boxed_statement(&mut self, statement: Box<dyn Statement>);
64
65    /// Adds the expression as a statement.
66    fn with_expression_statement<E>(self, expression: E) -> Self
67    where
68        E: 'static + Expression,
69    {
70        self.with_statement(ExpressionStatement::from(expression))
71    }
72
73    /// Adds the expression as a statement.
74    fn add_expression_statement<E>(&mut self, expression: E)
75    where
76        E: 'static + Expression,
77    {
78        self.add_statement(ExpressionStatement::from(expression));
79    }
80
81    /// Adds the source code.
82    fn add_source<S>(&mut self, source: S)
83    where
84        S: Into<Source>,
85    {
86        let source: Source = source.into();
87        let mut source: Vec<Box<dyn Statement>> = source.into();
88        for statement in source.drain(..) {
89            self.add_boxed_statement(statement);
90        }
91    }
92
93    /// Adds the source code.
94    fn with_source<S>(mut self, source: S) -> Self
95    where
96        S: Into<Source>,
97    {
98        self.add_source(source);
99        self
100    }
101
102    /// Writes the statements.
103    fn write_statements(&self, b: &mut CodeBuffer, level: usize) {
104        for statement in self.statements() {
105            statement.write(b, level);
106        }
107    }
108
109    /// Writes the curly-bracketed statement block. (`level` is the outer level)
110    fn write_curly_statement_block(&self, b: &mut CodeBuffer, level: usize) {
111        b.write("{");
112        if self.statements().is_empty() {
113            b.write("}");
114        } else {
115            b.end_line();
116            self.write_statements(b, level + 1);
117            b.indent(level);
118            b.write("}");
119        }
120    }
121}