jmap-websocket 0.6.0

JMAP over WebSocket (RFC 8887, urn:ietf:params:jmap:websocket): wire frames, capability, and a framework-agnostic server session driver
Documentation
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! The RFC 8887 §4.3 WebSocket messages.
//!
//! Every message is one JSON object whose `@type` names it. Serialize
//! always writes `@type`; deserializing one message type directly rejects
//! any other `@type` value and accepts its absence (the jmap-types
//! `StateChange` convention). [`ClientMessage`] and [`ServerMessage`]
//! pick the type from `@type`, so there it is required.

use serde::de::{self, DeserializeOwned};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{Map, Value};

use jmap_types::problem::ProblemDetails;
use jmap_types::{JmapRequest, JmapResponse, StateChange};

const REQUEST: &str = "Request";
const RESPONSE: &str = "Response";
const REQUEST_ERROR: &str = "RequestError";
const STATE_CHANGE: &str = "StateChange";
const PUSH_ENABLE: &str = "WebSocketPushEnable";
const PUSH_DISABLE: &str = "WebSocketPushDisable";

/// Checks a deserialized `@type`: absent is accepted, any value other
/// than `expected` is an error.
fn check_type<E: de::Error>(found: Option<String>, expected: &'static str) -> Result<(), E> {
    match found {
        None => Ok(()),
        Some(t) if t == expected => Ok(()),
        Some(t) => Err(E::invalid_value(de::Unexpected::Str(&t), &expected)),
    }
}

// ---------------------------------------------------------------------------
// Request
// ---------------------------------------------------------------------------

/// A JMAP Request sent over the WebSocket (RFC 8887 §4.3.2): an RFC 8620
/// §3.3 Request plus `@type: "Request"` and an optional client `id`.
///
/// Unknown members land in the inner [`JmapRequest::extra`].
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct WsRequest {
    /// `id`: "A client-specified identifier for the request to be echoed
    /// back in the response to this request."
    pub id: Option<String>,
    /// The RFC 8620 Request (`using`, `methodCalls`, `createdIds`).
    pub request: JmapRequest,
}

impl WsRequest {
    /// A request frame for `request` with correlation id `id`.
    pub fn new(request: JmapRequest, id: Option<String>) -> Self {
        Self { id, request }
    }
}

#[derive(Serialize)]
struct RequestOut<'a> {
    #[serde(rename = "@type")]
    ty: &'static str,
    #[serde(skip_serializing_if = "Option::is_none")]
    id: Option<&'a str>,
    #[serde(flatten)]
    request: &'a JmapRequest,
}

#[derive(Deserialize)]
struct RequestIn {
    #[serde(rename = "@type", default)]
    ty: Option<String>,
    #[serde(default)]
    id: Option<String>,
    #[serde(flatten)]
    request: JmapRequest,
}

impl Serialize for WsRequest {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        RequestOut {
            ty: REQUEST,
            id: self.id.as_deref(),
            request: &self.request,
        }
        .serialize(s)
    }
}

impl<'de> Deserialize<'de> for WsRequest {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let r = RequestIn::deserialize(d)?;
        check_type(r.ty, REQUEST)?;
        Ok(Self {
            id: r.id,
            request: r.request,
        })
    }
}

// ---------------------------------------------------------------------------
// Response
// ---------------------------------------------------------------------------

/// A JMAP Response sent over the WebSocket (RFC 8887 §4.3.3): an RFC 8620
/// §3.4 Response plus `@type: "Response"` and `requestId`.
///
/// `requestId` "MUST be returned if an identifier is included in the
/// request"; it is omitted on the wire when `None`.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct WsResponse {
    /// `requestId`: the `id` of the [`WsRequest`] this answers.
    pub request_id: Option<String>,
    /// The RFC 8620 Response.
    pub response: JmapResponse,
}

impl WsResponse {
    /// A response frame answering the request with id `request_id`.
    pub fn new(response: JmapResponse, request_id: Option<String>) -> Self {
        Self {
            request_id,
            response,
        }
    }
}

#[derive(Serialize)]
struct ResponseOut<'a> {
    #[serde(rename = "@type")]
    ty: &'static str,
    #[serde(rename = "requestId", skip_serializing_if = "Option::is_none")]
    request_id: Option<&'a str>,
    #[serde(flatten)]
    response: &'a JmapResponse,
}

#[derive(Deserialize)]
struct ResponseIn {
    #[serde(rename = "@type", default)]
    ty: Option<String>,
    #[serde(rename = "requestId", default)]
    request_id: Option<String>,
    #[serde(flatten)]
    response: JmapResponse,
}

impl Serialize for WsResponse {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        ResponseOut {
            ty: RESPONSE,
            request_id: self.request_id.as_deref(),
            response: &self.response,
        }
        .serialize(s)
    }
}

impl<'de> Deserialize<'de> for WsResponse {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let r = ResponseIn::deserialize(d)?;
        check_type(r.ty, RESPONSE)?;
        Ok(Self {
            request_id: r.request_id,
            response: r.response,
        })
    }
}

// ---------------------------------------------------------------------------
// RequestError
// ---------------------------------------------------------------------------

/// A request-level error sent over the WebSocket (RFC 8887 §4.3.4): an
/// RFC 8620 §3.6.1 problem details object plus `@type: "RequestError"`
/// and `requestId`.
///
/// `requestId` is always written, as `null` when `None`, the way the
/// RFC 8887 §4.4 `notJSON` example writes it for a message the server
/// could not read an id from. It "MUST be returned if given in the
/// request".
///
/// ```
/// use jmap_websocket::WsRequestError;
/// use jmap_websocket::jmap_types::problem::ProblemDetails;
///
/// let e = WsRequestError::new(
///     ProblemDetails::new("urn:ietf:params:jmap:error:notJSON", 400),
///     None,
/// );
/// let v = serde_json::to_value(&e).unwrap();
/// assert_eq!(v["@type"], "RequestError");
/// assert!(v["requestId"].is_null());
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct WsRequestError {
    /// `requestId`: the `id` of the request this error answers.
    pub request_id: Option<String>,
    /// The problem details (`type`, `status`, `title`, `detail`, `limit`,
    /// extension members).
    pub problem: ProblemDetails,
}

impl WsRequestError {
    /// An error frame carrying `problem` for the request with id
    /// `request_id`.
    pub fn new(problem: ProblemDetails, request_id: Option<String>) -> Self {
        Self {
            request_id,
            problem,
        }
    }
}

#[derive(Serialize)]
struct RequestErrorOut<'a> {
    #[serde(rename = "@type")]
    ty: &'static str,
    #[serde(rename = "requestId")]
    request_id: Option<&'a str>,
    #[serde(flatten)]
    problem: &'a ProblemDetails,
}

#[derive(Deserialize)]
struct RequestErrorIn {
    #[serde(rename = "@type", default)]
    ty: Option<String>,
    #[serde(rename = "requestId", default)]
    request_id: Option<String>,
    #[serde(flatten)]
    problem: ProblemDetails,
}

impl Serialize for WsRequestError {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        RequestErrorOut {
            ty: REQUEST_ERROR,
            request_id: self.request_id.as_deref(),
            problem: &self.problem,
        }
        .serialize(s)
    }
}

impl<'de> Deserialize<'de> for WsRequestError {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let r = RequestErrorIn::deserialize(d)?;
        check_type(r.ty, REQUEST_ERROR)?;
        Ok(Self {
            request_id: r.request_id,
            problem: r.problem,
        })
    }
}

// ---------------------------------------------------------------------------
// StateChange
// ---------------------------------------------------------------------------

/// A push notification sent over the WebSocket (RFC 8887 §4.3.5.1): an
/// RFC 8620 §7.1 StateChange plus an optional `pushState`.
///
/// `pushState` "encodes the entire server state visible to the user"; a
/// client echoes the last one it got in [`WebSocketPushEnable`] after a
/// reconnect. Omitted on the wire when `None`.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WsStateChange {
    /// `pushState`.
    pub push_state: Option<String>,
    /// The RFC 8620 StateChange (`changed` and extension members).
    pub state_change: StateChange,
}

impl WsStateChange {
    /// A push frame for `state_change` with token `push_state`.
    pub fn new(state_change: StateChange, push_state: Option<String>) -> Self {
        Self {
            push_state,
            state_change,
        }
    }
}

#[derive(Serialize)]
struct StateChangeOut<'a> {
    // StateChange's own Serialize writes `@type` first.
    #[serde(flatten)]
    state_change: &'a StateChange,
    #[serde(rename = "pushState", skip_serializing_if = "Option::is_none")]
    push_state: Option<&'a str>,
}

#[derive(Deserialize)]
struct StateChangeIn {
    #[serde(rename = "pushState", default)]
    push_state: Option<String>,
    // StateChange's own Deserialize checks and drops `@type`.
    #[serde(flatten)]
    state_change: StateChange,
}

impl Serialize for WsStateChange {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        StateChangeOut {
            state_change: &self.state_change,
            push_state: self.push_state.as_deref(),
        }
        .serialize(s)
    }
}

impl<'de> Deserialize<'de> for WsStateChange {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let r = StateChangeIn::deserialize(d)?;
        Ok(Self {
            push_state: r.push_state,
            state_change: r.state_change,
        })
    }
}

// ---------------------------------------------------------------------------
// WebSocketPushEnable / WebSocketPushDisable
// ---------------------------------------------------------------------------

/// Turns on push notifications for the connection (RFC 8887 §4.3.5.2).
///
/// `dataTypes` is `String[]|null` and always written; `null` asks for
/// every supported data type. A missing `dataTypes` reads as `null`.
/// Sending it again replaces the earlier subscription.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct WebSocketPushEnable {
    /// `dataTypes`: the data type names whose changes the client wants;
    /// "Other types are omitted from the TypeState object." `None` is
    /// every type.
    pub data_types: Option<Vec<String>>,
    /// `pushState`: the last token the client got; "the server SHOULD
    /// immediately send all changes since that state token".
    pub push_state: Option<String>,
    /// Vendor, site and future-spec members.
    pub extra: Map<String, Value>,
}

impl WebSocketPushEnable {
    /// A push-enable message for `data_types` (`None`: all types),
    /// resuming from `push_state`.
    pub fn new(data_types: Option<Vec<String>>, push_state: Option<String>) -> Self {
        Self {
            data_types,
            push_state,
            extra: Map::new(),
        }
    }
}

#[derive(Serialize)]
struct PushEnableOut<'a> {
    #[serde(rename = "@type")]
    ty: &'static str,
    #[serde(rename = "dataTypes")]
    data_types: Option<&'a [String]>,
    #[serde(rename = "pushState", skip_serializing_if = "Option::is_none")]
    push_state: Option<&'a str>,
    #[serde(flatten)]
    extra: &'a Map<String, Value>,
}

#[derive(Deserialize)]
struct PushEnableIn {
    #[serde(rename = "@type", default)]
    ty: Option<String>,
    #[serde(rename = "dataTypes", default)]
    data_types: Option<Vec<String>>,
    #[serde(rename = "pushState", default)]
    push_state: Option<String>,
    #[serde(flatten)]
    extra: Map<String, Value>,
}

impl Serialize for WebSocketPushEnable {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        PushEnableOut {
            ty: PUSH_ENABLE,
            data_types: self.data_types.as_deref(),
            push_state: self.push_state.as_deref(),
            extra: &self.extra,
        }
        .serialize(s)
    }
}

impl<'de> Deserialize<'de> for WebSocketPushEnable {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let r = PushEnableIn::deserialize(d)?;
        check_type(r.ty, PUSH_ENABLE)?;
        Ok(Self {
            data_types: r.data_types,
            push_state: r.push_state,
            extra: r.extra,
        })
    }
}

/// Turns off push notifications for the connection (RFC 8887 §4.3.5.3).
/// Its only RFC property is `@type`.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct WebSocketPushDisable {
    /// Vendor, site and future-spec members.
    pub extra: Map<String, Value>,
}

impl WebSocketPushDisable {
    /// A push-disable message with no extension members.
    pub fn new() -> Self {
        Self::default()
    }
}

#[derive(Serialize)]
struct PushDisableOut<'a> {
    #[serde(rename = "@type")]
    ty: &'static str,
    #[serde(flatten)]
    extra: &'a Map<String, Value>,
}

#[derive(Deserialize)]
struct PushDisableIn {
    #[serde(rename = "@type", default)]
    ty: Option<String>,
    #[serde(flatten)]
    extra: Map<String, Value>,
}

impl Serialize for WebSocketPushDisable {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        PushDisableOut {
            ty: PUSH_DISABLE,
            extra: &self.extra,
        }
        .serialize(s)
    }
}

impl<'de> Deserialize<'de> for WebSocketPushDisable {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let r = PushDisableIn::deserialize(d)?;
        check_type(r.ty, PUSH_DISABLE)?;
        Ok(Self { extra: r.extra })
    }
}

// ---------------------------------------------------------------------------
// Message enums
// ---------------------------------------------------------------------------

/// Reads `@type` from a message object and deserializes the whole object
/// as `T`.
fn from_object<T: DeserializeOwned, E: de::Error>(obj: Map<String, Value>) -> Result<T, E> {
    serde_json::from_value(Value::Object(obj)).map_err(E::custom)
}

fn take_tag<E: de::Error>(obj: &Map<String, Value>) -> Result<String, E> {
    match obj.get("@type") {
        Some(Value::String(s)) => Ok(s.clone()),
        Some(_) => Err(E::custom("`@type` is not a string")),
        None => Err(E::missing_field("@type")),
    }
}

/// A message a client sends (RFC 8887 §4.3): "a single JMAP Request
/// object ..., JMAP WebSocketPushEnable object ..., or JMAP
/// WebSocketPushDisable object".
///
/// Deserialize requires `@type` and fails on any other value; a server
/// that must answer bad input with the right problem type uses
/// `server::parse_client_message` (feature `server`) instead.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum ClientMessage {
    /// `@type: "Request"`.
    Request(WsRequest),
    /// `@type: "WebSocketPushEnable"`.
    PushEnable(WebSocketPushEnable),
    /// `@type: "WebSocketPushDisable"`.
    PushDisable(WebSocketPushDisable),
}

impl Serialize for ClientMessage {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Request(m) => m.serialize(s),
            Self::PushEnable(m) => m.serialize(s),
            Self::PushDisable(m) => m.serialize(s),
        }
    }
}

impl<'de> Deserialize<'de> for ClientMessage {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let obj = Map::deserialize(d)?;
        match take_tag::<D::Error>(&obj)?.as_str() {
            REQUEST => from_object(obj).map(Self::Request),
            PUSH_ENABLE => from_object(obj).map(Self::PushEnable),
            PUSH_DISABLE => from_object(obj).map(Self::PushDisable),
            other => Err(de::Error::unknown_variant(
                other,
                &[REQUEST, PUSH_ENABLE, PUSH_DISABLE],
            )),
        }
    }
}

/// A message a server sends (RFC 8887 §4.3): "a single JMAP Response
/// object, JSON Problem Details object, or JMAP StateChange object".
///
/// Deserialize requires `@type` and fails on any other value; RFC 8887
/// §4.3.1 lets a client ignore such a message or close the connection.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub enum ServerMessage {
    /// `@type: "Response"`.
    Response(WsResponse),
    /// `@type: "RequestError"`.
    RequestError(WsRequestError),
    /// `@type: "StateChange"`.
    StateChange(WsStateChange),
}

impl Serialize for ServerMessage {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        match self {
            Self::Response(m) => m.serialize(s),
            Self::RequestError(m) => m.serialize(s),
            Self::StateChange(m) => m.serialize(s),
        }
    }
}

impl<'de> Deserialize<'de> for ServerMessage {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let obj = Map::deserialize(d)?;
        match take_tag::<D::Error>(&obj)?.as_str() {
            RESPONSE => from_object(obj).map(Self::Response),
            REQUEST_ERROR => from_object(obj).map(Self::RequestError),
            STATE_CHANGE => from_object(obj).map(Self::StateChange),
            other => Err(de::Error::unknown_variant(
                other,
                &[RESPONSE, REQUEST_ERROR, STATE_CHANGE],
            )),
        }
    }
}

#[cfg(test)]
mod tests;