jmap_client/core/
query_changes.rs

1/*
2 * Copyright Stalwart Labs LLC See the COPYING
3 * file at the top-level directory of this distribution.
4 *
5 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
8 * option. This file may not be copied, modified, or distributed
9 * except according to those terms.
10 */
11
12use serde::{Deserialize, Serialize};
13
14use super::{
15    query::{Comparator, Filter, QueryObject},
16    RequestParams,
17};
18
19#[derive(Debug, Clone, Serialize)]
20pub struct QueryChangesRequest<O: QueryObject> {
21    #[serde(rename = "accountId")]
22    account_id: String,
23
24    #[serde(rename = "filter")]
25    #[serde(skip_serializing_if = "Option::is_none")]
26    filter: Option<Filter<O::Filter>>,
27
28    #[serde(rename = "sort")]
29    #[serde(skip_serializing_if = "Option::is_none")]
30    sort: Option<Vec<Comparator<O::Sort>>>,
31
32    #[serde(rename = "sinceQueryState")]
33    since_query_state: String,
34
35    #[serde(rename = "maxChanges")]
36    #[serde(skip_serializing_if = "Option::is_none")]
37    max_changes: Option<usize>,
38
39    #[serde(rename = "upToId")]
40    #[serde(skip_serializing_if = "Option::is_none")]
41    up_to_id: Option<String>,
42
43    #[serde(rename = "calculateTotal")]
44    calculate_total: bool,
45
46    #[serde(flatten)]
47    arguments: O::QueryArguments,
48}
49
50#[derive(Debug, Clone, Deserialize)]
51pub struct QueryChangesResponse {
52    #[serde(rename = "accountId")]
53    account_id: String,
54    #[serde(rename = "oldQueryState")]
55    old_query_state: String,
56    #[serde(rename = "newQueryState")]
57    new_query_state: String,
58    #[serde(rename = "total")]
59    total: Option<usize>,
60    #[serde(rename = "removed")]
61    removed: Vec<String>,
62    #[serde(rename = "added")]
63    added: Vec<AddedItem>,
64}
65
66#[derive(Debug, Clone, Deserialize)]
67pub struct AddedItem {
68    id: String,
69    index: usize,
70}
71
72impl<O: QueryObject> QueryChangesRequest<O> {
73    pub fn new(params: RequestParams, since_query_state: String) -> Self {
74        QueryChangesRequest {
75            account_id: params.account_id,
76            filter: None,
77            sort: None,
78            since_query_state,
79            max_changes: None,
80            up_to_id: None,
81            calculate_total: false,
82            arguments: O::QueryArguments::default(),
83        }
84    }
85
86    pub fn account_id(&mut self, account_id: impl Into<String>) -> &mut Self {
87        self.account_id = account_id.into();
88        self
89    }
90
91    pub fn filter(&mut self, filter: impl Into<Filter<O::Filter>>) -> &mut Self {
92        self.filter = Some(filter.into());
93        self
94    }
95
96    pub fn sort(&mut self, sort: impl IntoIterator<Item = Comparator<O::Sort>>) -> &mut Self {
97        self.sort = Some(sort.into_iter().collect());
98        self
99    }
100
101    pub fn max_changes(&mut self, max_changes: usize) -> &mut Self {
102        self.max_changes = Some(max_changes);
103        self
104    }
105
106    pub fn up_to_id(&mut self, up_to_id: impl Into<String>) -> &mut Self {
107        self.up_to_id = Some(up_to_id.into());
108        self
109    }
110
111    pub fn calculate_total(&mut self, calculate_total: bool) -> &mut Self {
112        self.calculate_total = calculate_total;
113        self
114    }
115
116    pub fn arguments(&mut self) -> &mut O::QueryArguments {
117        &mut self.arguments
118    }
119}
120
121impl QueryChangesResponse {
122    pub fn account_id(&self) -> &str {
123        &self.account_id
124    }
125
126    pub fn old_query_state(&self) -> &str {
127        &self.old_query_state
128    }
129
130    pub fn new_query_state(&self) -> &str {
131        &self.new_query_state
132    }
133
134    pub fn total(&self) -> Option<usize> {
135        self.total
136    }
137
138    pub fn removed(&self) -> &[String] {
139        &self.removed
140    }
141
142    pub fn added(&self) -> &[AddedItem] {
143        &self.added
144    }
145}
146
147impl AddedItem {
148    pub fn id(&self) -> &str {
149        &self.id
150    }
151
152    pub fn index(&self) -> usize {
153        self.index
154    }
155}