summaryrefslogtreecommitdiff
path: root/src/msg.rs
blob: ead8ac089794401e6269a0ef1e3b33eb1c8a7062 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::fmt;
use serde_json::Value;

use parser::{parse_filter,parse_arg,parse_message};


#[derive(Debug,Clone)]
pub enum Arg {
    BareString(String),
    Json(Value),
    Filter(Filter),
}

#[derive(Debug,Clone)]
pub struct Message {
    name: String,
    args: Vec<Arg>,
}


#[derive(Debug,Clone)]
pub enum Filter {
    And(Box<Filter>, Box<Filter>),
    Or(Box<Filter>, Box<Filter>),
    Expr(String, Op, Value),
}


#[derive(Debug,Clone,Copy,PartialEq,Eq)]
pub enum Op {
    Eq,
    NEq,
    Le,
    LEq,
    Gt,
    GEq,
    Fuzzy,
}


impl Op {
    pub fn as_str(self) -> &'static str {
        match self {
            Op::Eq    => "=",
            Op::NEq   => "!=",
            Op::Le    => "<",
            Op::LEq   => "<=",
            Op::Gt    => ">",
            Op::GEq   => ">=",
            Op::Fuzzy => "~",
        }
    }
}


impl fmt::Display for Op {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.as_str())
    }
}


impl fmt::Display for Filter {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &Filter::And(ref x, ref y) => write!(f, "({} and {})", x, y),
            &Filter::Or(ref x, ref y) => write!(f, "({} or {})", x, y),
            &Filter::Expr(ref n, o, ref v) => write!(f, "({} {} {})", n, o, v),
        }
    }
}


impl fmt::Display for Arg {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &Arg::BareString(ref x) => f.write_str(&x),
            &Arg::Json(ref x)       => write!(f, "{}", x),
            &Arg::Filter(ref x)     => write!(f, "{}", x),
        }
    }
}


impl fmt::Display for Message {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(&self.name)?;
        for a in self.args.iter() {
            f.write_str(" ")?;
            write!(f, "{}", a)?;
        }
        Ok(())
    }
}


impl Filter {
    pub fn parse(s: &str) -> Result<(Filter, &str), &'static str> {
        parse_filter(s)
    }
}


impl Arg {
    pub fn parse(s: &str) -> Result<(Arg, &str), &'static str> {
        parse_arg(s)
    }
}


impl Message {
    pub fn parse(s: &str) -> Result<Message, &'static str> {
        parse_message(s)
    }

    pub fn new(name: &str) -> Result<Message, &'static str> {
        if name.contains(|c| !(c >= 'a' && c <= 'z')) {
            return Err("Invalid message name")
        }
        Ok(Message{name: name.to_string(), args: Vec::new()})
    }

    pub fn push_arg(mut self, arg: Arg) -> Message { self.args.push(arg); self }
    pub fn push_bare  (self, arg: &str  ) -> Message { self.push_arg(Arg::BareString(arg.to_string())) }
    pub fn push_json  (self, arg: Value ) -> Message { self.push_arg(Arg::Json(arg)) }
    pub fn push_filter(self, arg: Filter) -> Message { self.push_arg(Arg::Filter(arg)) }

    pub fn name(&self) -> &str   { &self.name }
    pub fn args(&self) -> &[Arg] { &self.args }
}