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
use crate::nodes::{
    Block,
    Expression,
    Statement,
};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IfBranch {
    pub condition: Expression,
    pub block: Block,
}

impl From<(Expression, Block)> for IfBranch {
    fn from((condition, block): (Expression, Block)) -> Self {
        Self {
            condition,
            block,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct IfStatement {
    pub branches: Vec<IfBranch>,
    pub else_block: Option<Block>,
}

impl From<(Vec<(Expression, Block)>, Option<Block>)> for IfStatement {
    fn from((branches, else_block): (Vec<(Expression, Block)>, Option<Block>)) -> Self {
        Self {
            branches: branches.into_iter().map(IfBranch::from).collect(),
            else_block,
        }
    }
}

impl Into<Statement> for IfStatement {
    fn into(self) -> Statement {
        Statement::If(self)
    }
}