From: Lizzy Fleckenstein Date: Wed, 15 Feb 2023 11:49:00 +0000 (+0100) Subject: Use thiserror X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=ea481ae3539e27a98ace48e746fea4a2cbf4d557;p=mt_rudp.git Use thiserror --- diff --git a/Cargo.toml b/Cargo.toml index 53ab291..a15ed83 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ async-recursion = "1.0.0" async-trait = "0.1.60" byteorder = "1.4.3" num_enum = "0.5.7" +thiserror = "1.0.38" tokio = { version = "1.23.0", features = ["sync", "time", "net", "signal", "macros", "rt"] } [dev-dependencies] diff --git a/src/error.rs b/src/error.rs index 55566fa..bac843a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,28 +1,32 @@ use crate::prelude::*; use num_enum::TryFromPrimitiveError; -use std::{fmt, io}; +use thiserror::Error; use tokio::sync::mpsc::error::SendError; -#[derive(Debug)] +#[derive(Error, Debug)] pub enum Error { - IoError(io::Error), + #[error("io error: {0}")] + IoError(#[from] std::io::Error), + #[error("invalid protocol ID: {0}")] InvalidProtoId(u32), + #[error("invalid channel: {0}")] InvalidChannel(u8), + #[error("invalid type: {0}")] InvalidType(u8), + #[error("invalid control type: {0}")] InvalidCtlType(u8), + #[error("peer ID already set")] PeerIDAlreadySet, + #[error("chunk index {0} bigger than chunk count {1}")] InvalidChunkIndex(usize, usize), + #[error("chunk count changed from {0} to {1}")] InvalidChunkCount(usize, usize), + #[error("remote disconnected (timeout = {0})")] RemoteDisco(bool), + #[error("local disconnected")] LocalDisco, } -impl From for Error { - fn from(err: io::Error) -> Self { - Self::IoError(err) - } -} - impl From> for Error { fn from(err: TryFromPrimitiveError) -> Self { Self::InvalidType(err.number) @@ -31,7 +35,7 @@ impl From> for Error { impl From> for Error { fn from(err: TryFromPrimitiveError) -> Self { - Self::InvalidType(err.number) + Self::InvalidCtlType(err.number) } } @@ -40,29 +44,3 @@ impl From> for Error { Self::LocalDisco } } - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use Error::*; - write!(f, "rudp: ")?; - - match self { - IoError(err) => write!(f, "IO error: {err}"), - InvalidProtoId(id) => write!(f, "invalid protocol ID: {id}"), - InvalidChannel(ch) => write!(f, "invalid channel: {ch}"), - InvalidType(tp) => write!(f, "invalid type: {tp}"), - InvalidCtlType(tp) => write!(f, "invalid control type: {tp}"), - PeerIDAlreadySet => write!(f, "peer ID already set"), - InvalidChunkIndex(i, n) => write!(f, "chunk index {i} bigger than chunk count {n}"), - InvalidChunkCount(o, n) => write!(f, "chunk count changed from {o} to {n}"), - RemoteDisco(to) => write!( - f, - "remote disconnected{}", - if *to { " (timeout)" } else { "" } - ), - LocalDisco => write!(f, "local disconnected"), - } - } -} - -impl std::error::Error for Error {}