workflow_websocket/client/
message.rs

1use super::error::Error;
2use std::sync::Arc;
3use workflow_core::channel::*;
4
5// /// Internal control message signaling WebSocket state
6// /// change, WebSocket shutdown and other custom events.
7// #[derive(Debug, Clone, PartialEq, Eq, Hash)]
8// pub enum Ctl {
9//     /// Connection is opened
10//     Open,
11//     /// Connection is closed
12//     Closed,
13// }
14
15/// The enum containing a client-side WebSocket message.
16/// This enum defines the message type.
17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18pub enum Message {
19    /// Text message
20    Text(String),
21    /// Binary message
22    Binary(Vec<u8>),
23    /// Connection has Opened
24    Open,
25    /// Connection has Closed
26    Close,
27}
28
29impl From<Message> for Vec<u8> {
30    fn from(msg: Message) -> Self {
31        match msg {
32            Message::Text(string) => string.into(),
33            Message::Binary(vec) => vec,
34            _ => {
35                panic!("WebSocket - From<Message> for Vec<u8>: unsupported message type: {msg:?}",);
36            }
37        }
38    }
39}
40
41impl From<Vec<u8>> for Message {
42    fn from(vec: Vec<u8>) -> Self {
43        Message::Binary(vec)
44    }
45}
46
47impl From<String> for Message {
48    fn from(s: String) -> Self {
49        Message::Text(s)
50    }
51}
52
53impl From<&str> for Message {
54    fn from(s: &str) -> Self {
55        Message::Text(s.to_string())
56    }
57}
58
59impl AsRef<[u8]> for Message {
60    fn as_ref(&self) -> &[u8] {
61        match self {
62            Message::Text(string) => string.as_ref(),
63            Message::Binary(vec) => vec.as_ref(),
64            _ => {
65                panic!("WebSocket - AsRef<[u8]> for Message: unsupported message type: {self:?}",);
66            }
67        }
68    }
69}
70
71// /// Wrapper for a WebSocket message being dispatched to the server.
72// /// This enum represents a message `Post` or `WithAck` type that
73// /// contains a callback that is invoked on successful message
74// /// handoff to the underlying interface (a browser WebSocket interface
75// /// of tokio WebSocket interface))
76// #[derive(Clone)]
77// pub enum DispatchMessage {
78//     Post(Message),
79//     WithAck(Message, Sender<Result<Arc<()>, Arc<Error>>>),
80//     // DispatcherShutdown,
81// }
82
83pub type Ack = Option<Sender<Result<Arc<()>, Arc<Error>>>>;
84
85// impl DispatchMessage {
86//     pub fn is_ctl(&self) -> bool {
87//         matches!(self, DispatchMessage::DispatcherShutdown)
88//     }
89// }