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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use crate::types::error::CruxError;
use edn_rs::Serialize;
use std::collections::BTreeSet;
use std::convert::TryFrom;

/// A [`Query`](https://opencrux.com/reference/queries.html) is a special kind of body that we submit to the `query` function. It has the following fields:
/// * `find` is responsible for defining which elements of the query you want shown in the response, it is **required**. Argument is a vector with elements to be queried, `vec!["a", "b", "c"]`. It is parsed as `:find [a b c]`, qhere `a, b, c` are the elements defined in `where` clause.
/// * `where_clause` is responsible for defining which rules will be applied to filter elements, it is **required**. Argument is a vector with the strings containing the filtering function, `vec!["a :db-key1 b", "a :db-key2 c", "a :db-key3 <some value>"]`. It is parsed as `:where [ [a :db-key1 b] [a :db-key2 c] [a :db-key3 <some value>] ]`.
/// * `args` is responsible for defining arguments to be replaced in `where_clause`, **optional**. Argument is a vector with strings containing the matches `vec!["?n \"Ivan\" ?l \"Ivanov\"", "?n \"Petr\" ?l \"Petrov\""]`.
/// * `order_by` is responsible for defining the order in which the response will be represented, **optional**. Argument is a vector with strings containing the element and how to order (`:asc` or `:desc`) `vec!["time :desc", "device-id :asc"]`.
/// * `limit` is responsible for defining the limit size of the response, **optional**. Argument is a usize.
/// * `offset` is responsible for defining the offset of the response, **optional**. Argument is a usize.
#[derive(Clone, Debug)]
pub struct Query {
    find: Find,
    where_: Option<Where>,
    args: Option<Args>,
    order_by: Option<OrderBy>,
    limit: Option<Limit>,
    offset: Option<Offset>,
    full_results: bool,
}
#[derive(Clone, Debug)]
struct Find(Vec<String>);
#[derive(Clone, Debug)]
struct Where(Vec<String>);
#[derive(Clone, Debug)]
struct Args(Vec<String>);
#[derive(Clone, Debug)]
struct OrderBy(Vec<String>);
#[derive(Clone, Debug)]
struct Limit(usize);
#[derive(Clone, Debug)]
struct Offset(usize);

impl Query {
    /// `find` is the function responsible for defining the `:find` key in the query.
    /// Input should be the elements to be queried by the `where_clause`.
    /// Ex: `vec!["time", "device-id", "temperature", "humidity"]`.
    /// Becomes: `:find [time, device-id, temperature, humidity]`.
    ///
    /// Error cases:
    /// * All elements should start with `?`, example `vec!["?p1", "?n", "?g"]`. If theey do not start the CruxError::QueryFormatError containing `All elements of find clause should start with '?', element '{}' doesn't conform` is thrown.
    pub fn find(find: Vec<&str>) -> Result<Self, CruxError> {
        if find.iter().any(|e| !e.starts_with("?")) {
            let error = find.iter().find(|e| !e.starts_with("?")).unwrap();
            return Err(CruxError::QueryFormatError(format!(
                "All elements of find clause should start with '?', element '{}' doesn't conform",
                error
            )));
        }

        Ok(Self {
            find: Find {
                0: find.into_iter().map(String::from).collect::<Vec<String>>(),
            },
            where_: None,
            args: None,
            order_by: None,
            limit: None,
            offset: None,
            full_results: false,
        })
    }

    /// `where_clause` is the function responsible for defining the required `:where` key in the query.
    /// Input should be `element1 :key element2`, `element2` may have a modifier like `#inst`. The order matters.
    /// Ex: `vec!["c :condition/time time", "c :condition/device-id device-id", "c :condition/temperature temperature", "c :condition/humidity humidity"]`.
    /// Becomes:
    /// `:where [[c :condition/time time] [c :condition/device-id device-id] [c :condition/temperature temperature] [c :condition/humidity humidity]]`.
    ///
    /// Error cases:
    /// * All elements present in find clause should be present in where clause. If your find clause is `"?p", "?n", "?s"`, and your where clause is `"?p1 :alpha ?n", "?p1 :beta true"` an error `Not all element of find, `"?p", "?n", "?s"`, are present in the where clause, ?s is missing` is thrown.
    pub fn where_clause(mut self, where_: Vec<&str>) -> Result<Self, CruxError> {
        if self.find.0.iter().any(|e| !where_.join(" ").contains(e)) {
            let error = self
                .find
                .0
                .iter()
                .find(|e| !where_.join(" ").contains(*e))
                .unwrap();
            return Err(CruxError::QueryFormatError(format!(
                "Not all element of find, {}, are present in the where clause, {} is missing",
                self.find.0.join(", "),
                error
            )));
        }
        let w = where_
            .iter()
            .map(|s| s.replace("[", "").replace("]", ""))
            .collect::<Vec<String>>();
        self.where_ = Some(Where { 0: w });
        Ok(self)
    }

    /// `args` is the function responsible for defining the optional `:args` key in the query.
    /// Input are elements you want to replace in the `where_clause`, a good practice is to name them with `?` before.
    /// Ex: `vec!["?n \"Ivan\" ?l \"Ivanov\"", "?n \"Petr\" ?l \"Petrov\""]`.
    /// Becomes: `:args [{?n "Ivan" ?l "Ivanov"} {?n "Petr" ?l "Petrov"}]`.
    ///
    /// Error cases:
    /// * The first element of the argument key-value tuple should start with `?`. An input `vec!["n true"]` will return an error `All elements should start with '?'`.
    /// * All arguments key should be present in the where clause. If the where clause `?p1 :name ?n", "?p1 :is-sql ?s", "?p1 :is-sql true"` and an args clause `vec!["?s true ?x 1243"]` will return an error `All elements should be present in where clause`.
    pub fn args(mut self, args: Vec<&str>) -> Result<Self, CruxError> {
        let where_ = self.where_.clone().unwrap().0.join(" ");
        self.args = Some(Args::try_from((args, where_))?);
        Ok(self)
    }

    /// `order_by` is the function responsible for defining the optional `:order-by` key in the query.
    /// Input is the elements to be ordered by, the first element is the first order, the second is the further orthers. Allowed keys are `:Asc`and `:desc`.
    /// Ex: `vec!["time :desc", "device-id :asc"]`.
    /// Becomes: `:order-by [[time :desc] [device-id :asc]]`.
    ///
    /// Error cases:
    /// * The second element of each order clause should be `:asc` or `:desc`, if different, like `:eq` in `"?p1 :asc", "?n :desc", "?s :eq"`, error `Order element should be ':asc' or ':desc'` is thrown.
    /// * The first element of each order clause should be present in the find clause. If the order clause is `"?p1 :asc", "?n :desc", "?g :asc"` and the find clause is `"?p1", "?n"` the error `All elements to be ordered should be present in find clause, ?g not present` is thrown.
    pub fn order_by(mut self, order_by: Vec<&str>) -> Result<Self, CruxError> {
        let f = self.find.0.join(" ");
        if !order_by
            .iter()
            .map(|e| e.split(" ").collect::<Vec<&str>>())
            .map(|e| e[1])
            .all(|e| e.to_lowercase() == ":asc" || e.to_lowercase() == ":desc")
        {
            return Err(CruxError::QueryFormatError(
                "Order element should be ':asc' or ':desc'".to_string(),
            ));
        }
        if !order_by
            .iter()
            .map(|e| e.split(" ").collect::<Vec<&str>>())
            .map(|e| e[0])
            .all(|e| f.contains(e))
        {
            let error = order_by
                .iter()
                .map(|e| e.split(" ").collect::<Vec<&str>>())
                .map(|e| e[0])
                .find(|e| !f.contains(e))
                .unwrap();
            return Err(CruxError::QueryFormatError(format!(
                "All elements to be ordered should be present in find clause, {} not present",
                error
            )));
        }

        let o = order_by
            .iter()
            .map(|s| s.replace("[", "").replace("]", ""))
            .collect::<Vec<String>>();
        self.order_by = Some(OrderBy { 0: o });
        Ok(self)
    }

    /// `limit` is the function responsible for defining the optional `:limit` key in the query.
    /// Input is a usize with the query limit size.
    /// `.limit(5usize)` Becomes: `:limit 5`.
    pub fn limit(mut self, limit: usize) -> Self {
        self.limit = Some(Limit { 0: limit });
        self
    }

    /// `offset` is the function responsible for defining the optional `:offset` key in the query.
    /// Input is a usize with the query offset.
    /// `.offset(5usize)` Becomes: `:offset 5`.
    pub fn offset(mut self, offset: usize) -> Self {
        self.offset = Some(Offset { 0: offset });
        self
    }

    /// `with_full_results` adds `:full-results? true` to the query map to easily retrieve the source documents relating to the entities in the result set.
    pub fn with_full_results(mut self) -> Self {
        self.full_results = true;
        self
    }

    /// `build` function helps you assert that required fields were implemented.
    pub fn build(self) -> Result<Self, CruxError> {
        if self.where_.is_none() {
            Err(CruxError::QueryFormatError(String::from(
                "Where clause is required",
            )))
        } else {
            Ok(self)
        }
    }
}

impl Serialize for Query {
    fn serialize(self) -> String {
        let mut q = String::from("{:query\n {");
        q.push_str(&edn_rs::to_string(self.find));
        q.push_str(&edn_rs::to_string(self.where_.unwrap()));
        if self.args.is_some() {
            q.push_str(&edn_rs::to_string(self.args.unwrap()));
        }
        if self.order_by.is_some() {
            q.push_str(&edn_rs::to_string(self.order_by.unwrap()));
        }
        if self.limit.is_some() {
            q.push_str(&edn_rs::to_string(self.limit.unwrap()));
        }
        if self.offset.is_some() {
            q.push_str(&edn_rs::to_string(self.offset.unwrap()));
        }
        if self.full_results == true {
            q.push_str(" :full-results? true\n")
        }
        q.push_str("}}");
        q
    }
}

impl Serialize for Find {
    fn serialize(self) -> String {
        let mut q = String::from(":find [");
        q.push_str(&self.0.join(" "));
        q.push_str("]\n");
        q
    }
}

impl Serialize for Where {
    fn serialize(self) -> String {
        let mut q = String::from(":where [[");
        q.push_str(&self.0.join("]\n["));
        q.push_str("]]\n");
        q
    }
}

impl Serialize for Args {
    fn serialize(self) -> String {
        let mut q = String::from(":args [{");
        q.push_str(&self.0.join("}\n{"));
        q.push_str("}]\n");
        q
    }
}

impl Serialize for OrderBy {
    fn serialize(self) -> String {
        let mut q = String::from(":order-by [[");
        q.push_str(&self.0.join("]\n["));
        q.push_str("]]\n");
        q
    }
}

impl Serialize for Limit {
    fn serialize(self) -> String {
        let mut q = String::from(":limit ");
        q.push_str(&self.0.to_string());
        q.push_str("\n");
        q
    }
}

impl Serialize for Offset {
    fn serialize(self) -> String {
        let mut q = String::from(":offset ");
        q.push_str(&self.0.to_string());
        q.push_str("\n");
        q
    }
}

type RawArgsWithWhere<'a> = (Vec<&'a str>, String);

impl TryFrom<RawArgsWithWhere<'_>> for Args {
    type Error = CruxError;

    fn try_from(value: RawArgsWithWhere) -> Result<Self, Self::Error> {
        let (args, where_) = value;

        let args_key_set = args_key_bset(&args);

        let all_elements_in_where = args_key_set.iter().any(|e| !where_.contains(e));
        let has_question = args_key_set.iter().any(|e| !e.starts_with("?"));

        match (all_elements_in_where, has_question) {
            (true, false) =>  Err(CruxError::QueryFormatError("All elements should be present in where clause".to_string())),
            (false, true) =>  Err(CruxError::QueryFormatError("All elements should start with '?'".to_string())),
            (true, true) =>  Err(CruxError::QueryFormatError("All elements should be present in where clause and all elements should start with '?'".to_string())),
            (false, false) => Ok(Args{0: args.iter().map(|s| s.replace("{", "").replace("}", "")).collect::<Vec<String>>()}),
        }
    }
}

fn args_key_bset(args: &Vec<&str>) -> BTreeSet<String> {
    let args_without_inst = args.join(" ").replace("#inst", "");
    args_without_inst
        .split(" ")
        .filter(|i| !i.is_empty())
        .enumerate()
        .filter(|(i, _)| i % 2 == 0)
        .map(|(_, s)| s.to_owned())
        .collect::<BTreeSet<String>>()
}

#[cfg(test)]
mod test {
    use super::Query;
    use crate::client::Crux;

    #[test]
    fn query_with_find_and_where() {
        let expected =
            "{:query\n {:find [?p1]\n:where [[?p1 :first-name n]\n[?p1 :last-name \"Jorge\"]]\n}}";
        let q = Query::find(vec!["?p1"])
            .unwrap()
            .where_clause(vec!["?p1 :first-name n", "?p1 :last-name \"Jorge\""])
            .unwrap()
            .build();

        assert_eq!(edn_rs::to_string(q.unwrap()), expected);
    }

    #[test]
    #[should_panic(expected = "Where clause is required")]
    fn expect_query_format_error() {
        let client = Crux::new("", "").http_client();
        let query_where_is_none = Query::find(vec!["?p1", "?n"]).unwrap().build().unwrap();

        let _ = client.query(query_where_is_none).unwrap();
    }

    #[test]
    fn query_with_order() {
        let expected =
            "{:query\n {:find [?p1]\n:where [[?p1 :first-name n]\n[?p1 :last-name \"Jorge\"]]\n:order-by [[?p1 :asc]]\n}}";
        let q = Query::find(vec!["?p1"])
            .unwrap()
            .where_clause(vec!["?p1 :first-name n", "?p1 :last-name \"Jorge\""])
            .unwrap()
            .order_by(vec!["?p1 :asc"])
            .unwrap()
            .build();

        assert_eq!(edn_rs::to_string(q.unwrap()), expected);
    }

    #[test]
    fn query_with_args() {
        let expected =
            "{:query\n {:find [?p1]\n:where [[?p1 :first-name n]\n[?p1 :last-name ?n]]\n:args [{?n \"Jorge\"}]\n}}";
        let q = Query::find(vec!["?p1"])
            .unwrap()
            .where_clause(vec!["?p1 :first-name n", "?p1 :last-name ?n"])
            .unwrap()
            .args(vec!["?n \"Jorge\""])
            .unwrap()
            .build();

        assert_eq!(edn_rs::to_string(q.unwrap()), expected);
    }

    #[test]
    fn query_with_limit_and_offset() {
        let expected =
            "{:query\n {:find [?p1]\n:where [[?p1 :first-name n]\n[?p1 :last-name n]]\n:limit 5\n:offset 10\n}}";
        let q = Query::find(vec!["?p1"])
            .unwrap()
            .where_clause(vec!["?p1 :first-name n", "?p1 :last-name n"])
            .unwrap()
            .limit(5)
            .offset(10)
            .build();

        assert_eq!(edn_rs::to_string(q.unwrap()), expected);
    }

    #[test]
    fn full_query() {
        let expected =
            "{:query\n {:find [?p1]\n:where [[?p1 :first-name n]\n[?p1 :last-name ?n]]\n:args [{?n \"Jorge\"}]\n:order-by [[?p1 :Asc]]\n:limit 5\n:offset 10\n}}";
        let q = Query::find(vec!["?p1"])
            .unwrap()
            .where_clause(vec!["?p1 :first-name n", "?p1 :last-name ?n"])
            .unwrap()
            .args(vec!["?n \"Jorge\""])
            .unwrap()
            .order_by(vec!["?p1 :Asc"])
            .unwrap()
            .limit(5)
            .offset(10)
            .build();

        assert_eq!(edn_rs::to_string(q.unwrap()), expected);
    }

    #[test]
    #[should_panic(
        expected = "Not all element of find, ?p1, ?n, ?s, are present in the where clause, ?n is missing"
    )]
    fn where_query_format_error() {
        let _query = Query::find(vec!["?p1", "?n", "?s"])
            .unwrap()
            .where_clause(vec!["?p1 :name ?g", "?p1 :is-sql ?s", "?p1 :is-sql true"])
            .unwrap()
            .build();
    }

    #[test]
    #[should_panic(expected = "Order element should be \\\':asc\\\' or \\\':desc\\\'")]
    fn order_should_panic_for_unknow_order_element() {
        let _query = Query::find(vec!["?p1", "?n", "?s"])
            .unwrap()
            .where_clause(vec!["?p1 :name ?n", "?p1 :is-sql ?s", "?p1 :is-sql true"])
            .unwrap()
            .order_by(vec!["?p1 :asc", "?n :desc", "?s :eq"])
            .unwrap()
            .build();
    }

    #[test]
    #[should_panic(
        expected = "All elements to be ordered should be present in find clause, ?g not present"
    )]
    fn order_element_should_be_present_in_find_clause() {
        let _query = Query::find(vec!["?p1", "?n", "?s"])
            .unwrap()
            .where_clause(vec!["?p1 :name ?n", "?p1 :is-sql ?s", "?p1 :is-sql true"])
            .unwrap()
            .order_by(vec!["?p1 :asc", "?n :desc", "?g :asc"])
            .unwrap()
            .build();
    }

    #[test]
    #[should_panic(expected = "All elements should be present in where clause")]
    fn all_args_present_in_where() {
        let _query = Query::find(vec!["?p1", "?n"])
            .unwrap()
            .where_clause(vec!["?p1 :name ?n", "?p1 :is-sql ?s", "?p1 :is-sql true"])
            .unwrap()
            .args(vec!["?s true ?x 1243"])
            .unwrap()
            .build();
    }

    #[test]
    #[should_panic(expected = "All elements should start with \\\'?\\\'")]
    fn all_args_should_start_with_question() {
        let _query = Query::find(vec!["?p1", "?n"])
            .unwrap()
            .where_clause(vec!["?p1 :name ?n", "?p1 :is-sql s", "?p1 :is-sql true"])
            .unwrap()
            .args(vec!["s    true"])
            .unwrap()
            .build();
    }

    #[test]
    fn query_with_full_results() {
        let expected =
            "{:query\n {:find [?p1]\n:where [[?p1 :first-name n]\n[?p1 :last-name \"Jorge\"]]\n :full-results? true\n}}";
        let q = Query::find(vec!["?p1"])
            .unwrap()
            .where_clause(vec!["?p1 :first-name n", "?p1 :last-name \"Jorge\""])
            .unwrap()
            .with_full_results()
            .build();

        assert_eq!(edn_rs::to_string(q.unwrap()), expected);
    }
}