use bytes::Bytes;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Message {
Text(String),
Binary(Bytes),
}
impl Message {
pub fn len(&self) -> usize {
match self {
Self::Text(text) => text.len(),
Self::Binary(bytes) => bytes.len(),
}
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl From<String> for Message {
fn from(text: String) -> Self {
Self::Text(text)
}
}
impl From<&str> for Message {
fn from(text: &str) -> Self {
Self::Text(text.to_owned())
}
}
impl From<Bytes> for Message {
fn from(bytes: Bytes) -> Self {
Self::Binary(bytes)
}
}
impl From<Vec<u8>> for Message {
fn from(bytes: Vec<u8>) -> Self {
Self::Binary(bytes.into())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CloseFrame {
pub code: u16,
pub reason: String,
}
impl CloseFrame {
pub const NORMAL: u16 = 1000;
pub const GOING_AWAY: u16 = 1001;
pub const ABNORMAL: u16 = 1006;
pub const MESSAGE_TOO_BIG: u16 = 1009;
pub const INTERNAL_ERROR: u16 = 1011;
pub fn new(code: u16, reason: impl Into<String>) -> Self {
Self {
code,
reason: reason.into(),
}
}
pub(crate) fn abnormal() -> Self {
Self::new(Self::ABNORMAL, "")
}
}