tiger_lib/block/
comparator.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::fmt::{Display, Error, Formatter};
use std::str::FromStr;

use crate::block::comparator::Eq::*;

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Comparator {
    /// =, ?=, ==,
    Equals(Eq),
    /// !=
    NotEquals,
    /// <
    LessThan,
    /// >
    GreaterThan,
    /// <=
    AtMost,
    /// >=
    AtLeast,
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Eq {
    /// Notation: =
    /// Valid as an equality comparison operator, assignment operator and scope opener.
    Single,
    /// Notation: ==
    /// Only valid as an equality comparison operator.
    Double,
    /// Notation: ?=
    /// Valid as a conditional equality comparison operator and condition scope opener.
    Question,
}

pub struct UnknownComparatorError;

impl FromStr for Comparator {
    type Err = UnknownComparatorError;

    fn from_str(s: &str) -> Result<Self, UnknownComparatorError> {
        match s {
            "=" => Ok(Comparator::Equals(Single)),
            "==" => Ok(Comparator::Equals(Double)),
            "?=" => Ok(Comparator::Equals(Question)),
            "<" => Ok(Comparator::LessThan),
            ">" => Ok(Comparator::GreaterThan),
            "<=" => Ok(Comparator::AtMost),
            ">=" => Ok(Comparator::AtLeast),
            "!=" => Ok(Comparator::NotEquals),
            _ => Err(UnknownComparatorError),
        }
    }
}

impl Display for Comparator {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        match *self {
            Comparator::Equals(Single) => write!(f, "="),
            Comparator::Equals(Double) => write!(f, "=="),
            Comparator::Equals(Question) => write!(f, "?="),
            Comparator::LessThan => write!(f, "<"),
            Comparator::GreaterThan => write!(f, ">"),
            Comparator::AtMost => write!(f, "<="),
            Comparator::AtLeast => write!(f, ">="),
            Comparator::NotEquals => write!(f, "!="),
        }
    }
}