use super::{fmt, hasher, Box, CodeBlock, Digest};
#[derive(Clone, Debug)]
pub struct Split {
branches: Box<[CodeBlock; 2]>,
hash: Digest,
}
impl Split {
pub fn new(t_branch: CodeBlock, f_branch: CodeBlock) -> Self {
let hash = hasher::merge(&[t_branch.hash(), f_branch.hash()]);
Self {
branches: Box::new([t_branch, f_branch]),
hash,
}
}
pub fn hash(&self) -> Digest {
self.hash
}
pub fn on_true(&self) -> &CodeBlock {
&self.branches[0]
}
pub fn on_false(&self) -> &CodeBlock {
&self.branches[1]
}
}
impl fmt::Display for Split {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"if.true {} else {} end",
self.branches[0], self.branches[1]
)
}
}