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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use core::fmt;

#[derive(Clone, Debug, Default)]
pub struct Query {
    pub preset: Option<ExprPreset>,
    pub source: Option<ExprSource>,
    pub row: Option<ClauseRow>,
    pub request: Option<ClauseRequest>,
    pub response: Option<ClauseResponse>,
    pub and: Option<(Box<Query>, Box<Query>)>,
    pub or: Option<(Box<Query>, Box<Query>)>,
}

impl fmt::Display for Query {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(expr) = &self.preset {
            return write!(f, "{}", expr);
        }
        if let Some(expr) = &self.source {
            return write!(f, "{}", expr);
        }
        if let Some(expr) = &self.row {
            return write!(f, "row.{}", expr);
        }
        if let Some(expr) = &self.request {
            return write!(f, "req.{}", expr);
        }
        if let Some(expr) = &self.response {
            return write!(f, "resp.{}", expr);
        }
        if let Some(expr) = &self.and {
            return write!(f, "({} and {})", expr.0, expr.1);
        }
        if let Some(expr) = &self.or {
            return write!(f, "({} or {})", expr.0, expr.1);
        }
        write!(f, "()")
    }
}

#[derive(Clone, Debug, Default)]
pub struct ClauseRow {
    pub id: Option<ExprInt>,
}

impl fmt::Display for ClauseRow {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(expr) = &self.id {
            return write!(f, "id.{}", expr);
        }
        Ok(())
    }
}

#[derive(Clone, Debug, Default)]
pub struct ClauseResponse {
    pub raw: Option<ExprString>,
    pub status_code: Option<ExprInt>,
    pub roundtrip_time: Option<ExprInt>,
}

impl fmt::Display for ClauseResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(expr) = &self.raw {
            return write!(f, "raw.{}", expr);
        }
        if let Some(expr) = &self.status_code {
            return write!(f, "code.{}", expr);
        }
        if let Some(expr) = &self.roundtrip_time {
            return write!(f, "roundtrip.{}", expr);
        }
        Err(fmt::Error)
    }
}

#[derive(Clone, Debug, Default)]
pub struct ClauseRequest {
    pub created_at: Option<ExprDate>,
    pub file_extension: Option<ExprString>,
    pub host: Option<ExprString>,
    pub is_tls: Option<ExprBool>,
    pub method: Option<ExprString>,
    pub path: Option<ExprString>,
    pub port: Option<ExprInt>,
    pub query: Option<ExprString>,
    pub raw: Option<ExprString>,
}

impl fmt::Display for ClauseRequest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(expr) = &self.created_at {
            return write!(f, "created_at.{}", expr);
        }
        if let Some(expr) = &self.file_extension {
            return write!(f, "ext.{}", expr);
        }
        if let Some(expr) = &self.host {
            return write!(f, "host.{}", expr);
        }
        if let Some(expr) = &self.is_tls {
            return write!(f, "tls.{}", expr);
        }
        if let Some(expr) = &self.method {
            return write!(f, "method.{}", expr);
        }
        if let Some(expr) = &self.path {
            return write!(f, "path.{}", expr);
        }
        if let Some(expr) = &self.port {
            return write!(f, "port.{}", expr);
        }
        if let Some(expr) = &self.query {
            return write!(f, "query.{}", expr);
        }
        if let Some(expr) = &self.raw {
            return write!(f, "raw.{}", expr);
        }
        Err(fmt::Error)
    }
}

#[derive(Clone, Debug)]
pub struct ExprString {
    pub value: String,
    pub operator: OperatorString,
    pub is_raw: bool,
}

impl fmt::Display for ExprString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_raw {
            write!(f, r"{}:/{}/", self.operator, self.value)
        } else {
            write!(f, "{}:\"{}\"", self.operator, self.value)
        }
    }
}

#[derive(Clone, Debug)]
pub enum OperatorString {
    Eq,
    Ne,
    Cont,
    Ncont,
    Like,
    Nlike,
    Regex,
    Nregex,
}

impl fmt::Display for OperatorString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OperatorString::Eq => write!(f, "eq"),
            OperatorString::Ne => write!(f, "ne"),
            OperatorString::Cont => write!(f, "cont"),
            OperatorString::Ncont => write!(f, "ncont"),
            OperatorString::Like => write!(f, "like"),
            OperatorString::Nlike => write!(f, "nlike"),
            OperatorString::Regex => write!(f, "regex"),
            OperatorString::Nregex => write!(f, "nregex"),
        }
    }
}

#[derive(Clone, Debug)]
pub struct ExprInt {
    pub value: i32,
    pub operator: OperatorInt,
}

impl fmt::Display for ExprInt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.operator, self.value)
    }
}

#[derive(Clone, Debug)]
pub enum OperatorInt {
    Lt,
    Lte,
    Gt,
    Gte,
    Eq,
    Ne,
}

impl fmt::Display for OperatorInt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OperatorInt::Lt => write!(f, "lt"),
            OperatorInt::Lte => write!(f, "lte"),
            OperatorInt::Gt => write!(f, "gt"),
            OperatorInt::Gte => write!(f, "gte"),
            OperatorInt::Eq => write!(f, "eq"),
            OperatorInt::Ne => write!(f, "ne"),
        }
    }
}

#[derive(Clone, Debug)]
pub struct ExprDate {
    pub value: String,
    pub operator: OperatorDate,
}

impl fmt::Display for ExprDate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:\"{}\"", self.operator, self.value)
    }
}

#[derive(Clone, Debug)]
pub enum OperatorDate {
    Lt,
    Gt,
}

impl fmt::Display for OperatorDate {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OperatorDate::Lt => write!(f, "lt"),
            OperatorDate::Gt => write!(f, "gt"),
        }
    }
}

#[derive(Clone, Debug)]
pub struct ExprBool {
    pub value: bool,
    pub operator: OperatorBool,
}

impl fmt::Display for ExprBool {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}:{}", self.operator, self.value)
    }
}

#[derive(Clone, Debug)]
pub enum OperatorBool {
    Eq,
    Ne,
}

impl fmt::Display for OperatorBool {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OperatorBool::Eq => write!(f, "eq"),
            OperatorBool::Ne => write!(f, "ne"),
        }
    }
}

#[derive(Clone, Debug)]
pub enum ExprPreset {
    Name(String),
    Alias(String),
}

impl fmt::Display for ExprPreset {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ExprPreset::Name(name) => write!(f, "preset:\"{}\"", name),
            ExprPreset::Alias(alias) => write!(f, "preset:{}", alias),
        }
    }
}

#[derive(Clone, Debug)]
pub struct ExprSource {
    pub name: String,
}

impl fmt::Display for ExprSource {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "source:\"{}\"", self.name)
    }
}