synd-protocol 0.4.0

Shared wire protocol contracts for syndicationd
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
use std::{fmt, time::Duration};

use serde::{Deserialize, Serialize};

use crate::CapabilitySet;

/// Opaque identifier assigned by the daemon to an opened session.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SessionId(String);

impl SessionId {
    pub fn new(value: impl Into<String>) -> Self {
        Self(value.into())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for SessionId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Request body used by a client to open a daemon session.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OpenSessionRequest {
    required_capabilities: CapabilitySet,
}

impl OpenSessionRequest {
    pub fn new(required_capabilities: CapabilitySet) -> Self {
        Self {
            required_capabilities,
        }
    }

    pub fn required_capabilities(&self) -> &CapabilitySet {
        &self.required_capabilities
    }
}

/// Response body returned after the daemon accepts a session.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OpenSessionResponse {
    session_id: SessionId,
    available_capabilities: CapabilitySet,
    lease: SessionLease,
}

impl OpenSessionResponse {
    pub fn new(session_id: SessionId, available_capabilities: CapabilitySet) -> Self {
        Self::with_lease(session_id, available_capabilities, SessionLease::default())
    }

    pub fn with_lease(
        session_id: SessionId,
        available_capabilities: CapabilitySet,
        lease: SessionLease,
    ) -> Self {
        Self {
            session_id,
            available_capabilities,
            lease,
        }
    }

    pub fn session_id(&self) -> &SessionId {
        &self.session_id
    }

    pub fn available_capabilities(&self) -> &CapabilitySet {
        &self.available_capabilities
    }

    pub fn lease(&self) -> SessionLease {
        self.lease
    }
}

/// Machine-readable reason for rejecting a session open request.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OpenSessionErrorCode {
    MissingCapabilities,
}

/// Error body returned when the daemon rejects a session open request.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct OpenSessionErrorResponse {
    code: OpenSessionErrorCode,
    missing_capabilities: CapabilitySet,
}

impl OpenSessionErrorResponse {
    pub fn from_missing_capabilities(missing_capabilities: CapabilitySet) -> Self {
        Self {
            code: OpenSessionErrorCode::MissingCapabilities,
            missing_capabilities,
        }
    }

    pub fn code(&self) -> OpenSessionErrorCode {
        self.code
    }

    pub fn missing_capabilities(&self) -> &CapabilitySet {
        &self.missing_capabilities
    }
}

impl fmt::Display for OpenSessionErrorResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.code {
            OpenSessionErrorCode::MissingCapabilities => {
                write!(
                    f,
                    "missing required capabilities: {}",
                    self.missing_capabilities
                )
            }
        }
    }
}

/// Request body used by a client to close a daemon session.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CloseSessionRequest {
    session_id: SessionId,
}

impl CloseSessionRequest {
    pub fn new(session_id: SessionId) -> Self {
        Self { session_id }
    }

    pub fn session_id(&self) -> &SessionId {
        &self.session_id
    }
}

/// Lease granted by the daemon for one accepted session.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct SessionLease(Duration);

impl SessionLease {
    pub fn new(duration: Duration) -> Self {
        Self(duration)
    }

    pub fn duration(self) -> Duration {
        self.0
    }
}

impl Default for SessionLease {
    fn default() -> Self {
        Self(Duration::from_secs(30))
    }
}

/// Request body used by a client to renew a daemon session lease.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RenewSessionRequest {
    session_id: SessionId,
}

impl RenewSessionRequest {
    pub fn new(session_id: SessionId) -> Self {
        Self { session_id }
    }

    pub fn session_id(&self) -> &SessionId {
        &self.session_id
    }
}

/// Response body returned after the daemon renews a session lease.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RenewSessionResponse {
    session_id: SessionId,
    lease: SessionLease,
}

impl RenewSessionResponse {
    pub fn new(session_id: SessionId, lease: SessionLease) -> Self {
        Self { session_id, lease }
    }

    pub fn session_id(&self) -> &SessionId {
        &self.session_id
    }

    pub fn lease(&self) -> SessionLease {
        self.lease
    }
}

/// Machine-readable reason for rejecting a session renew request.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RenewSessionErrorCode {
    UnknownSession,
}

/// Error body returned when the daemon rejects a session renew request.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RenewSessionErrorResponse {
    code: RenewSessionErrorCode,
    session_id: SessionId,
}

impl RenewSessionErrorResponse {
    pub fn unknown_session(session_id: SessionId) -> Self {
        Self {
            code: RenewSessionErrorCode::UnknownSession,
            session_id,
        }
    }

    pub fn code(&self) -> RenewSessionErrorCode {
        self.code
    }

    pub fn session_id(&self) -> &SessionId {
        &self.session_id
    }
}

impl fmt::Display for RenewSessionErrorResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.code {
            RenewSessionErrorCode::UnknownSession => {
                write!(f, "unknown session {}", self.session_id)
            }
        }
    }
}

/// Response body returned after the daemon closes a session.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct CloseSessionResponse {}

impl CloseSessionResponse {
    pub fn new() -> Self {
        Self {}
    }
}

/// Machine-readable reason for rejecting a session close request.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CloseSessionErrorCode {
    UnknownSession,
}

/// Error body returned when the daemon rejects a session close request.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CloseSessionErrorResponse {
    code: CloseSessionErrorCode,
    session_id: SessionId,
}

impl CloseSessionErrorResponse {
    pub fn unknown_session(session_id: SessionId) -> Self {
        Self {
            code: CloseSessionErrorCode::UnknownSession,
            session_id,
        }
    }

    pub fn code(&self) -> CloseSessionErrorCode {
        self.code
    }

    pub fn session_id(&self) -> &SessionId {
        &self.session_id
    }
}

impl fmt::Display for CloseSessionErrorResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.code {
            CloseSessionErrorCode::UnknownSession => {
                write!(f, "unknown session {}", self.session_id)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use crate::{
        CapabilitySet,
        session::{
            CloseSessionErrorResponse, CloseSessionRequest, CloseSessionResponse,
            OpenSessionErrorResponse, OpenSessionRequest, OpenSessionResponse,
            RenewSessionErrorResponse, RenewSessionRequest, RenewSessionResponse, SessionId,
            SessionLease,
        },
    };

    #[test]
    fn serializes_session_contracts() {
        let cases = [
            (
                serde_json::to_value(OpenSessionRequest::new(CapabilitySet::new([
                    "timeline.read",
                ])))
                .unwrap(),
                json!({
                    "required_capabilities": {
                        "names": ["timeline.read"]
                    }
                }),
            ),
            (
                serde_json::to_value(OpenSessionResponse::new(
                    SessionId::new("session-1"),
                    CapabilitySet::new(["timeline.read"]),
                ))
                .unwrap(),
                json!({
                    "session_id": "session-1",
                    "available_capabilities": {
                        "names": ["timeline.read"]
                    },
                    "lease": {
                        "secs": 30,
                        "nanos": 0
                    }
                }),
            ),
            (
                serde_json::to_value(RenewSessionRequest::new(SessionId::new("session-1")))
                    .unwrap(),
                json!({
                    "session_id": "session-1"
                }),
            ),
            (
                serde_json::to_value(RenewSessionResponse::new(
                    SessionId::new("session-1"),
                    SessionLease::new(std::time::Duration::from_secs(30)),
                ))
                .unwrap(),
                json!({
                    "session_id": "session-1",
                    "lease": {
                        "secs": 30,
                        "nanos": 0
                    }
                }),
            ),
            (
                serde_json::to_value(CloseSessionRequest::new(SessionId::new("session-1")))
                    .unwrap(),
                json!({
                    "session_id": "session-1"
                }),
            ),
            (
                serde_json::to_value(CloseSessionResponse::new()).unwrap(),
                json!({}),
            ),
            (
                serde_json::to_value(OpenSessionErrorResponse::from_missing_capabilities(
                    CapabilitySet::new(["timeline.read"]),
                ))
                .unwrap(),
                json!({
                    "code": "missing_capabilities",
                    "missing_capabilities": {
                        "names": ["timeline.read"]
                    }
                }),
            ),
            (
                serde_json::to_value(CloseSessionErrorResponse::unknown_session(SessionId::new(
                    "session-1",
                )))
                .unwrap(),
                json!({
                    "code": "unknown_session",
                    "session_id": "session-1"
                }),
            ),
            (
                serde_json::to_value(RenewSessionErrorResponse::unknown_session(SessionId::new(
                    "session-1",
                )))
                .unwrap(),
                json!({
                    "code": "unknown_session",
                    "session_id": "session-1"
                }),
            ),
        ];

        for (actual, expected) in cases {
            assert_eq!(actual, expected);
        }
    }

    #[test]
    fn detects_missing_capabilities() {
        let required = CapabilitySet::new(["timeline.read", "subscription.write"]);
        let available = CapabilitySet::new(["timeline.read"]);

        assert_eq!(
            required.missing_from(&available),
            CapabilitySet::new(["subscription.write"])
        );
    }
}