toasty_core/stmt/
op_binary.rs

1use std::fmt;
2
3#[derive(Copy, Clone, PartialEq, Eq, Hash)]
4pub enum BinaryOp {
5    Eq,
6    Ne,
7    Ge,
8    Gt,
9    Le,
10    Lt,
11}
12
13impl BinaryOp {
14    pub fn is_eq(self) -> bool {
15        matches!(self, Self::Eq)
16    }
17
18    pub fn is_ne(self) -> bool {
19        matches!(self, Self::Ne)
20    }
21
22    pub fn reverse(&mut self) {
23        match *self {
24            Self::Eq => {}
25            _ => todo!(),
26        }
27    }
28
29    /// Returns the logical negation of this operator, if one exists.
30    ///
31    /// - `=` → `!=`
32    /// - `!=` → `=`
33    /// - `<` → `>=`
34    /// - `>=` → `<`
35    /// - `>` → `<=`
36    /// - `<=` → `>`
37    pub fn negate(self) -> Option<Self> {
38        match self {
39            Self::Eq => Some(Self::Ne),
40            Self::Ne => Some(Self::Eq),
41            Self::Lt => Some(Self::Ge),
42            Self::Ge => Some(Self::Lt),
43            Self::Gt => Some(Self::Le),
44            Self::Le => Some(Self::Gt),
45        }
46    }
47
48    /// Returns the operator that represents an equivalent comparison when the
49    /// operands are commuted (swapped).
50    ///
51    /// For example, `5 < x` becomes `x > 5`, so `Lt.commute()` returns `Gt`.
52    /// Symmetric operators like `Eq` and `Ne` return themselves.
53    pub fn commute(self) -> Self {
54        match self {
55            Self::Eq => Self::Eq,
56            Self::Ne => Self::Ne,
57            Self::Ge => Self::Le,
58            Self::Gt => Self::Lt,
59            Self::Le => Self::Ge,
60            Self::Lt => Self::Gt,
61        }
62    }
63}
64
65impl fmt::Display for BinaryOp {
66    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67        match self {
68            BinaryOp::Eq => "=".fmt(f),
69            BinaryOp::Ne => "!=".fmt(f),
70            BinaryOp::Ge => ">=".fmt(f),
71            BinaryOp::Gt => ">".fmt(f),
72            BinaryOp::Le => "<=".fmt(f),
73            BinaryOp::Lt => "<".fmt(f),
74        }
75    }
76}
77
78impl fmt::Debug for BinaryOp {
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        fmt::Display::fmt(self, f)
81    }
82}