use super::Arity;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ComparisonOperator {
Equal,
NotEqual,
Less,
Greater,
LessOrEqual,
GreaterOrEqual,
}
impl ComparisonOperator {
pub const ALL: [Self; 6] = [
Self::Equal,
Self::NotEqual,
Self::Less,
Self::Greater,
Self::LessOrEqual,
Self::GreaterOrEqual,
];
#[must_use]
pub const fn symbol(self) -> &'static str {
match self {
Self::Equal => "==",
Self::NotEqual => "!=",
Self::Less => "<",
Self::Greater => ">",
Self::LessOrEqual => "<=",
Self::GreaterOrEqual => ">=",
}
}
#[must_use]
#[allow(
clippy::unused_self,
reason = "uniform surface across operator families"
)]
pub const fn arity(self) -> Arity {
Arity::Binary
}
#[must_use]
#[allow(
clippy::unused_self,
reason = "uniform surface across operator families"
)]
pub const fn precedence(self) -> u8 {
5
}
}
#[cfg(test)]
mod tests {
use super::ComparisonOperator;
#[test]
fn all_comparisons_share_one_binding_level() {
let levels: Vec<u8> = ComparisonOperator::ALL
.iter()
.map(|op| op.precedence())
.collect();
assert!(levels.windows(2).all(|pair| pair.first() == pair.last()));
}
#[test]
fn every_comparison_has_a_distinct_symbol() {
let mut symbols: Vec<&str> = ComparisonOperator::ALL
.iter()
.map(|op| op.symbol())
.collect();
symbols.sort_unstable();
let count = symbols.len();
symbols.dedup();
assert_eq!(symbols.len(), count);
}
}