wsprism_gateway/realtime/
types.rs1use axum::extract::ws::Message;
2use bytes::Bytes;
3use serde_json::Value;
4
5use wsprism_core::error::{Result, WsPrismError};
6
7#[derive(Debug, Clone)]
12pub enum QoS {
13 Lossy,
15 Reliable { timeout_ms: u64 },
17}
18
19impl Default for QoS {
20 fn default() -> Self {
21 QoS::Lossy
22 }
23}
24
25#[derive(Debug, Clone)]
27pub enum Payload {
28 TextJson(Value),
30 Utf8Bytes(Bytes),
32 Binary(Bytes),
34}
35
36#[derive(Debug, Clone)]
38pub struct Outgoing {
39 pub qos: QoS,
40 pub payload: Payload,
41}
42
43#[derive(Debug, Clone)]
45pub enum PreparedMsg {
46 Text(String),
47 Binary(Bytes),
48}
49
50impl PreparedMsg {
51 pub fn prepare(out: &Outgoing) -> Result<Self> {
52 match &out.payload {
53 Payload::TextJson(v) => {
54 let s = serde_json::to_string(v)
55 .map_err(|e| WsPrismError::BadRequest(format!("json encode failed: {e}")))?;
56 Ok(PreparedMsg::Text(s))
57 }
58 Payload::Utf8Bytes(b) => {
59 let s = std::str::from_utf8(b)
61 .map_err(|e| WsPrismError::BadRequest(format!("utf8 invalid: {e}")))?
62 .to_owned();
63 Ok(PreparedMsg::Text(s))
64 }
65 Payload::Binary(b) => Ok(PreparedMsg::Binary(b.clone())),
66 }
67 }
68
69 pub fn to_ws_message(&self) -> Message {
72 match self {
73 PreparedMsg::Text(s) => Message::Text(s.clone()),
74 PreparedMsg::Binary(b) => Message::Binary(b.to_vec()),
75 }
76 }
77}