Skip to main content

os_query_builder_rs/compound_query/
bool.rs

1use crate::misc::query_field::QueryField;
2use serde::Serialize;
3
4#[derive(Default, Debug, Clone, Serialize)]
5pub struct Bool {
6    #[serde(skip_serializing_if = "Option::is_none")]
7    must: Option<Vec<QueryField>>,
8
9    #[serde(skip_serializing_if = "Option::is_none")]
10    must_not: Option<Vec<QueryField>>,
11
12    #[serde(skip_serializing_if = "Option::is_none")]
13    filter: Option<Vec<QueryField>>,
14
15    #[serde(skip_serializing_if = "Option::is_none")]
16    should: Option<Vec<QueryField>>,
17
18    #[serde(skip_serializing_if = "Option::is_none")]
19    minimum_should_match: Option<usize>,
20}
21
22impl Bool {
23    pub fn new() -> Self {
24        Default::default()
25    }
26
27    pub fn must<T, F>(self, must_value: F) -> Self
28    where
29        T: Into<QueryField>,
30        F: IntoIterator<Item = T>,
31    {
32        Self {
33            must: Some(must_value.into_iter().map(|x| x.into()).collect()),
34            ..self
35        }
36    }
37
38    pub fn must_not<T, F>(self, must_value: F) -> Self
39    where
40        T: Into<QueryField>,
41        F: IntoIterator<Item = T>,
42    {
43        Self {
44            must_not: Some(must_value.into_iter().map(|x| x.into()).collect()),
45            ..self
46        }
47    }
48
49    pub fn should<T, F>(self, must_value: F) -> Self
50    where
51        T: Into<QueryField>,
52        F: IntoIterator<Item = T>,
53    {
54        Self {
55            should: Some(must_value.into_iter().map(|x| x.into()).collect()),
56            ..self
57        }
58    }
59
60    pub fn filter<T, F>(self, filter_values: F) -> Self
61    where
62        T: Into<QueryField>,
63        F: IntoIterator<Item = T>,
64    {
65        Self {
66            filter: Some(filter_values.into_iter().map(|x| x.into()).collect()),
67            ..self
68        }
69    }
70
71    pub fn minimum_should_match<T: Into<usize>>(self, minimum_should_match: T) -> Self {
72        Self {
73            minimum_should_match: Some(minimum_should_match.into()),
74            ..self
75        }
76    }
77}