use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "command", rename_all = "lowercase")]
pub enum CableFrame {
Subscribe {
identifier: String,
},
Unsubscribe {
identifier: String,
},
Message {
identifier: String,
data: serde_json::Value,
},
}
impl CableFrame {
pub fn parse(json: &str) -> doido_core::Result<Self> {
serde_json::from_str(json)
.map_err(|e| doido_core::anyhow::anyhow!("invalid cable frame: {e}"))
}
pub fn to_json(&self) -> doido_core::Result<String> {
serde_json::to_string(self)
.map_err(|e| doido_core::anyhow::anyhow!("cable frame serialize error: {e}"))
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ServerFrame {
Welcome,
Ping { message: i64 },
ConfirmSubscription { identifier: String },
RejectSubscription { identifier: String },
}
impl ServerFrame {
pub fn parse(json: &str) -> doido_core::Result<Self> {
serde_json::from_str(json)
.map_err(|e| doido_core::anyhow::anyhow!("invalid server frame: {e}"))
}
pub fn to_json(&self) -> doido_core::Result<String> {
serde_json::to_string(self)
.map_err(|e| doido_core::anyhow::anyhow!("server frame serialize error: {e}"))
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ServerMessage {
pub identifier: String,
pub message: serde_json::Value,
}
impl ServerMessage {
pub fn new(identifier: impl Into<String>, message: serde_json::Value) -> Self {
Self {
identifier: identifier.into(),
message,
}
}
pub fn parse(json: &str) -> doido_core::Result<Self> {
serde_json::from_str(json)
.map_err(|e| doido_core::anyhow::anyhow!("invalid server message: {e}"))
}
pub fn to_json(&self) -> doido_core::Result<String> {
serde_json::to_string(self)
.map_err(|e| doido_core::anyhow::anyhow!("server message serialize error: {e}"))
}
}