Skip to main content

doido_cable/
protocol.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
4#[serde(tag = "type", rename_all = "lowercase")]
5pub enum CableFrame {
6    Subscribe {
7        identifier: String,
8    },
9    Unsubscribe {
10        identifier: String,
11    },
12    Message {
13        identifier: String,
14        data: serde_json::Value,
15    },
16}
17
18impl CableFrame {
19    pub fn parse(json: &str) -> doido_core::Result<Self> {
20        serde_json::from_str(json)
21            .map_err(|e| doido_core::anyhow::anyhow!("invalid cable frame: {e}"))
22    }
23
24    pub fn to_json(&self) -> doido_core::Result<String> {
25        serde_json::to_string(self)
26            .map_err(|e| doido_core::anyhow::anyhow!("cable frame serialize error: {e}"))
27    }
28}