use tokio_tungstenite::tungstenite::{
protocol::{frame::coding::CloseCode, CloseFrame as TungsteniteCloseFrame},
Message as TungsteniteMessage,
};
use twilight_model::gateway::CloseFrame;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Message {
Close(Option<CloseFrame<'static>>),
Text(String),
}
impl Message {
pub(crate) fn from_tungstenite(tungstenite: TungsteniteMessage) -> Option<Self> {
match tungstenite {
TungsteniteMessage::Close(frame) => Some(Self::Close(frame.map(|frame| CloseFrame {
code: frame.code.into(),
reason: frame.reason,
}))),
TungsteniteMessage::Text(string) => Some(Self::Text(string)),
TungsteniteMessage::Binary(_)
| TungsteniteMessage::Frame(_)
| TungsteniteMessage::Ping(_)
| TungsteniteMessage::Pong(_) => None,
}
}
pub(crate) fn into_tungstenite(self) -> TungsteniteMessage {
match self {
Self::Close(frame) => {
TungsteniteMessage::Close(frame.map(|frame| TungsteniteCloseFrame {
code: CloseCode::from(frame.code),
reason: frame.reason,
}))
}
Self::Text(string) => TungsteniteMessage::Text(string),
}
}
}
#[cfg(test)]
mod tests {
use super::Message;
use static_assertions::assert_impl_all;
use std::fmt::Debug;
assert_impl_all!(Message: Clone, Debug, Eq, PartialEq);
}