cipherstash_config/
operator.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
69
70
71
72
73
74
75
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Operator {
    Lt,
    Lte,
    Eq,
    Gt,
    Gte,
    Like,
    ILike,
    Unsupported,
}

impl Operator {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Lt => "<",
            Self::Lte => "<=",
            Self::Eq => "=",
            Self::Gt => ">",
            Self::Gte => ">=",
            Self::Like => "~~",   //Note: This is PostgreSQL specific syntax.
            Self::ILike => "~~*", //Note: This PostgreSQL specific syntax.
            // TODO: Probably better to use an Error and handle errors in callers
            _ => "unsupported",
        }
    }
}

impl From<&str> for Operator {
    fn from(value: &str) -> Self {
        match value {
            "<" => Self::Lt,
            "<=" => Self::Lte,
            "=" => Self::Eq,
            ">" => Self::Gt,
            ">=" => Self::Gte,
            "~~" => Self::Like,
            "~~*" => Self::ILike,
            _ => Self::Unsupported,
        }
    }
}

impl From<String> for Operator {
    fn from(value: String) -> Self {
        value.as_str().into()
    }
}

impl From<Vec<String>> for Operator {
    fn from(value: Vec<String>) -> Self {
        if value.len() == 1 {
            value[0].to_string().into()
        } else {
            Self::Unsupported
        }
    }
}

impl std::fmt::Display for Operator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let text = match self {
            Self::Lt => "<",
            Self::Lte => "<=",
            Self::Eq => "==",
            Self::Gt => ">",
            Self::Gte => ">=",
            Self::Like => "LIKE",
            Self::ILike => "ILIKE",
            Self::Unsupported => "Unsupported",
        };

        write!(f, "{text}")
    }
}