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
use super::document::DocumentId;
use crate::{document::TypedCouchDocument, types::view::ViewCollection};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct QueriesParams {
    queries: Vec<QueryParams<DocumentId>>,
}

impl QueriesParams {
    #[must_use]
    pub fn new(params: Vec<QueryParams<DocumentId>>) -> Self {
        QueriesParams { queries: params }
    }
}

#[derive(Default, Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
#[serde(bound(deserialize = "T: TypedCouchDocument"))]
pub struct QueriesCollection<K: DeserializeOwned, V: DeserializeOwned, T: TypedCouchDocument> {
    pub results: Vec<ViewCollection<K, V, T>>,
}

/// Whether or not the view in question should be updated prior to responding to the user
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)]
pub enum UpdateView {
    #[serde(rename = "true")]
    True,
    #[serde(rename = "false")]
    False,
    #[serde(rename = "lazy")]
    Lazy,
}

/// Query parameters. You can use the builder paradigm to construct these parameters easily:
/// [views.html](https://docs.couchdb.org/en/stable/api/ddoc/views.html)
/// ```
/// use couch_rs::types::query::QueryParams;
/// let _qp = QueryParams::default().group(true).conflicts(false).start_key("1".to_string());
/// ```
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
pub struct QueryParams<K: Serialize + PartialEq + std::fmt::Debug + Clone> {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub conflicts: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub descending: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub end_key: Option<K>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub end_key_doc_id: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub group: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub group_level: Option<u32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub include_docs: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub attachments: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub att_encoding_info: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub inclusive_end: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub key: Option<K>,

    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub keys: Vec<K>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub reduce: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub skip: Option<u64>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub sorted: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub stable: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub stale: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_key: Option<K>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub start_key_doc_id: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub update: Option<UpdateView>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub update_seq: Option<bool>,
}

impl<K: Serialize + DeserializeOwned + PartialEq + std::fmt::Debug + Clone> Default for QueryParams<K> {
    fn default() -> Self {
        Self {
            conflicts: None,
            descending: None,
            end_key: None,
            end_key_doc_id: None,
            group: None,
            group_level: None,
            include_docs: None,
            attachments: None,
            att_encoding_info: None,
            inclusive_end: None,
            key: None,
            keys: Vec::new(),
            limit: None,
            reduce: None,
            skip: None,
            sorted: None,
            stable: None,
            stale: None,
            start_key: None,
            start_key_doc_id: None,
            update: None,
            update_seq: None,
        }
    }
}

impl<K: Serialize + DeserializeOwned + PartialEq + std::fmt::Debug + Clone> QueryParams<K> {
    #[must_use]
    pub fn from_keys(keys: Vec<K>) -> Self {
        QueryParams {
            keys,
            ..Default::default()
        }
    }

    #[must_use]
    pub fn conflicts(mut self, conflicts: bool) -> Self {
        self.conflicts = Some(conflicts);
        self
    }

    #[must_use]
    pub fn descending(mut self, descending: bool) -> Self {
        self.descending = Some(descending);
        self
    }

    #[must_use]
    pub fn end_key(mut self, end_key: K) -> Self {
        self.end_key = Some(end_key);
        self
    }

    #[must_use]
    pub fn group(mut self, group: bool) -> Self {
        self.group = Some(group);
        self
    }

    #[must_use]
    pub fn group_level(mut self, group_level: u32) -> Self {
        self.group_level = Some(group_level);
        self
    }

    #[must_use]
    pub fn include_docs(mut self, include_docs: bool) -> Self {
        self.include_docs = Some(include_docs);
        self
    }

    #[must_use]
    pub fn attachments(mut self, attachments: bool) -> Self {
        self.attachments = Some(attachments);
        self
    }

    #[must_use]
    pub fn att_encoding_info(mut self, att_encoding_info: bool) -> Self {
        self.att_encoding_info = Some(att_encoding_info);
        self
    }

    #[must_use]
    pub fn inclusive_end(mut self, inclusive_end: bool) -> Self {
        self.inclusive_end = Some(inclusive_end);
        self
    }

    #[must_use]
    pub fn key(mut self, key: K) -> Self {
        self.key = Some(key);
        self
    }

    #[must_use]
    pub fn keys(mut self, keys: Vec<K>) -> Self {
        self.keys = keys;
        self
    }

    #[must_use]
    pub fn limit(mut self, limit: u64) -> Self {
        self.limit = Some(limit);
        self
    }

    #[must_use]
    pub fn reduce(mut self, reduce: bool) -> Self {
        self.reduce = Some(reduce);
        self
    }

    #[must_use]
    pub fn skip(mut self, skip: u64) -> Self {
        self.skip = Some(skip);
        self
    }

    #[must_use]
    pub fn sorted(mut self, sorted: bool) -> Self {
        self.sorted = Some(sorted);
        self
    }

    #[must_use]
    pub fn stable(mut self, stable: bool) -> Self {
        self.stable = Some(stable);
        self
    }

    #[must_use]
    pub fn start_key(mut self, start_key: K) -> Self {
        self.start_key = Some(start_key);
        self
    }

    #[must_use]
    pub fn start_key_doc_id(mut self, start_key_doc_id: &str) -> Self {
        self.start_key_doc_id = Some(start_key_doc_id.to_string());
        self
    }

    #[must_use]
    pub fn update(mut self, update: UpdateView) -> Self {
        self.update = Some(update);
        self
    }

    #[must_use]
    pub fn update_seq(mut self, update_seq: bool) -> Self {
        self.update_seq = Some(update_seq);
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_query_params_builder_paradigm() {
        let qp = QueryParams::default()
            .group(true)
            .conflicts(false)
            .start_key("1".to_string())
            .update(UpdateView::Lazy);
        assert_eq!(qp.group, Some(true));
        assert_eq!(qp.start_key, Some("1".to_string()));
        let str_val = serde_json::to_string(&qp).expect("can not convert to string");
        assert!(str_val.contains(r#""update":"lazy""#));
    }
}