jacquard-common 0.10.1

Core AT Protocol types and utilities for Jacquard
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
//! Jetstream subscription support
//!
//! Jetstream is a simplified JSON-based alternative to the atproto firehose.
//! Unlike subscribeRepos which uses DAG-CBOR, Jetstream uses JSON encoding.

use crate::types::cid::Cid;
use crate::types::nsid::Nsid;
use crate::types::string::{Datetime, Did, Handle, Rkey};
use crate::xrpc::{MessageEncoding, SubscriptionResp, XrpcSubscription};
use crate::{CowStr, Data, IntoStatic, RawData};
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};

/// Parameters for subscribing to Jetstream
#[cfg_attr(feature = "std", derive(bon::Builder))]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "std", builder(start_fn = new))]
pub struct JetstreamParams<'a> {
    /// Filter by collection NSIDs (max 100)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    #[builder(into)]
    pub wanted_collections: Option<Vec<Nsid<'a>>>,

    /// Filter by DIDs (max 10,000)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    #[builder(into)]
    pub wanted_dids: Option<Vec<Did<'a>>>,

    /// Unix microseconds timestamp to start playback
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cursor: Option<i64>,

    /// Maximum payload size in bytes
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_message_size_bytes: Option<u64>,

    /// Enable zstd compression
    #[serde(skip_serializing_if = "Option::is_none")]
    pub compress: Option<bool>,

    /// Pause stream until first options update
    #[serde(skip_serializing_if = "Option::is_none")]
    pub require_hello: Option<bool>,
}

/// Commit operation type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CommitOperation {
    /// Create a new record
    Create,
    /// Update an existing record
    Update,
    /// Delete a record
    Delete,
}

/// Commit event details (minimal validation)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RawJetstreamCommit<'a> {
    /// Revision string
    #[serde(borrow)]
    pub rev: CowStr<'a>,
    /// Operation type
    pub operation: CommitOperation,
    /// Collection NSID
    #[serde(borrow)]
    pub collection: CowStr<'a>,
    /// Record key
    #[serde(borrow)]
    pub rkey: CowStr<'a>,
    /// Record data (present for create/update)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub record: Option<RawData<'a>>,
    /// Content identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub cid: Option<CowStr<'a>>,
}

/// Commit event details (additional validation)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JetstreamCommit<'a> {
    /// Revision string
    #[serde(borrow)]
    pub rev: CowStr<'a>,
    /// Operation type
    pub operation: CommitOperation,
    /// Collection NSID
    #[serde(borrow)]
    pub collection: Nsid<'a>,
    /// Record key
    #[serde(borrow)]
    pub rkey: Rkey<'a>,
    /// Record data (present for create/update)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub record: Option<Data<'a>>,
    /// Content identifier
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub cid: Option<Cid<'a>>,
}

/// Identity event details
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JetstreamIdentity<'a> {
    /// DID
    #[serde(borrow)]
    pub did: Did<'a>,
    /// Handle
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub handle: Option<Handle<'a>>,
    /// Sequence number
    pub seq: i64,
    /// Timestamp
    pub time: Datetime,
}

/// Account event details
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JetstreamAccount<'a> {
    /// Account active status
    pub active: bool,
    /// DID
    #[serde(borrow)]
    pub did: Did<'a>,
    /// Sequence number
    pub seq: i64,
    /// Timestamp
    pub time: Datetime,
    /// Optional status message
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    pub status: Option<CowStr<'a>>,
}

/// Jetstream event message
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind")]
#[serde(rename_all = "lowercase")]
pub enum JetstreamMessage<'a> {
    /// Commit event
    Commit {
        /// DID
        #[serde(borrow)]
        did: Did<'a>,
        /// Unix microseconds timestamp
        time_us: i64,
        /// Commit details
        #[serde(borrow)]
        commit: JetstreamCommit<'a>,
    },
    /// Identity event
    Identity {
        /// DID
        #[serde(borrow)]
        did: Did<'a>,
        /// Unix microseconds timestamp
        time_us: i64,
        /// Identity details
        #[serde(borrow)]
        identity: JetstreamIdentity<'a>,
    },
    /// Account event
    Account {
        /// DID
        #[serde(borrow)]
        did: Did<'a>,
        /// Unix microseconds timestamp
        time_us: i64,
        /// Account details
        #[serde(borrow)]
        account: JetstreamAccount<'a>,
    },
}

/// Jetstream event message
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind")]
#[serde(rename_all = "lowercase")]
pub enum RawJetstreamMessage<'a> {
    /// Commit event
    Commit {
        /// DID
        #[serde(borrow)]
        did: Did<'a>,
        /// Unix microseconds timestamp
        time_us: i64,
        /// Commit details
        #[serde(borrow)]
        commit: RawJetstreamCommit<'a>,
    },
    /// Identity event
    Identity {
        /// DID
        #[serde(borrow)]
        did: Did<'a>,
        /// Unix microseconds timestamp
        time_us: i64,
        /// Identity details
        #[serde(borrow)]
        identity: JetstreamIdentity<'a>,
    },
    /// Account event
    Account {
        /// DID
        #[serde(borrow)]
        did: Did<'a>,
        /// Unix microseconds timestamp
        time_us: i64,
        /// Account details
        #[serde(borrow)]
        account: JetstreamAccount<'a>,
    },
    /// Unknown messsage type
    #[serde(untagged)]
    Unknown(RawData<'a>),
}

impl IntoStatic for CommitOperation {
    type Output = CommitOperation;

    fn into_static(self) -> Self::Output {
        self
    }
}

impl IntoStatic for JetstreamCommit<'_> {
    type Output = JetstreamCommit<'static>;

    fn into_static(self) -> Self::Output {
        JetstreamCommit {
            rev: self.rev.into_static(),
            operation: self.operation,
            collection: self.collection.into_static(),
            rkey: self.rkey.into_static(),
            record: self.record.map(|r| r.into_static()),
            cid: self.cid.map(|c| c.into_static()),
        }
    }
}

impl IntoStatic for RawJetstreamCommit<'_> {
    type Output = RawJetstreamCommit<'static>;

    fn into_static(self) -> Self::Output {
        RawJetstreamCommit {
            rev: self.rev.into_static(),
            operation: self.operation,
            collection: self.collection.into_static(),
            rkey: self.rkey.into_static(),
            record: self.record.map(|r| r.into_static()),
            cid: self.cid.map(|c| c.into_static()),
        }
    }
}

impl IntoStatic for JetstreamIdentity<'_> {
    type Output = JetstreamIdentity<'static>;

    fn into_static(self) -> Self::Output {
        JetstreamIdentity {
            did: self.did.into_static(),
            handle: self.handle.map(|h| h.into_static()),
            seq: self.seq,
            time: self.time,
        }
    }
}

impl IntoStatic for JetstreamAccount<'_> {
    type Output = JetstreamAccount<'static>;

    fn into_static(self) -> Self::Output {
        JetstreamAccount {
            active: self.active,
            did: self.did.into_static(),
            seq: self.seq,
            time: self.time,
            status: self.status.map(|s| s.into_static()),
        }
    }
}

impl IntoStatic for JetstreamMessage<'_> {
    type Output = JetstreamMessage<'static>;

    fn into_static(self) -> Self::Output {
        match self {
            JetstreamMessage::Commit {
                did,
                time_us,
                commit,
            } => JetstreamMessage::Commit {
                did: did.into_static(),
                time_us,
                commit: commit.into_static(),
            },
            JetstreamMessage::Identity {
                did,
                time_us,
                identity,
            } => JetstreamMessage::Identity {
                did: did.into_static(),
                time_us,
                identity: identity.into_static(),
            },
            JetstreamMessage::Account {
                did,
                time_us,
                account,
            } => JetstreamMessage::Account {
                did: did.into_static(),
                time_us,
                account: account.into_static(),
            },
        }
    }
}

impl IntoStatic for RawJetstreamMessage<'_> {
    type Output = RawJetstreamMessage<'static>;

    fn into_static(self) -> Self::Output {
        match self {
            RawJetstreamMessage::Commit {
                did,
                time_us,
                commit,
            } => RawJetstreamMessage::Commit {
                did: did.into_static(),
                time_us,
                commit: commit.into_static(),
            },
            RawJetstreamMessage::Identity {
                did,
                time_us,
                identity,
            } => RawJetstreamMessage::Identity {
                did: did.into_static(),
                time_us,
                identity: identity.into_static(),
            },
            RawJetstreamMessage::Account {
                did,
                time_us,
                account,
            } => RawJetstreamMessage::Account {
                did: did.into_static(),
                time_us,
                account: account.into_static(),
            },
            RawJetstreamMessage::Unknown(data) => RawJetstreamMessage::Unknown(data.into_static()),
        }
    }
}

/// Stream response type for Jetstream subscriptions
pub struct JetstreamStream;

impl SubscriptionResp for JetstreamStream {
    const NSID: &'static str = "jetstream";
    const ENCODING: MessageEncoding = MessageEncoding::Json;

    /// Typed Jetstream message
    type Message<'de> = JetstreamMessage<'de>;

    /// Generic error type
    type Error<'de> = crate::xrpc::GenericError<'de>;
}

impl<'a> XrpcSubscription for JetstreamParams<'a> {
    const NSID: &'static str = "jetstream";
    const ENCODING: MessageEncoding = MessageEncoding::Json;
    const CUSTOM_PATH: Option<&'static str> = Some("/subscribe");
    type Stream = JetstreamStream;
}

impl IntoStatic for JetstreamParams<'_> {
    type Output = JetstreamParams<'static>;

    fn into_static(self) -> Self::Output {
        JetstreamParams {
            wanted_collections: self
                .wanted_collections
                .map(|v| v.into_iter().map(|s| s.into_static()).collect()),
            wanted_dids: self
                .wanted_dids
                .map(|v| v.into_iter().map(|s| s.into_static()).collect()),
            cursor: self.cursor,
            max_message_size_bytes: self.max_message_size_bytes,
            compress: self.compress,
            require_hello: self.require_hello,
        }
    }
}

/// Parameters for subscribing to Jetstream
#[cfg_attr(feature = "std", derive(bon::Builder))]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "std", builder(start_fn = new))]
pub struct RawJetstreamParams<'a> {
    /// Filter by collection NSIDs (max 100)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    #[builder(into)]
    pub wanted_collections: Option<Vec<crate::CowStr<'a>>>,

    /// Filter by DIDs (max 10,000)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(borrow)]
    #[builder(into)]
    pub wanted_dids: Option<Vec<crate::CowStr<'a>>>,

    /// Unix microseconds timestamp to start playback
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cursor: Option<i64>,

    /// Maximum payload size in bytes
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_message_size_bytes: Option<u64>,

    /// Enable zstd compression
    #[serde(skip_serializing_if = "Option::is_none")]
    pub compress: Option<bool>,

    /// Pause stream until first options update
    #[serde(skip_serializing_if = "Option::is_none")]
    pub require_hello: Option<bool>,
}

/// Stream response type for Jetstream subscriptions
pub struct JetstreamRawStream;

impl SubscriptionResp for JetstreamRawStream {
    const NSID: &'static str = "jetstream";
    const ENCODING: MessageEncoding = MessageEncoding::Json;

    /// Typed Jetstream message
    type Message<'de> = RawJetstreamMessage<'de>;

    /// Generic error type
    type Error<'de> = crate::xrpc::GenericError<'de>;
}

impl<'a> XrpcSubscription for RawJetstreamParams<'a> {
    const NSID: &'static str = "jetstream";
    const ENCODING: MessageEncoding = MessageEncoding::Json;
    const CUSTOM_PATH: Option<&'static str> = Some("/subscribe");
    type Stream = JetstreamRawStream;
}

impl IntoStatic for RawJetstreamParams<'_> {
    type Output = RawJetstreamParams<'static>;

    fn into_static(self) -> Self::Output {
        RawJetstreamParams {
            wanted_collections: self
                .wanted_collections
                .map(|v| v.into_iter().map(|s| s.into_static()).collect()),
            wanted_dids: self
                .wanted_dids
                .map(|v| v.into_iter().map(|s| s.into_static()).collect()),
            cursor: self.cursor,
            max_message_size_bytes: self.max_message_size_bytes,
            compress: self.compress,
            require_hello: self.require_hello,
        }
    }
}