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
use crate::query::QueryTrait;
use serde_json::{json, Value};

#[derive(Default)]
pub struct ExistsQuery {
    field: String,
}

impl ExistsQuery {
    pub fn new(field: &str) -> ExistsQuery {
        let mut value = ExistsQuery::default();
        value.field = field.to_string();
        return value;
    }
}

impl QueryTrait for ExistsQuery {
    fn build(&self) -> Value {
        let name = self.query_name();
        let field = self.field.to_string();
        json!({
            name: {
                "field":field
            }
        })
    }

    fn query_name(&self) -> String {
        return "exists".to_string();
    }
}

#[cfg(test)]
mod tests {
    use crate::query::exists_query::ExistsQuery;
    use crate::query::QueryTrait;

    #[test]
    fn build() {
        assert_eq!(
            ExistsQuery::new("id").build().to_string(),
            "{\"exists\":{\"field\":\"id\"}}"
        );
    }
}