diff --git a/Cargo.lock b/Cargo.lock index 28936dc..830ca58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -719,7 +719,6 @@ version = "0.2.0" dependencies = [ "bitflags 2.5.0", "log", - "thiserror", ] [[package]] diff --git a/protocol/Cargo.toml b/protocol/Cargo.toml index 83a8484..2bbee0e 100644 --- a/protocol/Cargo.toml +++ b/protocol/Cargo.toml @@ -12,6 +12,5 @@ homepage = "https://xash.su" repository = "https://git.mentality.rip/numas13/xash3d-master" [dependencies] -thiserror = "1.0.49" log = "0.4.18" bitflags = "2.4" diff --git a/protocol/src/cursor.rs b/protocol/src/cursor.rs index a8d2524..1902064 100644 --- a/protocol/src/cursor.rs +++ b/protocol/src/cursor.rs @@ -6,43 +6,51 @@ use std::{ mem, str, }; -use thiserror::Error; - use super::color; use super::wrappers::Str; /// The error type for `Cursor` and `CursorMut`. -#[derive(Error, Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq)] pub enum CursorError { /// Invalid number. - #[error("Invalid number")] InvalidNumber, /// Invalid string. - #[error("Invalid string")] InvalidString, /// Invalid boolean. - #[error("Invalid boolean")] InvalidBool, /// Invalid table entry. - #[error("Invalid table key")] InvalidTableKey, /// Invalid table entry. - #[error("Invalid table entry")] InvalidTableValue, /// Table end found. - #[error("Table end")] TableEnd, /// Expected data not found. - #[error("Expected data not found")] Expect, /// An unexpected data found. - #[error("Unexpected data")] ExpectEmpty, /// Buffer size is no enougth to decode or encode a packet. - #[error("Unexpected end of buffer")] UnexpectedEnd, } +impl fmt::Display for CursorError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + let s = match self { + Self::InvalidNumber => "Invalid number", + Self::InvalidString => "Invalid string", + Self::InvalidBool => "Invalid boolean", + Self::InvalidTableKey => "Invalid table key", + Self::InvalidTableValue => "Invalid table entry", + Self::TableEnd => "Table end", + Self::Expect => "Expected data not found", + Self::ExpectEmpty => "Unexpected data", + Self::UnexpectedEnd => "Unexpected end of buffer", + }; + s.fmt(fmt) + } +} + +impl std::error::Error for CursorError {} + pub type Result = std::result::Result; pub trait GetKeyValue<'a>: Sized { diff --git a/protocol/src/lib.rs b/protocol/src/lib.rs index 80f8a5a..0fd2dfe 100644 --- a/protocol/src/lib.rs +++ b/protocol/src/lib.rs @@ -20,11 +20,11 @@ pub mod master; pub mod server; pub mod wrappers; +use std::fmt; + pub use cursor::CursorError; pub use server_info::ServerInfo; -use thiserror::Error; - use crate::filter::Version; /// Current protocol version. @@ -33,30 +33,53 @@ pub const PROTOCOL_VERSION: u8 = 49; pub const CLIENT_VERSION: Version = Version::new(0, 20); /// The error type for decoding and encoding packets. -#[derive(Error, Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq)] pub enum Error { /// Failed to decode a packet. - #[error("Invalid packet")] InvalidPacket, /// Invalid region. - #[error("Invalid region")] InvalidRegion, /// Invalid client announce IP. - #[error("Invalid client announce IP")] InvalidClientAnnounceIp, /// Invalid last IP. - #[error("Invalid last server IP")] InvalidQueryServersLast, /// Server protocol version is not supported. - #[error("Invalid protocol version")] InvalidProtocolVersion, /// Cursor error. - #[error("{0}")] - CursorError(#[from] CursorError), + CursorError(CursorError), /// Invalid value for server add packet. - #[error("Invalid value for server add key `{0}`: {1}")] - InvalidServerValue(&'static str, #[source] CursorError), + InvalidServerValue(&'static str, CursorError), /// Invalid value for query servers packet. - #[error("Invalid value for filter key `{0}`: {1}")] - InvalidFilterValue(&'static str, #[source] CursorError), + InvalidFilterValue(&'static str, CursorError), +} + +impl fmt::Display for Error { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + match self { + Self::InvalidPacket => "Invalid packet".fmt(fmt), + Self::InvalidRegion => "Invalid region".fmt(fmt), + Self::InvalidClientAnnounceIp => "Invalid client announce IP".fmt(fmt), + Self::InvalidQueryServersLast => "Invalid last server IP".fmt(fmt), + Self::InvalidProtocolVersion => "Invalid protocol version".fmt(fmt), + Self::CursorError(source) => source.fmt(fmt), + Self::InvalidServerValue(key, source) => { + write!( + fmt, + "Invalid value for server add key `{}`: {}", + key, source + ) + } + Self::InvalidFilterValue(key, source) => { + write!(fmt, "Invalid value for filter key `{}`: {}", key, source) + } + } + } +} + +impl std::error::Error for Error {} + +impl From for Error { + fn from(source: CursorError) -> Self { + Self::CursorError(source) + } }