use super::error::Error;
use std::sync::Arc;
use workflow_core::channel::*;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Message {
Text(String),
Binary(Vec<u8>),
Open,
Close,
}
impl From<Message> for Vec<u8> {
fn from(msg: Message) -> Self {
match msg {
Message::Text(string) => string.into(),
Message::Binary(vec) => vec,
_ => {
panic!("WebSocket - From<Message> for Vec<u8>: unsupported message type: {msg:?}",);
}
}
}
}
impl From<Vec<u8>> for Message {
fn from(vec: Vec<u8>) -> Self {
Message::Binary(vec)
}
}
impl From<String> for Message {
fn from(s: String) -> Self {
Message::Text(s)
}
}
impl From<&str> for Message {
fn from(s: &str) -> Self {
Message::Text(s.to_string())
}
}
impl AsRef<[u8]> for Message {
fn as_ref(&self) -> &[u8] {
match self {
Message::Text(string) => string.as_ref(),
Message::Binary(vec) => vec.as_ref(),
_ => {
panic!("WebSocket - AsRef<[u8]> for Message: unsupported message type: {self:?}",);
}
}
}
}
pub type Ack = Option<Sender<Result<Arc<()>, Arc<Error>>>>;