logicaffeine_system/relay_proto.rs
1//! The relay wire protocol — shared verbatim by the native server/client
2//! (`super::relay`) and the browser client (`super::relay_browser`). Both
3//! targets serialize the SAME [`RelayFrame`] with `bincode`, so a browser tab and
4//! a native node speak an identical language across the WebSocket.
5//!
6//! This module is target-agnostic (no `tokio`, no `web-sys`) so it compiles into
7//! both the native binary and the wasm bundle.
8
9use serde::{Deserialize, Serialize};
10
11/// A frame on the relay WebSocket. Client→relay: `Subscribe`/`Unsubscribe`/
12/// `Publish`. Relay→client: `SubAck`/`Event`.
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14pub enum RelayFrame {
15 /// Client → relay: start receiving `Event`s for `topic`.
16 Subscribe { topic: String },
17 /// Client → relay: stop receiving `topic`.
18 Unsubscribe { topic: String },
19 /// Client → relay: deliver `data` to every subscriber of `topic`.
20 Publish { topic: String, data: Vec<u8> },
21 /// Relay → client: acknowledges a `Subscribe` is registered (so a publish
22 /// that races a subscribe cannot be missed — the client awaits this).
23 SubAck { topic: String },
24 /// Relay → client: a published message on a topic the client subscribes to.
25 Event { topic: String, data: Vec<u8> },
26}
27
28impl RelayFrame {
29 /// Serialize to the bytes that cross the WebSocket (a binary message).
30 pub fn to_bytes(&self) -> Vec<u8> {
31 bincode::serialize(self).expect("relay frame is serializable")
32 }
33
34 /// Deserialize a frame from WebSocket bytes; `None` on a malformed frame.
35 pub fn from_bytes(bytes: &[u8]) -> Option<RelayFrame> {
36 bincode::deserialize(bytes).ok()
37 }
38}
39
40#[cfg(test)]
41#[path = "../../logicaffeine_tests/unit/logicaffeine_system/relay_proto/tests.rs"]
42mod tests;