use crate::Error;
use bytes::Bytes;
use tokio_tungstenite::tungstenite as ts;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Utf8Bytes(ts::Utf8Bytes);
impl Utf8Bytes {
#[inline]
pub const fn from_static(str: &'static str) -> Self {
Self(ts::Utf8Bytes::from_static(str))
}
#[inline]
pub fn from_bytes_unchecked(bytes: Bytes) -> Self {
Self(unsafe { ts::Utf8Bytes::from_bytes_unchecked(bytes) })
}
#[inline]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
#[inline(always)]
pub(super) fn into_tungstenite(self) -> ts::Utf8Bytes {
self.0
}
}
impl std::ops::Deref for Utf8Bytes {
type Target = str;
#[inline]
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl std::fmt::Display for Utf8Bytes {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl TryFrom<Bytes> for Utf8Bytes {
type Error = std::str::Utf8Error;
#[inline]
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
Ok(Self(bytes.try_into()?))
}
}
impl TryFrom<Vec<u8>> for Utf8Bytes {
type Error = std::str::Utf8Error;
#[inline]
fn try_from(v: Vec<u8>) -> Result<Self, Self::Error> {
Ok(Self(v.try_into()?))
}
}
impl From<String> for Utf8Bytes {
#[inline]
fn from(s: String) -> Self {
Self(s.into())
}
}
impl From<&str> for Utf8Bytes {
#[inline]
fn from(s: &str) -> Self {
Self(s.into())
}
}
impl From<&String> for Utf8Bytes {
#[inline]
fn from(s: &String) -> Self {
Self(s.into())
}
}
impl From<Utf8Bytes> for Bytes {
#[inline]
fn from(Utf8Bytes(bytes): Utf8Bytes) -> Self {
bytes.into()
}
}
impl<T> PartialEq<T> for Utf8Bytes
where
for<'a> &'a str: PartialEq<T>,
{
#[inline]
fn eq(&self, other: &T) -> bool {
self.as_str() == *other
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct CloseCode(pub u16);
impl CloseCode {
pub const NORMAL: CloseCode = CloseCode(1000);
pub const AWAY: CloseCode = CloseCode(1001);
pub const PROTOCOL: CloseCode = CloseCode(1002);
pub const UNSUPPORTED: CloseCode = CloseCode(1003);
pub const STATUS: CloseCode = CloseCode(1005);
pub const ABNORMAL: CloseCode = CloseCode(1006);
pub const INVALID: CloseCode = CloseCode(1007);
pub const POLICY: CloseCode = CloseCode(1008);
pub const SIZE: CloseCode = CloseCode(1009);
pub const EXTENSION: CloseCode = CloseCode(1010);
pub const ERROR: CloseCode = CloseCode(1011);
pub const RESTART: CloseCode = CloseCode(1012);
pub const AGAIN: CloseCode = CloseCode(1013);
}
impl From<CloseCode> for u16 {
fn from(code: CloseCode) -> u16 {
code.0
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct CloseFrame {
pub code: CloseCode,
pub reason: Utf8Bytes,
}
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Message {
Text(Utf8Bytes),
Binary(Bytes),
Ping(Bytes),
Pong(Bytes),
Close(Option<CloseFrame>),
}
impl Message {
pub(super) fn into_tungstenite(self) -> ts::Message {
match self {
Self::Text(text) => ts::Message::Text(text.into_tungstenite()),
Self::Binary(binary) => ts::Message::Binary(binary),
Self::Ping(ping) => ts::Message::Ping(ping),
Self::Pong(pong) => ts::Message::Pong(pong),
Self::Close(Some(close)) => ts::Message::Close(Some(ts::protocol::CloseFrame {
code: ts::protocol::frame::coding::CloseCode::from(close.code.0),
reason: close.reason.into_tungstenite(),
})),
Self::Close(None) => ts::Message::Close(None),
}
}
pub(super) fn from_tungstenite(message: ts::Message) -> Option<Self> {
match message {
ts::Message::Text(text) => Some(Self::Text(Utf8Bytes(text))),
ts::Message::Binary(binary) => Some(Self::Binary(binary)),
ts::Message::Ping(ping) => Some(Self::Ping(ping)),
ts::Message::Pong(pong) => Some(Self::Pong(pong)),
ts::Message::Close(Some(close)) => Some(Self::Close(Some(CloseFrame {
code: CloseCode(close.code.into()),
reason: Utf8Bytes(close.reason),
}))),
ts::Message::Close(None) => Some(Self::Close(None)),
ts::Message::Frame(_) => None,
}
}
pub fn into_data(self) -> Bytes {
match self {
Self::Text(string) => Bytes::from(string),
Self::Binary(data) | Self::Ping(data) | Self::Pong(data) => data,
Self::Close(None) => Bytes::new(),
Self::Close(Some(frame)) => Bytes::from(frame.reason),
}
}
pub fn into_text(self) -> Result<Utf8Bytes, Error> {
match self {
Self::Text(string) => Ok(string),
Self::Binary(data) | Self::Ping(data) | Self::Pong(data) => {
Utf8Bytes::try_from(data).map_err(Into::into)
}
Self::Close(None) => Ok(Utf8Bytes::default()),
Self::Close(Some(frame)) => Ok(frame.reason),
}
}
pub fn to_text(&self) -> Result<&str, Error> {
match *self {
Self::Text(ref string) => Ok(string.as_str()),
Self::Binary(ref data) | Self::Ping(ref data) | Self::Pong(ref data) => {
std::str::from_utf8(data).map_err(Into::into)
}
Self::Close(None) => Ok(""),
Self::Close(Some(ref frame)) => Ok(&frame.reason),
}
}
}
impl Message {
pub fn text<S>(string: S) -> Message
where
S: Into<Utf8Bytes>,
{
Message::Text(string.into())
}
pub fn binary<B>(bin: B) -> Message
where
B: Into<Bytes>,
{
Message::Binary(bin.into())
}
pub fn ping<B>(bin: B) -> Message
where
B: Into<Bytes>,
{
Message::Ping(bin.into())
}
pub fn pong<B>(bin: B) -> Message
where
B: Into<Bytes>,
{
Message::Pong(bin.into())
}
pub fn close<C>(close: C) -> Message
where
C: Into<Option<CloseFrame>>,
{
Message::Close(close.into())
}
}
impl From<String> for Message {
fn from(string: String) -> Self {
Message::Text(string.into())
}
}
impl<'s> From<&'s str> for Message {
fn from(string: &'s str) -> Self {
Message::Text(string.into())
}
}
impl<'b> From<&'b [u8]> for Message {
fn from(data: &'b [u8]) -> Self {
Message::Binary(Bytes::copy_from_slice(data))
}
}
impl From<Vec<u8>> for Message {
fn from(data: Vec<u8>) -> Self {
Message::Binary(data.into())
}
}
impl From<Message> for Vec<u8> {
fn from(msg: Message) -> Self {
msg.into_data().to_vec()
}
}