diff --git a/src/errors/error.rs b/src/errors/error.rs index 638eb5c..9f776d3 100644 --- a/src/errors/error.rs +++ b/src/errors/error.rs @@ -1,4 +1,4 @@ -use crate::GDErrorKind; +use crate::{GDErrorKind, Packet, Socket}; use std::error::Error; use std::fmt::Formatter; use std::{backtrace, fmt}; diff --git a/src/errors/kind.rs b/src/errors/kind.rs index bf48d07..e77f800 100644 --- a/src/errors/kind.rs +++ b/src/errors/kind.rs @@ -1,39 +1,59 @@ use crate::GDError; use std::error::Error; -/// GameDig Error. #[derive(Debug, Clone, PartialEq, Eq)] -pub enum GDErrorKind { +pub enum Packet { /// The received packet was bigger than the buffer size. - PacketOverflow, + Overflow, /// The received packet was shorter than the expected one. - PacketUnderflow, + Underflow, /// The received packet is badly formatted. - PacketBad, - /// Couldn't send the packet. - PacketSend, - /// Couldn't send the receive. - PacketReceive, + Bad, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Parse { /// Couldn't decompress data. Decompress, - /// Couldn't create a socket connection. - SocketConnect, - /// Couldn't bind a socket. - SocketBind, - /// Invalid input. - InvalidInput, - /// The server queried is not the queried game server. - BadGame, - /// Couldn't automatically query. - AutoQuery, - /// A protocol-defined expected format was not met. - ProtocolFormat, /// Couldn't cast a value to an enum. UnknownEnumCast, /// Couldn't parse a json string. JsonParse, /// Couldn't parse a value. TypeParse, + /// A protocol-defined expected format was not met. + ProtocolFormat, + /// The server queried is not the queried game server. + BadGame, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Socket { + /// Couldn't create a socket connection. + Connect, + /// Couldn't bind a socket. + Bind, + /// Couldn't send a packet. + Send, + /// Couldn't receive a packet. + Receive, +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Input { + /// Invalid input. + Invalid, + /// Couldn't automatically query. + AutoQuery, +} + +/// GameDig Error. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum GDErrorKind { + Packet(Packet), + Parse(Parse), + Socket(Socket), + Input(Input), } impl GDErrorKind { @@ -41,8 +61,8 @@ impl GDErrorKind { /// backtrace) /// /// ``` - /// use gamedig::{GDErrorKind, GDResult}; - /// let _: GDResult = "thing".parse().map_err(|e| GDErrorKind::TypeParse.context(e)); + /// use gamedig::{GDErrorKind, GDResult, Parse}; + /// let _: GDResult = "thing".parse().map_err(|e| GDErrorKind::Parse(Parse::TypeParse).context(e)); /// ``` pub fn context>>(self, source: E) -> GDError { GDError::from_error(self, source) } } @@ -54,7 +74,7 @@ mod tests { // Testing cloning the GDErrorKind type #[test] fn test_cloning() { - let error = GDErrorKind::BadGame; + let error = GDErrorKind::Parse(Parse::BadGame); let cloned_error = error.clone(); assert_eq!(error, cloned_error); } @@ -62,7 +82,7 @@ mod tests { // test display GDError #[test] fn test_display() { - let err = GDErrorKind::BadGame.context("Rust is not a game"); + let err = Parse::BadGame.context("Rust is not a game"); assert_eq!( format!("{err}"), "GDError{ kind=BadGame\n source=\"Rust is not a game\"\n backtrace=\n}\n"