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
|
use std::any::Any;
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::session::ISession;
use crate::*;
// JMAP Core capability {{{
/// JMAP Core URN
pub const URN_JMAP_CORE: &str = "urn:ietf:params:jmap:core";
// Capabilities {{{
pub trait Capability: 'static + Sized {
const URN: &str;
fn from_object(obj: &JObject) -> Self;
fn account_cap(&self, account_id: JMAPId, obj: &JObject) -> Self;
fn account(&self) -> Option<JMAPId>;
fn from_object_dyn(obj: &JObject) -> Box<dyn CapabilityDyn> {
Box::new(Self::from_object(obj))
}
fn is_account_capability(&self) -> bool {
self.account().is_some()
}
}
pub trait CapabilityDyn {
// Get urn of the capability
fn get_urn(&self) -> &'static str;
// Convert to any for downcasting, immutable
fn as_any(&self) -> &dyn Any;
/// Convert to any for downcasting, mutable
fn as_any_mut(&mut self) -> &mut dyn Any;
/// Convert to any for downcasting, consumes object
fn to_any(self) -> Box<dyn Any>;
/// Given a capability and accountCapabilities Object get a capability
fn account_cap_dyn(&self, account_id: JMAPId, obj: &JObject) -> Box<dyn CapabilityDyn>;
/// Return the account if this capabilities has been given info for account
fn account_dyn(&self) -> Option<JMAPId>;
}
impl<T: Capability> CapabilityDyn for T {
fn get_urn(&self) -> &'static str {
T::URN
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn to_any(self) -> Box<dyn Any> {
Box::new(self)
}
fn account_cap_dyn(&self, account_id: JMAPId, obj: &JObject) -> Box<dyn CapabilityDyn> {
Box::new(self.account_cap(account_id, obj))
}
fn account_dyn(&self) -> Option<JMAPId> {
self.account()
}
}
/// A function that parses a JObject into a Capability Trait object.
/// This capability will be able to implement functionality to the JMAPSession object.
pub type CapabilityParser = Box<dyn Fn(&JObject) -> Box<dyn CapabilityDyn>>;
/// Collection of CapabilityParser
pub struct CapabilityRegistry(pub BTreeMap<String, CapabilityParser>);
impl Default for CapabilityRegistry {
fn default() -> Self {
let mut c = Self::new();
c.add_capability::<JMAPCoreCapability>();
c
}
}
impl CapabilityRegistry {
/// Create a collection of parsers
pub fn new() -> Self {
Self(BTreeMap::new())
}
/// Insert a parser
pub fn add_capability<T: Capability>(&mut self) {
self.0
.insert(T::URN.to_string(), Box::new(T::from_object_dyn));
}
/// Use the parsers to create a list of capabilities supported by the server in the session.
pub fn server_capabilities(&self, session: &ISession) -> ServerCapabilities {
let mut out = BTreeMap::new();
for (urn, cap) in session.capabilities.iter() {
if let Some(parser) = self.0.get(urn) {
out.insert(urn.clone(), parser(cap));
};
}
ServerCapabilities(out)
}
}
/// The backing datatype in the CapabilityCollection trait.
pub type CapabilityMap = BTreeMap<String, Box<dyn CapabilityDyn>>;
/// Trait for list of capabilities, or any URN -> Capabilty collection
pub trait CapabilityCollection {
fn inner(&self) -> &CapabilityMap;
fn inner_mut(&mut self) -> &mut CapabilityMap;
fn get_capability<T: Capability>(&self) -> Option<&T> {
self.inner().get(T::URN)?.as_any().downcast_ref::<T>()
}
fn get_capability_mut<T: Capability>(&mut self) -> Option<&mut T> {
self.inner_mut()
.get_mut(T::URN)?
.as_any_mut()
.downcast_mut::<T>()
}
}
/// Collection
pub struct ServerCapabilities(pub CapabilityMap);
impl CapabilityCollection for ServerCapabilities {
fn inner(&self) -> &CapabilityMap {
&self.0
}
fn inner_mut(&mut self) -> &mut CapabilityMap {
&mut self.0
}
}
impl std::fmt::Debug for ServerCapabilities {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("ServerCapabilities")
.field(&self.0.keys().collect::<Vec<&String>>())
.finish()
}
}
impl ServerCapabilities {
/// Evaluate AccountCapabilities using this list of capabilities and the intermediate session
pub fn account_capabilities(
&self,
client: &JMAPId,
session: &ISession,
) -> Option<AccountCapabilities> {
session.accounts.get(client).map(|acc| {
let mut out = BTreeMap::new();
let empty = JObj::new();
for (urn, scap) in self.0.iter() {
let ccap = acc.accountCapabilities.get(urn).unwrap_or(&empty);
out.insert(urn.clone(), scap.account_cap_dyn(client.clone(), ccap));
}
AccountCapabilities(out)
})
}
}
/// List of capabilities, specific to an account.
pub struct AccountCapabilities(pub CapabilityMap);
impl std::fmt::Debug for AccountCapabilities {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("AccountCapabilities")
.field(&self.0.keys().collect::<Vec<&String>>())
.finish()
}
}
impl CapabilityCollection for AccountCapabilities {
fn inner(&self) -> &CapabilityMap {
&self.0
}
fn inner_mut(&mut self) -> &mut CapabilityMap {
&mut self.0
}
}
// }}}
/// JMAP Core capability as outlined in RFC 8620
#[allow(non_snake_case)]
#[derive(Serialize, Deserialize, Clone)]
pub struct JMAPCoreCapability {
pub maxSizeUpload: JUint,
pub maxConcurrentUpload: JUint,
pub maxSizeRequest: JUint,
pub maxConcurrentRequests: JUint,
pub maxCallsInRequest: JUint,
pub maxObjectsInGet: JUint,
pub maxObjectsInSet: JUint,
pub collationAlgorithms: JArr<String>,
#[serde(skip)]
accountId: Option<JMAPId>,
}
impl Capability for JMAPCoreCapability {
const URN: &str = URN_JMAP_CORE;
fn from_object(obj: &JObject) -> Self {
// TODO: Unwrap
let out: Self = serde_json::from_value(JUnknown::Object(obj.clone()).into()).unwrap();
out
}
fn account_cap(&self, account_id: JMAPId, _: &JObject) -> Self {
let mut out = self.clone();
out.accountId = Some(account_id);
out
}
fn account(&self) -> Option<JMAPId> {
self.accountId.clone()
}
}
// }}}
|