lp_types/constraints/
display.rs

1use super::*;
2
3impl<T: Display> Display for LinearConstraint<T> {
4    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
5        match self {
6            LinearConstraint::None => f.write_char('∀'),
7            LinearConstraint::Greater { lower, inclusive } => {
8                if *inclusive {
9                    write!(f, "≥ {}", lower)
10                }
11                else {
12                    write!(f, "> {}", lower)
13                }
14            }
15            LinearConstraint::Less { upper, inclusive } => {
16                if *inclusive {
17                    write!(f, "≤ {}", upper)
18                }
19                else {
20                    write!(f, "< {}", upper)
21                }
22            }
23            LinearConstraint::Equal { value } => {
24                write!(f, "= {}", value)
25            }
26            LinearConstraint::NotEqual { value } => {
27                write!(f, "≠ {}", value)
28            }
29            LinearConstraint::Or { left, right } => match should_add_parentheses(left, right) {
30                true => write!(f, "({} ∨ {})", left, right),
31                false => write!(f, "{} ∨ {}", left, right),
32            },
33            LinearConstraint::And { left, right } => match should_add_parentheses(left, right) {
34                true => write!(f, "({} ∧ {})", left, right),
35                false => write!(f, "{} ∧ {}", left, right),
36            },
37        }
38    }
39}
40
41fn should_add_parentheses<T>(lhs: &LinearConstraint<T>, rhs: &LinearConstraint<T>) -> bool {
42    match (lhs, rhs) {
43        (LinearConstraint::Or { .. }, _) => true,
44        (LinearConstraint::And { .. }, _) => true,
45        _ => false,
46    }
47}