Repository: 1c3t3a/rust-socketio (language: rust)

Issue:
Implement Error for Error



Discussion / hints:

Fix patch:
diff --git a/src/engineio/packet.rs b/src/engineio/packet.rs
index 55eefd69..779d74da 100644
--- a/src/engineio/packet.rs
+++ b/src/engineio/packet.rs
@@ -3,8 +3,7 @@ use base64::{decode, encode};
 use std::char;
 use std::str;
 
-use crate::error::Error;
-
+use crate::error::{Error, Result};
 /// Enumeration of the `engine.io` `Packet` types.
 #[derive(Copy, Clone, Eq, PartialEq, Debug)]
 pub enum PacketId {
@@ -29,7 +28,7 @@ const SEPERATOR: char = '\x1e';
 
 /// Converts a byte into the corresponding `PacketId`.
 #[inline]
-const fn u8_to_packet_id(b: u8) -> Result<PacketId, Error> {
+const fn u8_to_packet_id(b: u8) -> Result<PacketId> {
     match b as char {
         '0' => Ok(PacketId::Open),
         '1' => Ok(PacketId::Close),
@@ -52,7 +51,7 @@ impl Packet {
     // inefficient
 
     /// Decodes a single `Packet` from an `u8` byte stream.
-    fn decode_packet(bytes: Vec<u8>) -> Result<Self, Error> {
+    fn decode_packet(bytes: Vec<u8>) -> Result<Self> {
         if bytes.is_empty() {
             return Err(Error::EmptyPacket);
         }
@@ -110,7 +109,7 @@ impl Packet {
 
 /// Decodes a `payload` which in the `engine.io` context means a chain of normal
 /// packets separated by a certain SEPERATOR, in this case the delimiter `\x30`.
-pub fn decode_payload(payload: Vec<u8>) -> Result<Vec<Packet>, Error> {
+pub fn decode_payload(payload: Vec<u8>) -> Result<Vec<Packet>> {
     let mut vec = Vec::new();
     for packet_bytes in payload.split(|byte| (*byte as char) == SEPERATOR) {
         // TODO this conversion might be inefficent, as the 'to_vec' method
diff --git a/src/engineio/socket.rs b/src/engineio/socket.rs
index c793ec69..ffaefa3b 100644
--- a/src/engineio/socket.rs
+++ b/src/engineio/socket.rs
@@ -3,7 +3,7 @@ use std::thread;
 
 use super::packet::Packet;
 use crate::engineio::transport::TransportClient;
-use crate::error::Error;
+use crate::error::{Error, Result};
 use std::sync::{
     atomic::{AtomicBool, Ordering},
     Arc, RwLock,
@@ -28,7 +28,7 @@ impl EngineSocket {
 
     /// Binds the socket to a certain `address`. Attention! This doesn't allow
     /// to configure callbacks afterwards.
-    pub fn bind<T: Into<String>>(&self, address: T) -> Result<(), Error> {
+    pub fn bind<T: Into<String>>(&self, address: T) -> Result<()> {
         self.transport_client.write()?.open(address.into())?;
 
         let cl = Arc::clone(&self.transport_client);
@@ -52,7 +52,7 @@ impl EngineSocket {
     }
 
     /// Sends a packet to the server.
-    pub fn emit(&mut self, packet: Packet) -> Result<(), Error> {
+    pub fn emit(&mut self, packet: Packet) -> Result<()> {
         if !self.serving.load(Ordering::Relaxed) {
             return Err(Error::ActionBeforeOpen);
         }
@@ -60,7 +60,7 @@ impl EngineSocket {
     }
 
     /// Registers the `on_open` callback.
-    pub fn on_open<F>(&mut self, function: F) -> Result<(), Error>
+    pub fn on_open<F>(&mut self, function: F) -> Result<()>
     where
         F: Fn(()) + 'static + Sync + Send,
     {
@@ -72,7 +72,7 @@ impl EngineSocket {
     }
 
     /// Registers the `on_close` callback.
-    pub fn on_close<F>(&mut self, function: F) -> Result<(), Error>
+    pub fn on_close<F>(&mut self, function: F) -> Result<()>
     where
         F: Fn(()) + 'static + Sync + Send,
     {
@@ -84,7 +84,7 @@ impl EngineSocket {
     }
 
     /// Registers the `on_packet` callback.
-    pub fn on_packet<F>(&mut self, function: F) -> Result<(), Error>
+    pub fn on_packet<F>(&mut self, function: F) -> Result<()>
     where
         F: Fn(Packet) + 'static + Sync + Send,
     {
@@ -96,7 +96,7 @@ impl EngineSocket {
     }
 
     /// Registers the `on_data` callback.
-    pub fn on_data<F>(&mut self, function: F) -> Result<(), Error>
+    pub fn on_data<F>(&mut self, function: F) -> Result<()>
     where
         F: Fn(Vec<u8>) + 'static + Sync + Send,
     {
@@ -108,7 +108,7 @@ impl EngineSocket {
     }
 
     /// Registers the `on_error` callback.
-    pub fn on_error<F>(&mut self, function: F) -> Result<(), Error>
+    pub fn on_error<F>(&mut self, function: F) -> Result<()>
     where
         F: Fn(String) + 'static + Sync + Send + Send,
     {
diff --git a/src/engineio/transport.rs b/src/engineio/transport.rs
index 3ee6dc61..bcf9eaa0 100644
--- a/src/engineio/transport.rs
+++ b/src/engineio/transport.rs
@@ -1,5 +1,5 @@
 use crate::engineio::packet::{decode_payload, encode_payload, Packet, PacketId};
-use crate::error::Error;
+use crate::error::{Error, Result};
 use adler32::adler32;
 use reqwest::{blocking::Client, Url};
 use serde::{Deserialize, Serialize};
@@ -69,7 +69,7 @@ impl TransportClient {
     }
 
     /// Registers an `on_open` callback.
-    pub fn set_on_open<F>(&mut self, function: F) -> Result<(), Error>
+    pub fn set_on_open<F>(&mut self, function: F) -> Result<()>
     where
         F: Fn(()) + 'static + Sync + Send,
     {
@@ -80,7 +80,7 @@ impl TransportClient {
     }
 
     /// Registers an `on_error` callback.
-    pub fn set_on_error<F>(&mut self, function: F) -> Result<(), Error>
+    pub fn set_on_error<F>(&mut self, function: F) -> Result<()>
     where
         F: Fn(String) + 'static + Sync + Send,
     {
@@ -91,7 +91,7 @@ impl TransportClient {
     }
 
     /// Registers an `on_packet` callback.
-    pub fn set_on_packet<F>(&mut self, function: F) -> Result<(), Error>
+    pub fn set_on_packet<F>(&mut self, function: F) -> Result<()>
     where
         F: Fn(Packet) + 'static + Sync + Send,
     {
@@ -102,7 +102,7 @@ impl TransportClient {
     }
 
     /// Registers an `on_data` callback.
-    pub fn set_on_data<F>(&mut self, function: F) -> Result<(), Error>
+    pub fn set_on_data<F>(&mut self, function: F) -> Result<()>
     where
         F: Fn(Vec<u8>) + 'static + Sync + Send,
     {
@@ -113,7 +113,7 @@ impl TransportClient {
     }
 
     /// Registers an `on_close` callback.
-    pub fn set_on_close<F>(&mut self, function: F) -> Result<(), Error>
+    pub fn set_on_close<F>(&mut self, function: F) -> Result<()>
     where
         F: Fn(()) + 'static + Sync + Send,
     {
@@ -127,7 +127,7 @@ impl TransportClient {
     /// request to the server, the server passes back the handshake data in the
     /// response. Afterwards a first Pong packet is sent to the server to
     /// trigger the Ping-cycle.
-    pub fn open<T: Into<String> + Clone>(&mut self, address: T) -> Result<(), Error> {
+    pub fn open<T: Into<String> + Clone>(&mut self, address: T) -> Result<()> {
         // TODO: Check if Relaxed is appropiate -> change all occurences if not
         if self.connected.load(Ordering::Relaxed) {
             return Ok(());
@@ -177,7 +177,7 @@ impl TransportClient {
     }
 
     /// Sends a packet to the server
-    pub fn emit(&self, packet: Packet) -> Result<(), Error> {
+    pub fn emit(&self, packet: Packet) -> Result<()> {
         if !self.connected.load(Ordering::Relaxed) {
             let error = Error::ActionBeforeOpen;
             self.call_error_callback(format!("{}", error))?;
@@ -214,7 +214,7 @@ impl TransportClient {
     /// Performs the server long polling procedure as long as the client is
     /// connected. This should run separately at all time to ensure proper
     /// response handling from the server.
-    pub fn poll_cycle(&self) -> Result<(), Error> {
+    pub fn poll_cycle(&self) -> Result<()> {
         if !self.connected.load(Ordering::Relaxed) {
             let error = Error::ActionBeforeOpen;
             self.call_error_callback(format!("{}", error))?;
@@ -324,7 +324,7 @@ impl TransportClient {
 
     /// Calls the error callback with a given message.
     #[inline]
-    fn call_error_callback(&self, text: String) -> Result<(), Error> {
+    fn call_error_callback(&self, text: String) -> Result<()> {
         let function = self.on_error.read()?;
         if let Some(function) = function.as_ref() {
             spawn_scoped!(function(text));
diff --git a/src/error.rs b/src/error.rs
index 3ac4bd40..4e1ddcc8 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -25,6 +25,10 @@ pub enum Error {
     PoisonedLockError,
 }
 
+impl std::error::Error for Error {}
+
+pub(crate) type Result<T> = std::result::Result<T, Error>;
+
 impl From<DecodeError> for Error {
     fn from(error: DecodeError) -> Self {
         Self::Base64Error(error)
diff --git a/src/lib.rs b/src/lib.rs
index 049b01c9..cd332eb5 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -85,7 +85,7 @@ pub mod socketio;
 /// crate. Handles all kinds of errors.
 pub mod error;
 
-use crate::error::Error;
+use crate::error::Result;
 use std::time::Duration;
 
 use crate::socketio::transport::TransportClient;
@@ -134,7 +134,7 @@ impl Socket {
     ///
     /// assert!(result.is_ok());
     /// ```
-    pub fn on<F>(&mut self, event: &str, callback: F) -> Result<(), Error>
+    pub fn on<F>(&mut self, event: &str, callback: F) -> Result<()>
     where
         F: FnMut(String) + 'static + Sync + Send,
     {
@@ -156,7 +156,7 @@ impl Socket {
     ///
     /// assert!(result.is_ok());
     /// ```
-    pub fn connect(&mut self) -> Result<(), Error> {
+    pub fn connect(&mut self) -> Result<()> {
         self.transport.connect()
     }
 
@@ -182,7 +182,7 @@ impl Socket {
     /// assert!(result.is_ok());
     /// ```
     #[inline]
-    pub fn emit(&mut self, event: &str, data: &str) -> Result<(), Error> {
+    pub fn emit(&mut self, event: &str, data: &str) -> Result<()> {
         self.transport.emit(event.into(), data)
     }
 
@@ -226,7 +226,7 @@ impl Socket {
         data: &str,
         timeout: Duration,
         callback: F,
-    ) -> Result<(), Error>
+    ) -> Result<()>
     where
         F: FnMut(String) + 'static + Send + Sync,
     {
diff --git a/src/socketio/packet.rs b/src/socketio/packet.rs
index a9fb11ce..708a4251 100644
--- a/src/socketio/packet.rs
+++ b/src/socketio/packet.rs
@@ -1,4 +1,4 @@
-use crate::error::Error;
+use crate::error::{Error, Result};
 use bytes::Bytes;
 use regex::Regex;
 
@@ -27,7 +27,7 @@ pub struct Packet {
 
 /// Converts a `u8` into a `PacketId`.
 #[inline]
-pub const fn u8_to_packet_id(b: u8) -> Result<PacketId, Error> {
+pub const fn u8_to_packet_id(b: u8) -> Result<PacketId> {
     match b as char {
         '0' => Ok(PacketId::Connect),
         '1' => Ok(PacketId::Disconnect),
@@ -113,7 +113,7 @@ impl Packet {
     }
 
     /// Decodes a packet given a `Bytes` type.
-    pub fn decode_bytes(payload: Bytes) -> Result<Self, Error> {
+    pub fn decode_bytes(payload: Bytes) -> Result<Self> {
         let mut i = 0;
         let packet_id = u8_to_packet_id(*payload.first().ok_or(Error::EmptyPacket)?)?;
 
diff --git a/src/socketio/transport.rs b/src/socketio/transport.rs
index 9783247b..386c3579 100644
--- a/src/socketio/transport.rs
+++ b/src/socketio/transport.rs
@@ -2,7 +2,7 @@ use crate::engineio::{
     packet::{Packet as EnginePacket, PacketId as EnginePacketId},
     socket::EngineSocket,
 };
-use crate::error::Error;
+use crate::error::{Error, Result};
 use crate::socketio::packet::{Packet as SocketPacket, PacketId as SocketPacketId};
 use bytes::Bytes;
 use if_chain::if_chain;
@@ -60,7 +60,7 @@ impl TransportClient {
     }
 
     /// Registers a new event with some callback function `F`.
-    pub fn on<F>(&mut self, event: Event, callback: F) -> Result<(), Error>
+    pub fn on<F>(&mut self, event: Event, callback: F) -> Result<()>
     where
         F: FnMut(String) + 'static + Sync + Send,
     {
@@ -72,7 +72,7 @@ impl TransportClient {
 
     /// Connects to the server. This includes a connection of the underlying
     /// engine.io client and afterwards an opening socket.io request.
-    pub fn connect(&mut self) -> Result<(), Error> {
+    pub fn connect(&mut self) -> Result<()> {
         self.setup_callbacks()?;
 
         self.engine_socket
@@ -96,7 +96,7 @@ impl TransportClient {
     }
 
     /// Sends a `socket.io` packet to the server using the `engine.io` client.
-    pub fn send(&self, packet: &SocketPacket) -> Result<(), Error> {
+    pub fn send(&self, packet: &SocketPacket) -> Result<()> {
         if !self.engineio_connected.load(Ordering::Relaxed) {
             return Err(Error::ActionBeforeOpen);
         }
@@ -109,7 +109,7 @@ impl TransportClient {
 
     /// Emits to certain event with given data. The data needs to be JSON,
     /// otherwise this returns an `InvalidJson` error.
-    pub fn emit(&self, event: Event, data: &str) -> Result<(), Error> {
+    pub fn emit(&self, event: Event, data: &str) -> Result<()> {
         if serde_json::from_str::<serde_json::Value>(data).is_err() {
             return Err(Error::InvalidJson(data.to_owned()));
         }
@@ -140,7 +140,7 @@ impl TransportClient {
         data: &str,
         timeout: Duration,
         callback: F,
-    ) -> Result<(), Error>
+    ) -> Result<()>
     where
         F: FnMut(String) + 'static + Send + Sync,
     {
@@ -265,7 +265,7 @@ impl TransportClient {
 
     /// Sets up the callback routes on the engine.io socket, called before
     /// opening the connection.
-    fn setup_callbacks(&mut self) -> Result<(), Error> {
+    fn setup_callbacks(&mut self) -> Result<()> {
         let clone_self = self.clone();
         let error_callback = move |msg| {
             if let Some(function) = clone_self.get_event_callback(&Event::Error) {
@@ -309,10 +309,7 @@ impl TransportClient {
 
     /// A method for handling the Event Socket Packets.
     // this could only be called with an event
-    fn handle_event(
-        socket_packet: SocketPacket,
-        clone_self: &TransportClient,
-    ) -> Result<(), Error> {
+    fn handle_event(socket_packet: SocketPacket, clone_self: &TransportClient) -> Result<()> {
         // unwrap the potential data
         if let Some(data) = socket_packet.data {
             if_chain! {


Test patch: