Skip to main content

toasty_core/stmt/
op_binary.rs

1use std::fmt;
2
3/// A binary comparison operator.
4///
5/// Used by [`ExprBinaryOp`](super::ExprBinaryOp) to specify the comparison
6/// applied between two expressions.
7///
8/// # Examples
9///
10/// ```
11/// use toasty_core::stmt::BinaryOp;
12///
13/// let op = BinaryOp::Eq;
14/// assert!(op.is_eq());
15/// assert_eq!(op.to_string(), "=");
16///
17/// // Negation
18/// assert_eq!(op.negate(), Some(BinaryOp::Ne));
19///
20/// // Commutation (swapping operands)
21/// assert_eq!(BinaryOp::Lt.commute(), BinaryOp::Gt);
22/// ```
23#[derive(Copy, Clone, PartialEq, Eq, Hash)]
24pub enum BinaryOp {
25    /// Equality (`=`).
26    Eq,
27    /// Inequality (`!=`).
28    Ne,
29    /// Greater than or equal (`>=`).
30    Ge,
31    /// Greater than (`>`).
32    Gt,
33    /// Less than or equal (`<=`).
34    Le,
35    /// Less than (`<`).
36    Lt,
37}
38
39impl BinaryOp {
40    /// Returns `true` if this is the equality operator.
41    pub fn is_eq(self) -> bool {
42        matches!(self, Self::Eq)
43    }
44
45    /// Returns `true` if this is the inequality operator.
46    pub fn is_ne(self) -> bool {
47        matches!(self, Self::Ne)
48    }
49
50    /// Reverses the operator in place (currently only supports `Eq`).
51    ///
52    /// # Panics
53    ///
54    /// Panics (via `todo!()`) for operators other than `Eq`.
55    pub fn reverse(&mut self) {
56        match *self {
57            Self::Eq => {}
58            _ => todo!(),
59        }
60    }
61
62    /// Returns the logical negation of this operator, if one exists.
63    ///
64    /// - `=` → `!=`
65    /// - `!=` → `=`
66    /// - `<` → `>=`
67    /// - `>=` → `<`
68    /// - `>` → `<=`
69    /// - `<=` → `>`
70    pub fn negate(self) -> Option<Self> {
71        match self {
72            Self::Eq => Some(Self::Ne),
73            Self::Ne => Some(Self::Eq),
74            Self::Lt => Some(Self::Ge),
75            Self::Ge => Some(Self::Lt),
76            Self::Gt => Some(Self::Le),
77            Self::Le => Some(Self::Gt),
78        }
79    }
80
81    /// Returns the operator that represents an equivalent comparison when the
82    /// operands are commuted (swapped).
83    ///
84    /// For example, `5 < x` becomes `x > 5`, so `Lt.commute()` returns `Gt`.
85    /// Symmetric operators like `Eq` and `Ne` return themselves.
86    pub fn commute(self) -> Self {
87        match self {
88            Self::Eq => Self::Eq,
89            Self::Ne => Self::Ne,
90            Self::Ge => Self::Le,
91            Self::Gt => Self::Lt,
92            Self::Le => Self::Ge,
93            Self::Lt => Self::Gt,
94        }
95    }
96}
97
98impl fmt::Display for BinaryOp {
99    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100        match self {
101            BinaryOp::Eq => "=".fmt(f),
102            BinaryOp::Ne => "!=".fmt(f),
103            BinaryOp::Ge => ">=".fmt(f),
104            BinaryOp::Gt => ">".fmt(f),
105            BinaryOp::Le => "<=".fmt(f),
106            BinaryOp::Lt => "<".fmt(f),
107        }
108    }
109}
110
111impl fmt::Debug for BinaryOp {
112    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113        fmt::Display::fmt(self, f)
114    }
115}