Skip to main content

zaun_community_protocol/command/
sync.rs

1//! Synchronization commands (specification §10.1–§10.3, §10.5).
2
3mod response;
4mod validate;
5
6pub use response::{
7    ChannelEvents, CommunitySnapshot, EventsPage, Resumed, SnapshotItem, SnapshotPage,
8};
9
10use std::fmt;
11
12use serde::{Deserialize, Serialize};
13
14use super::object::{ChannelAfter, ChannelCursor, Stream};
15use crate::ids::ChannelId;
16
17/// `sync.snapshot` (§10.1).
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
19pub struct Snapshot {
20    /// Which part.
21    pub part: SnapshotPart,
22    /// Page start: the last role or channel id of the previous page.
23    #[serde(default, skip_serializing_if = "Option::is_none")]
24    pub after: Option<PageCursor>,
25    /// 1–100.
26    #[serde(default, skip_serializing_if = "Option::is_none")]
27    pub limit: Option<u32>,
28}
29
30/// `part` (§10.1). SPEC: a plain string, as in the §10.1 request.
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
32#[serde(rename_all = "snake_case")]
33pub enum SnapshotPart {
34    /// Profile, descriptor, delegation and the member's own member object.
35    Community,
36    /// Role objects.
37    Roles,
38    /// Visible channel objects with `head_seq`.
39    Channels,
40}
41
42/// A snapshot page cursor: a 16-byte role or channel id (§10.1 `after?: id`).
43#[derive(Clone, Copy, PartialEq, Eq, Hash)]
44pub struct PageCursor(pub [u8; 16]);
45
46impl fmt::Debug for PageCursor {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        write!(f, "PageCursor({})", crate::b64::encode(&self.0))
49    }
50}
51
52impl Serialize for PageCursor {
53    fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
54        s.serialize_str(&crate::b64::encode(&self.0))
55    }
56}
57
58impl<'de> Deserialize<'de> for PageCursor {
59    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
60        let text = <std::borrow::Cow<'de, str>>::deserialize(d)?;
61        crate::b64::decode_array(&text)
62            .map(Self)
63            .map_err(|_| serde::de::Error::custom("invalid id"))
64    }
65}
66
67/// `sync.events` (§10.2).
68#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
69pub struct Events {
70    /// `member` or `channel` (never `live`).
71    pub stream: Stream,
72    /// Required for the channel stream, absent for the member stream.
73    #[serde(default, skip_serializing_if = "Option::is_none")]
74    pub channel_id: Option<ChannelId>,
75    /// The client's cursor.
76    pub after: u64,
77    /// 1–200.
78    pub limit: u32,
79}
80
81/// `sync.resume` (§10.3).
82#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
83pub struct Resume {
84    /// The member-stream cursor.
85    pub member_after: u64,
86    /// ≤ 100 channel cursors, each channel once.
87    pub channels: Vec<ChannelAfter>,
88}
89
90/// `sync.ack` (§10.5): the highest contiguous sequence applied per stream. Not
91/// idempotency-recorded; lower values are ignored by the host.
92#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
93pub struct Ack {
94    /// `member_seq`.
95    pub member: u64,
96    /// ≤ 100 channel cursors, each channel once.
97    pub channels: Vec<ChannelCursor>,
98}