Skip to main content

zaun_community_protocol/command/object/
sync.rs

1//! Streams and cursors (specification §9.1, §10).
2
3use serde::{Deserialize, Serialize};
4
5use crate::ids::ChannelId;
6
7/// An event stream (§9.1). SPEC: a plain string, as in the §7.4 `stream` field and the
8/// §10.3 example (`"stream": "channel"`), not `{t}`.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
10#[serde(rename_all = "snake_case")]
11pub enum Stream {
12    /// Per channel, `channel_seq`.
13    Channel,
14    /// Per member, `member_seq`.
15    Member,
16    /// Not sequenced, not recoverable.
17    Live,
18}
19
20impl Stream {
21    /// The wire string.
22    #[must_use]
23    pub const fn as_str(self) -> &'static str {
24        match self {
25            Self::Channel => "channel",
26            Self::Member => "member",
27            Self::Live => "live",
28        }
29    }
30}
31
32/// The cursors a binding command returns (`community.create`, `invite.redeem`,
33/// `membership.request`).
34///
35/// SPEC: `cursors` is never defined. §10 says a client holds one `member_seq` and one
36/// `channel_seq` per visible channel; the `sync.ack` shape `{member, channels: [{channel_id,
37/// seq}]}` is reused.
38#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
39pub struct Cursors {
40    /// `member_seq`.
41    pub member: u64,
42    /// One `channel_seq` per channel.
43    #[serde(default)]
44    pub channels: Vec<ChannelCursor>,
45}
46
47/// `{channel_id, seq}` (`sync.ack`, §10.5).
48#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
49pub struct ChannelCursor {
50    /// The channel.
51    pub channel_id: ChannelId,
52    /// A `channel_seq`.
53    pub seq: u64,
54}
55
56/// `{channel_id, after}` (`sync.resume`, §10.3).
57#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
58pub struct ChannelAfter {
59    /// The channel.
60    pub channel_id: ChannelId,
61    /// The client's cursor.
62    pub after: u64,
63}
64
65/// `{channel_id, head_seq, floor}` (`sync.resume.new`, §10.3).
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
67pub struct ChannelHead {
68    /// A channel the client did not list but can now view.
69    pub channel_id: ChannelId,
70    /// Its latest `channel_seq`.
71    pub head_seq: u64,
72    /// The member's floor in it.
73    pub floor: u64,
74}
75
76/// `{channel_id, floor}` (`sync.resume.floors`, §10.3, §10.6).
77#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
78pub struct ChannelFloor {
79    /// The channel.
80    pub channel_id: ChannelId,
81    /// Events at or below it are not readable.
82    pub floor: u64,
83}
84
85/// `{stream, channel_id?}` of `sync.resume.resync` and `RESYNC_REQUIRED` (§10.2, §10.3).
86#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
87pub struct Resync {
88    /// The stream to rebuild.
89    pub stream: Stream,
90    /// For a channel stream.
91    #[serde(default, skip_serializing_if = "Option::is_none")]
92    pub channel_id: Option<ChannelId>,
93}