1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::{Expression, Builder};
use std::fmt::{Display, Formatter, Result};
#[derive(Debug)]
pub struct IfElse {
condition: Expression,
then: Builder,
otherwise: Option<Builder>,
}
impl IfElse {
pub fn new(condition: Expression) -> Self {
Self {
condition,
then: Builder::new(),
otherwise: None,
}
}
pub fn condition(&mut self, condition: Expression) -> &mut Self {
self.condition = condition;
self
}
pub fn then(&mut self, then: Builder) -> &mut Self {
self.then = then;
self
}
pub fn otherwise(&mut self, otherwise: Builder) -> &mut Self {
self.otherwise = Some(otherwise);
self
}
}
impl Display for IfElse {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "if ({}) {{\n{}\n}}{}\n",
self.condition,
self.then,
if let Some(otherwise) = &self.otherwise {
format!(" else {{\n{}\n}}", otherwise)
} else {
"".to_owned()
}
)
}
}