Skip to main content

talos_sync_types/
control.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{SyncError, SyncFeedEntry, SyncListParams, SyncPage, PROTOCOL_VERSION, SCHEMA_VERSION};
4
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(rename_all = "camelCase", deny_unknown_fields)]
7pub struct SyncControlFrame {
8    pub request_id: Option<u64>,
9    #[serde(flatten)]
10    pub body: SyncControlMessage,
11}
12
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14#[serde(
15    tag = "type",
16    content = "payload",
17    rename_all = "camelCase",
18    rename_all_fields = "camelCase"
19)]
20pub enum SyncControlMessage {
21    Hello {
22        peer_id: String,
23        schema_version: u32,
24        protocol_version: u32,
25    },
26    HelloAccepted {
27        hub_id: String,
28        schema_version: u32,
29        protocol_version: u32,
30    },
31    CatalogRequest(SyncListParams),
32    CatalogResponse(SyncPage),
33    Subscribe {
34        after_feed_cursor: Option<String>,
35    },
36    Subscribed {
37        subscription_id: String,
38        feed_cursor: String,
39    },
40    FeedBatch {
41        subscription_id: String,
42        entries: Vec<SyncFeedEntry>,
43        next_cursor: String,
44    },
45    Unsubscribe {
46        subscription_id: String,
47    },
48    Error {
49        error: SyncError,
50        target: SyncErrorTarget,
51    },
52}
53
54impl SyncControlMessage {
55    pub fn hello(peer_id: String) -> Self {
56        Self::Hello {
57            peer_id,
58            schema_version: SCHEMA_VERSION,
59            protocol_version: PROTOCOL_VERSION,
60        }
61    }
62}
63
64#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
65#[serde(
66    tag = "kind",
67    rename_all = "camelCase",
68    rename_all_fields = "camelCase",
69    deny_unknown_fields
70)]
71pub enum SyncErrorTarget {
72    Control,
73    Document { room_id: String, batch_id: [u8; 8] },
74    Connection,
75}