code_gen/rust/control/
match_case.rs

1use crate::{CodeBuffer, Expression, Literal, Statement, WithStatements};
2
3/// A `match` statement case.
4pub struct MatchCase {
5    expression: Box<dyn Expression>,
6    statements: Vec<Box<dyn Statement>>,
7}
8
9impl<E: 'static + Expression> From<E> for MatchCase {
10    fn from(expression: E) -> Self {
11        Self {
12            expression: Box::new(expression),
13            statements: Vec::default(),
14        }
15    }
16}
17
18impl From<&str> for MatchCase {
19    fn from(literal: &str) -> Self {
20        Self::from(Literal::from(literal))
21    }
22}
23
24impl From<String> for MatchCase {
25    fn from(literal: String) -> Self {
26        Self::from(Literal::from(literal))
27    }
28}
29
30impl WithStatements for MatchCase {
31    fn statements(&self) -> &[Box<dyn Statement>] {
32        self.statements.as_slice()
33    }
34
35    fn add_boxed_statement(&mut self, statement: Box<dyn Statement>) {
36        self.statements.push(statement);
37    }
38}
39
40impl Statement for MatchCase {
41    fn write(&self, b: &mut CodeBuffer, level: usize) {
42        b.indent(level);
43        self.expression.write(b);
44        b.write(" => ");
45        self.write_curly_statement_block(b, level);
46        b.end_line();
47    }
48}