Skip to main content

tiger_lib/block/
comparator.rs

1use std::fmt::{Display, Error, Formatter};
2use std::str::FromStr;
3
4use crate::block::comparator::Eq::*;
5
6#[derive(Copy, Clone, Debug, PartialEq)]
7pub enum Comparator {
8    /// =, ?=, ==,
9    Equals(Eq),
10    /// !=
11    NotEquals,
12    /// <
13    LessThan,
14    /// >
15    GreaterThan,
16    /// <=
17    AtMost,
18    /// >=
19    AtLeast,
20}
21
22#[derive(Copy, Clone, Debug, PartialEq)]
23pub enum Eq {
24    /// Notation: =
25    /// Valid as an equality comparison operator, assignment operator and scope opener.
26    Single,
27    /// Notation: ==
28    /// Only valid as an equality comparison operator.
29    Double,
30    /// Notation: ?=
31    /// Valid as a conditional equality comparison operator and condition scope opener.
32    Question,
33}
34
35#[derive(Debug, Clone, Copy)]
36pub struct UnknownComparatorError;
37
38impl FromStr for Comparator {
39    type Err = UnknownComparatorError;
40
41    fn from_str(s: &str) -> Result<Self, UnknownComparatorError> {
42        match s {
43            "=" => Ok(Comparator::Equals(Single)),
44            "==" => Ok(Comparator::Equals(Double)),
45            "?=" => Ok(Comparator::Equals(Question)),
46            "<" => Ok(Comparator::LessThan),
47            ">" => Ok(Comparator::GreaterThan),
48            "<=" => Ok(Comparator::AtMost),
49            ">=" => Ok(Comparator::AtLeast),
50            "!=" => Ok(Comparator::NotEquals),
51            _ => Err(UnknownComparatorError),
52        }
53    }
54}
55
56impl Display for Comparator {
57    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
58        match *self {
59            Comparator::Equals(Single) => write!(f, "="),
60            Comparator::Equals(Double) => write!(f, "=="),
61            Comparator::Equals(Question) => write!(f, "?="),
62            Comparator::LessThan => write!(f, "<"),
63            Comparator::GreaterThan => write!(f, ">"),
64            Comparator::AtMost => write!(f, "<="),
65            Comparator::AtLeast => write!(f, ">="),
66            Comparator::NotEquals => write!(f, "!="),
67        }
68    }
69}