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
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

/// The language of the filter expression.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(tag = "filter-lang", content = "filter")]
pub enum Filter {
    /// `cql2-text`
    #[serde(rename = "cql2-text")]
    Cql2Text(String),

    /// `cql2-json`
    #[serde(rename = "cql2-json")]
    Cql2Json(Map<String, Value>),
}

#[cfg(test)]
mod tests {
    use super::Filter;
    use serde_json::json;

    #[test]
    fn json() {
        let filter = Filter::Cql2Json(json!({
                  "filter": {
                    "op" : "and",
                    "args": [
                      {
                        "op": "=",
                        "args": [ { "property": "id" }, "LC08_L1TP_060247_20180905_20180912_01_T1_L1TP" ]
                      },
                      {
                        "op": "=",
                        "args" : [ { "property": "collection" }, "landsat8_l1tp" ]
                      }
                    ]
                  }
                }
            ).as_object().unwrap().clone(),
        );
        let value = serde_json::to_value(filter).unwrap();
        assert_eq!(value["filter-lang"], "cql2-json");
        assert!(value.get("filter").is_some());
    }

    #[test]
    fn text() {
        let filter = Filter::Cql2Text(
            "id='LC08_L1TP_060247_20180905_20180912_01_T1_L1TP' AND collection='landsat8_l1tp'"
                .to_string(),
        );
        let value = serde_json::to_value(filter).unwrap();
        assert_eq!(value["filter-lang"], "cql2-text");
        assert!(value.get("filter").is_some());
    }
}