use crate::client::ClientId;
use crate::room::RoomId;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default)]
pub struct Message {
pub roomid: RoomId,
pub clientid: ClientId,
pub msg: String,
}
#[derive(Debug, Clone, Default, Deserialize)]
pub struct WsClientMsg {
#[serde(default)]
pub cmd: String,
#[serde(default)]
pub roomid: String,
#[serde(default)]
pub clientid: String,
#[serde(default)]
pub msg: String,
#[serde(default)]
pub ver: Option<u8>,
#[serde(default)]
pub token: String,
#[serde(default)]
pub epoch: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize)]
pub struct V2Registered {
pub control: &'static str,
pub roomid: String,
pub epoch: String,
pub mode: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_initiator: Option<bool>,
}
#[derive(Debug, Clone, Serialize)]
pub struct V2Promoted {
pub control: &'static str,
pub roomid: String,
pub epoch: String,
pub is_initiator: bool,
}
#[derive(Debug, Clone, Serialize)]
pub struct V2Upgrade {
pub control: &'static str,
pub roomid: String,
pub epoch: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct V2Downgrade {
pub control: &'static str,
pub roomid: String,
pub epoch: String,
pub is_initiator: bool,
}
#[derive(Debug, Clone, Serialize)]
pub struct V2RoomFailed {
pub control: &'static str,
pub roomid: String,
pub reason: String,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct WsServerMsg {
pub msg: String,
pub error: String,
}
impl WsServerMsg {
pub fn msg(msg: impl Into<String>) -> Self {
Self {
msg: msg.into(),
error: String::new(),
}
}
pub fn err(error: impl Into<String>) -> Self {
Self {
msg: String::new(),
error: error.into(),
}
}
}
pub fn server_msg(msg: &str) -> String {
to_wire(&WsServerMsg::msg(msg))
}
pub fn server_err(error: &str) -> String {
to_wire(&WsServerMsg::err(error))
}
pub(crate) fn to_wire<T: Serialize>(value: &T) -> String {
serde_json::to_string(value).unwrap_or_default()
}