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
35pub struct UnknownComparatorError;
36
37impl FromStr for Comparator {
38    type Err = UnknownComparatorError;
39
40    fn from_str(s: &str) -> Result<Self, UnknownComparatorError> {
41        match s {
42            "=" => Ok(Comparator::Equals(Single)),
43            "==" => Ok(Comparator::Equals(Double)),
44            "?=" => Ok(Comparator::Equals(Question)),
45            "<" => Ok(Comparator::LessThan),
46            ">" => Ok(Comparator::GreaterThan),
47            "<=" => Ok(Comparator::AtMost),
48            ">=" => Ok(Comparator::AtLeast),
49            "!=" => Ok(Comparator::NotEquals),
50            _ => Err(UnknownComparatorError),
51        }
52    }
53}
54
55impl Display for Comparator {
56    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
57        match *self {
58            Comparator::Equals(Single) => write!(f, "="),
59            Comparator::Equals(Double) => write!(f, "=="),
60            Comparator::Equals(Question) => write!(f, "?="),
61            Comparator::LessThan => write!(f, "<"),
62            Comparator::GreaterThan => write!(f, ">"),
63            Comparator::AtMost => write!(f, "<="),
64            Comparator::AtLeast => write!(f, ">="),
65            Comparator::NotEquals => write!(f, "!="),
66        }
67    }
68}