mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-05-06 07:17:27 +00:00
Make error a folder and separate code
This commit is contained in:
parent
211cd5fd5f
commit
3db40649f5
4 changed files with 106 additions and 86 deletions
|
|
@ -1,57 +1,7 @@
|
|||
use std::{
|
||||
backtrace,
|
||||
error::Error,
|
||||
fmt::{self, Formatter},
|
||||
};
|
||||
|
||||
/// Result of Type and GDError.
|
||||
pub type GDResult<T> = Result<T, GDError>;
|
||||
|
||||
/// GameDig Error.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum GDErrorKind {
|
||||
/// The received packet was bigger than the buffer size.
|
||||
PacketOverflow,
|
||||
/// The received packet was shorter than the expected one.
|
||||
PacketUnderflow,
|
||||
/// The received packet is badly formatted.
|
||||
PacketBad,
|
||||
/// Couldn't send the packet.
|
||||
PacketSend,
|
||||
/// Couldn't send the receive.
|
||||
PacketReceive,
|
||||
/// 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,
|
||||
}
|
||||
|
||||
impl GDErrorKind {
|
||||
/// Convert error kind into a full error with a source (and implicit
|
||||
/// backtrace)
|
||||
///
|
||||
/// ```
|
||||
/// use gamedig::{GDErrorKind, GDResult};
|
||||
/// let _: GDResult<u32> = "thing".parse().map_err(|e| GDErrorKind::TypeParse.context(e));
|
||||
/// ```
|
||||
pub fn context<E: Into<Box<dyn Error + 'static>>>(self, source: E) -> GDError { GDError::from_error(self, source) }
|
||||
}
|
||||
use crate::GDErrorKind;
|
||||
use std::error::Error;
|
||||
use std::fmt::Formatter;
|
||||
use std::{backtrace, fmt};
|
||||
|
||||
type ErrorSource = Box<dyn Error + 'static>;
|
||||
|
||||
|
|
@ -147,38 +97,6 @@ impl GDError {
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// Testing Ok variant of the GDResult type
|
||||
#[test]
|
||||
fn test_gdresult_ok() {
|
||||
let result: GDResult<u32> = Ok(42);
|
||||
assert_eq!(result.unwrap(), 42);
|
||||
}
|
||||
|
||||
// Testing Err variant of the GDResult type
|
||||
#[test]
|
||||
fn test_gdresult_err() {
|
||||
let result: GDResult<u32> = Err(GDErrorKind::InvalidInput.into());
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
// Testing cloning the GDErrorKind type
|
||||
#[test]
|
||||
fn test_cloning() {
|
||||
let error = GDErrorKind::BadGame;
|
||||
let cloned_error = error.clone();
|
||||
assert_eq!(error, cloned_error);
|
||||
}
|
||||
|
||||
// test display GDError
|
||||
#[test]
|
||||
fn test_display() {
|
||||
let err = GDErrorKind::BadGame.context("Rust is not a game");
|
||||
assert_eq!(
|
||||
format!("{err}"),
|
||||
"GDError{ kind=BadGame\n source=\"Rust is not a game\"\n backtrace=<disabled>\n}\n"
|
||||
);
|
||||
}
|
||||
|
||||
// test error trait GDError
|
||||
#[test]
|
||||
fn test_error_trait() {
|
||||
71
src/errors/kind.rs
Normal file
71
src/errors/kind.rs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
use crate::GDError;
|
||||
use std::error::Error;
|
||||
|
||||
/// GameDig Error.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum GDErrorKind {
|
||||
/// The received packet was bigger than the buffer size.
|
||||
PacketOverflow,
|
||||
/// The received packet was shorter than the expected one.
|
||||
PacketUnderflow,
|
||||
/// The received packet is badly formatted.
|
||||
PacketBad,
|
||||
/// Couldn't send the packet.
|
||||
PacketSend,
|
||||
/// Couldn't send the receive.
|
||||
PacketReceive,
|
||||
/// 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,
|
||||
}
|
||||
|
||||
impl GDErrorKind {
|
||||
/// Convert error kind into a full error with a source (and implicit
|
||||
/// backtrace)
|
||||
///
|
||||
/// ```
|
||||
/// use gamedig::{GDErrorKind, GDResult};
|
||||
/// let _: GDResult<u32> = "thing".parse().map_err(|e| GDErrorKind::TypeParse.context(e));
|
||||
/// ```
|
||||
pub fn context<E: Into<Box<dyn Error + 'static>>>(self, source: E) -> GDError { GDError::from_error(self, source) }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// Testing cloning the GDErrorKind type
|
||||
#[test]
|
||||
fn test_cloning() {
|
||||
let error = GDErrorKind::BadGame;
|
||||
let cloned_error = error.clone();
|
||||
assert_eq!(error, cloned_error);
|
||||
}
|
||||
|
||||
// test display GDError
|
||||
#[test]
|
||||
fn test_display() {
|
||||
let err = GDErrorKind::BadGame.context("Rust is not a game");
|
||||
assert_eq!(
|
||||
format!("{err}"),
|
||||
"GDError{ kind=BadGame\n source=\"Rust is not a game\"\n backtrace=<disabled>\n}\n"
|
||||
);
|
||||
}
|
||||
}
|
||||
7
src/errors/mod.rs
Normal file
7
src/errors/mod.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
pub mod error;
|
||||
pub mod kind;
|
||||
pub mod result;
|
||||
|
||||
pub use error::*;
|
||||
pub use kind::*;
|
||||
pub use result::*;
|
||||
24
src/errors/result.rs
Normal file
24
src/errors/result.rs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use crate::GDError;
|
||||
|
||||
/// Result of Type and GDError.
|
||||
pub type GDResult<T> = Result<T, GDError>;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::GDErrorKind;
|
||||
|
||||
// Testing Ok variant of the GDResult type
|
||||
#[test]
|
||||
fn test_gdresult_ok() {
|
||||
let result: GDResult<u32> = Ok(42);
|
||||
assert_eq!(result.unwrap(), 42);
|
||||
}
|
||||
|
||||
// Testing Err variant of the GDResult type
|
||||
#[test]
|
||||
fn test_gdresult_err() {
|
||||
let result: GDResult<u32> = Err(GDErrorKind::InvalidInput.into());
|
||||
assert!(result.is_err());
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue