jmap_client/core/
changes.rs

1/*
2 * Copyright Stalwart Labs Ltd. 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 crate::Method;
15
16use super::{request::ResultReference, Object, RequestParams};
17
18pub trait ChangesObject: Object {
19    type ChangesResponse;
20}
21
22#[derive(Debug, Clone, Serialize)]
23pub struct ChangesRequest {
24    #[serde(skip)]
25    method: (Method, usize),
26
27    #[serde(rename = "accountId")]
28    account_id: String,
29
30    #[serde(rename = "sinceState")]
31    since_state: String,
32
33    #[serde(rename = "maxChanges")]
34    #[serde(skip_serializing_if = "Option::is_none")]
35    max_changes: Option<usize>,
36}
37
38#[derive(Debug, Clone, Deserialize)]
39pub struct ChangesResponse<O: ChangesObject> {
40    #[serde(rename = "accountId")]
41    account_id: String,
42
43    #[serde(rename = "oldState")]
44    old_state: String,
45
46    #[serde(rename = "newState")]
47    new_state: String,
48
49    #[serde(rename = "hasMoreChanges")]
50    has_more_changes: bool,
51
52    created: Vec<String>,
53
54    updated: Vec<String>,
55
56    destroyed: Vec<String>,
57
58    #[serde(flatten)]
59    arguments: O::ChangesResponse,
60}
61
62impl ChangesRequest {
63    pub fn new(params: RequestParams, since_state: String) -> Self {
64        ChangesRequest {
65            method: (params.method, params.call_id),
66            account_id: params.account_id,
67            since_state,
68            max_changes: None,
69        }
70    }
71
72    pub fn account_id(&mut self, account_id: impl Into<String>) -> &mut Self {
73        self.account_id = account_id.into();
74        self
75    }
76
77    pub fn max_changes(&mut self, max_changes: usize) -> &mut Self {
78        self.max_changes = Some(max_changes);
79        self
80    }
81
82    pub fn created_reference(&self) -> ResultReference {
83        ResultReference::new(self.method.0, self.method.1, "/created")
84    }
85
86    pub fn updated_reference(&self) -> ResultReference {
87        ResultReference::new(self.method.0, self.method.1, "/updated")
88    }
89
90    pub fn updated_properties_reference(&self) -> ResultReference {
91        ResultReference::new(self.method.0, self.method.1, "/updatedProperties")
92    }
93}
94
95impl<O: ChangesObject> ChangesResponse<O> {
96    pub fn account_id(&self) -> &str {
97        &self.account_id
98    }
99
100    pub fn take_account_id(&mut self) -> String {
101        std::mem::take(&mut self.account_id)
102    }
103
104    pub fn old_state(&self) -> &str {
105        &self.old_state
106    }
107
108    pub fn new_state(&self) -> &str {
109        &self.new_state
110    }
111
112    pub fn take_new_state(&mut self) -> String {
113        std::mem::take(&mut self.new_state)
114    }
115
116    pub fn has_more_changes(&self) -> bool {
117        self.has_more_changes
118    }
119
120    pub fn created(&self) -> &[String] {
121        &self.created
122    }
123
124    pub fn take_created(&mut self) -> Vec<String> {
125        std::mem::take(&mut self.created)
126    }
127
128    pub fn updated(&self) -> &[String] {
129        &self.updated
130    }
131
132    pub fn take_updated(&mut self) -> Vec<String> {
133        std::mem::take(&mut self.updated)
134    }
135
136    pub fn destroyed(&self) -> &[String] {
137        &self.destroyed
138    }
139
140    pub fn take_destroyed(&mut self) -> Vec<String> {
141        std::mem::take(&mut self.destroyed)
142    }
143
144    pub fn arguments(&self) -> &O::ChangesResponse {
145        &self.arguments
146    }
147
148    pub fn total_changes(&self) -> usize {
149        self.created.len() + self.updated.len() + self.destroyed.len()
150    }
151}