Skip to main content

code_gen/statement/
with_statements.rs

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