use std::fmt;
#[derive(Debug, Clone)]
pub enum WsMessage {
Binary(Vec<u8>),
Text(String),
Ping(Vec<u8>),
Pong(Vec<u8>),
Close,
}
#[derive(Debug)]
pub enum TransportError {
ConnectionFailed(String),
SendFailed(String),
Closed,
Other(String),
}
impl fmt::Display for TransportError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TransportError::ConnectionFailed(msg) => write!(f, "Connection failed: {}", msg),
TransportError::SendFailed(msg) => write!(f, "Send failed: {}", msg),
TransportError::Closed => write!(f, "Connection closed"),
TransportError::Other(msg) => write!(f, "{}", msg),
}
}
}
impl std::error::Error for TransportError {}
#[async_trait::async_trait]
pub trait SyncTransport: Send {
async fn send_binary(&mut self, data: Vec<u8>) -> Result<(), TransportError>;
async fn send_text(&mut self, text: String) -> Result<(), TransportError>;
async fn send_ping(&mut self) -> Result<(), TransportError>;
async fn recv(&mut self) -> Option<Result<WsMessage, TransportError>>;
async fn close(&mut self) -> Result<(), TransportError>;
}