use serde::{Deserialize, Serialize};
use crate::{SyncError, SyncFeedEntry, SyncListParams, SyncPage, PROTOCOL_VERSION, SCHEMA_VERSION};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SyncControlFrame {
pub request_id: Option<u64>,
#[serde(flatten)]
pub body: SyncControlMessage,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(
tag = "type",
content = "payload",
rename_all = "camelCase",
rename_all_fields = "camelCase"
)]
pub enum SyncControlMessage {
Hello {
peer_id: String,
schema_version: u32,
protocol_version: u32,
},
HelloAccepted {
hub_id: String,
schema_version: u32,
protocol_version: u32,
},
CatalogRequest(SyncListParams),
CatalogResponse(SyncPage),
Subscribe {
after_feed_cursor: Option<String>,
},
Subscribed {
subscription_id: String,
feed_cursor: String,
},
FeedBatch {
subscription_id: String,
entries: Vec<SyncFeedEntry>,
next_cursor: String,
},
Unsubscribe {
subscription_id: String,
},
Error {
error: SyncError,
target: SyncErrorTarget,
},
}
impl SyncControlMessage {
pub fn hello(peer_id: String) -> Self {
Self::Hello {
peer_id,
schema_version: SCHEMA_VERSION,
protocol_version: PROTOCOL_VERSION,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(
tag = "kind",
rename_all = "camelCase",
rename_all_fields = "camelCase",
deny_unknown_fields
)]
pub enum SyncErrorTarget {
Control,
Document { room_id: String, batch_id: [u8; 8] },
Connection,
}