From e38445b6446c8e59095bd2e471e4c64a8ffced5c Mon Sep 17 00:00:00 2001 From: bitfl0wer Date: Thu, 16 Nov 2023 19:05:36 +0100 Subject: [PATCH] Revert "Make GatewayMessage use new Adapter Type" This reverts commit b0b90f094e03cceb7bca1bf3204fc27afb537101. --- src/gateway/message.rs | 47 +++--------------------------------------- 1 file changed, 3 insertions(+), 44 deletions(-) diff --git a/src/gateway/message.rs b/src/gateway/message.rs index 747d53a..34218f8 100644 --- a/src/gateway/message.rs +++ b/src/gateway/message.rs @@ -1,12 +1,9 @@ -use std::fmt::Display; use std::str::Utf8Error; use crate::types; use super::*; -/// An Adapter type for [tokio_tungstenite::tungstenite::Message] and [ws_stream_wasm::WsMessage]. -/// Represents a message received from the gateway. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum GatewayMessageData { Text(String), @@ -46,42 +43,12 @@ impl From for GatewayMessageData { } impl GatewayMessageData { - /// Converts self to a string slice, if possible pub fn to_text(&self) -> Result<&str, Utf8Error> { match *self { GatewayMessageData::Text(ref text) => Ok(text), GatewayMessageData::Binary(ref data) => Ok(std::str::from_utf8(data)?), } } - - /// Returns the length of the message - pub fn len(&self) -> usize { - match *self { - Self::Text(ref string) => string.len(), - Self::Binary(ref data) => data.len(), - } - } - - /// Returns true if the WebSocket message is text. - pub fn is_text(&self) -> bool { - matches!(*self, Self::Binary(_)) - } - - /// Returns true if the WebSocket message has no content. - /// For example, if the other side of the connection sent an empty string. - pub fn is_empty(&self) -> bool { - self.len() == 0 - } -} - -impl Display for GatewayMessageData { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - if let Ok(string) = self.to_text() { - write!(f, "{}", string) - } else { - write!(f, "Binary Data", self.len()) - } - } } /// Represents a messsage received from the gateway. This will be either a [types::GatewayReceivePayload], containing events, or a [GatewayError]. @@ -89,21 +56,13 @@ impl Display for GatewayMessageData { #[derive(Clone, Debug)] pub struct GatewayMessage { /// The message we received from the server - pub(crate) message: GatewayMessageData, + pub(crate) message: tokio_tungstenite::tungstenite::Message, } impl GatewayMessage { /// Creates self from a tungstenite message pub fn from_tungstenite_message(message: tokio_tungstenite::tungstenite::Message) -> Self { - Self { - message: GatewayMessageData::from(message), - } - } - - pub fn from_ws_stream_wasm_message(message: ws_stream_wasm::WsMessage) -> Self { - Self { - message: GatewayMessageData::from(message), - } + Self { message } } /// Parses the message as an error; @@ -149,7 +108,7 @@ impl GatewayMessage { /// Returns whether or not the message is a payload pub fn is_payload(&self) -> bool { // close messages are never payloads, payloads are only text messages - if !self.message.is_text() { + if self.message.is_close() | !self.message.is_text() { return false; }