use crate::{CodeBuffer, Expression, ExpressionStatement, Literal, Semi, Statement};
pub trait WithStatements: Sized {
fn statements(&self) -> &[Box<dyn Statement>];
fn add_literal<L>(&mut self, literal: L)
where
L: Into<Literal>,
{
self.add_expression_statement(literal.into());
}
fn with_literal<L>(self, literal: L) -> Self
where
L: Into<Literal>,
{
self.with_expression_statement(literal.into())
}
fn add_semi<L>(&mut self, literal: L)
where
L: Into<Literal>,
{
self.add_statement(Semi::from(literal.into()))
}
fn with_semi<L>(self, literal: L) -> Self
where
L: Into<Literal>,
{
self.with_statement(Semi::from(literal.into()))
}
fn with_statement<S>(self, statement: S) -> Self
where
S: 'static + Statement,
{
self.with_boxed_statement(Box::new(statement))
}
fn add_statement<S>(&mut self, statement: S)
where
S: 'static + Statement,
{
self.add_boxed_statement(Box::new(statement));
}
fn with_boxed_statement(mut self, statement: Box<dyn Statement>) -> Self {
self.add_boxed_statement(statement);
self
}
fn add_boxed_statement(&mut self, statement: Box<dyn Statement>);
fn with_expression_statement<E>(self, expression: E) -> Self
where
E: 'static + Expression,
{
self.with_statement(ExpressionStatement::from(expression))
}
fn add_expression_statement<E>(&mut self, expression: E)
where
E: 'static + Expression,
{
self.add_statement(ExpressionStatement::from(expression));
}
fn write_statements(&self, b: &mut CodeBuffer, level: usize) {
for statement in self.statements() {
statement.write(b, level);
}
}
fn write_curly_statement_block(&self, b: &mut CodeBuffer, level: usize) {
b.write("{");
if self.statements().is_empty() {
b.write("}");
} else {
b.end_line();
self.write_statements(b, level + 1);
b.indent(level);
b.write("}");
}
}
}