rs_teststand/expression/operator/
logical.rs1use super::Arity;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub enum LogicalOperator {
12 And,
14 Or,
16 Not,
18}
19
20impl LogicalOperator {
21 pub const ALL: [Self; 3] = [Self::And, Self::Or, Self::Not];
23
24 #[must_use]
26 pub const fn symbol(self) -> &'static str {
27 match self {
28 Self::And => "&&",
29 Self::Or => "||",
30 Self::Not => "!",
31 }
32 }
33
34 #[must_use]
36 pub const fn arity(self) -> Arity {
37 match self {
38 Self::Not => Arity::Unary,
39 _ => Arity::Binary,
40 }
41 }
42
43 #[must_use]
45 pub const fn precedence(self) -> u8 {
46 match self {
47 Self::Not => 1,
48 Self::And => 9,
49 Self::Or => 10,
50 }
51 }
52}
53
54#[cfg(test)]
55mod tests {
56 use super::LogicalOperator;
57
58 #[test]
59 fn and_binds_tighter_than_or() {
60 assert!(LogicalOperator::And.precedence() < LogicalOperator::Or.precedence());
62 }
63
64 #[test]
65 fn logical_operators_bind_looser_than_every_bitwise_one() {
66 use crate::expression::BitwiseOperator;
69 assert!(BitwiseOperator::Or.precedence() < LogicalOperator::And.precedence());
70 }
71
72 #[test]
73 fn the_symbols_are_doubled_to_distinguish_them_from_bitwise() {
74 assert_eq!(LogicalOperator::And.symbol(), "&&");
75 assert_eq!(LogicalOperator::Or.symbol(), "||");
76 }
77}