use core::fmt::{self, Write};
use super::Condition;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Parenthetical {
pub(crate) condition: Box<Condition>,
}
impl Parenthetical {
}
impl<T> From<T> for Parenthetical
where
T: Into<Box<Condition>>,
{
fn from(condition: T) -> Self {
Self {
condition: condition.into(),
}
}
}
impl fmt::Display for Parenthetical {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_char('(')?;
self.condition.fmt(f)?;
f.write_char(')')
}
}
#[cfg(test)]
mod test {
use std::io::{self, Write};
use pretty_assertions::assert_str_eq;
use crate::condition::test::cmp_a_gt_b;
#[test]
fn parentheses() {
let expr = cmp_a_gt_b();
for i in 0..3 {
let mut wrapped = expr.clone().parenthesize();
for _ in 0..i {
wrapped = wrapped.parenthesize();
}
println!("{i}: {wrapped}");
io::stdout().lock().flush().unwrap();
assert_str_eq!(
match i {
0 => format!("({expr})"),
1 => format!("(({expr}))"),
2 => format!("((({expr})))"),
_ => unreachable!(),
},
wrapped.to_string(),
"The `Display` output wasn't what was expected."
);
}
}
}