use crate::types::VoiceCloseCode;
use crate::voice::gateway::{VoiceGatewayCommunication, VoiceGatewayMessage};
impl From<VoiceGatewayMessage> for tokio_tungstenite::tungstenite::Message {
fn from(message: VoiceGatewayMessage) -> Self {
Self::Text(message.0.into())
}
}
impl From<tokio_tungstenite::tungstenite::Message> for VoiceGatewayMessage {
fn from(value: tokio_tungstenite::tungstenite::Message) -> Self {
Self(value.to_string())
}
}
impl From<tokio_tungstenite::tungstenite::Message> for VoiceGatewayCommunication {
fn from(value: tokio_tungstenite::tungstenite::Message) -> Self {
match value {
tokio_tungstenite::tungstenite::Message::Text(text) => {
VoiceGatewayCommunication::Message(VoiceGatewayMessage(text.to_string()))
}
tokio_tungstenite::tungstenite::Message::Close(close_frame) => {
if close_frame.is_none() {
return VoiceGatewayCommunication::Error(VoiceCloseCode::FailedToDecodePayload);
}
let close_code = u16::from(close_frame.unwrap().code);
VoiceGatewayCommunication::Error(
VoiceCloseCode::try_from(close_code)
.unwrap_or(VoiceCloseCode::FailedToDecodePayload),
)
}
_ => VoiceGatewayCommunication::Error(VoiceCloseCode::FailedToDecodePayload),
}
}
}