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
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}