use crate::{Block, Expression, Indent, Node, NodeID, Statement};
use leo_span::Span;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct ConditionalStatement {
pub condition: Expression,
pub then: Block,
pub otherwise: Option<Box<Statement>>,
pub span: Span,
pub id: NodeID,
}
impl fmt::Display for ConditionalStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "if {} {{", self.condition)?;
for stmt in self.then.statements.iter() {
writeln!(f, "{}{}", Indent(stmt), stmt.semicolon())?;
}
match self.otherwise.as_deref() {
None => write!(f, "}}")?,
Some(Statement::Block(block)) => {
writeln!(f, "}} else {{")?;
for stmt in block.statements.iter() {
writeln!(f, "{}{}", Indent(stmt), stmt.semicolon())?;
}
write!(f, "}}")?;
}
Some(Statement::Conditional(cond)) => {
write!(f, "}} else {cond}")?;
}
Some(_) => panic!("`otherwise` of a `ConditionalStatement` must be a block or conditional."),
}
Ok(())
}
}
impl From<ConditionalStatement> for Statement {
fn from(value: ConditionalStatement) -> Self {
Statement::Conditional(value)
}
}
crate::simple_node_impl!(ConditionalStatement);