tiger_lib/block/
comparator.rs1use 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 Equals(Eq),
10 NotEquals,
12 LessThan,
14 GreaterThan,
16 AtMost,
18 AtLeast,
20}
21
22#[derive(Copy, Clone, Debug, PartialEq)]
23pub enum Eq {
24 Single,
27 Double,
30 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}