diff options
author | Lizzy Fleckenstein <eliasfleckenstein@web.de> | 2022-12-22 23:02:33 +0100 |
---|---|---|
committer | Lizzy Fleckenstein <eliasfleckenstein@web.de> | 2022-12-22 23:02:33 +0100 |
commit | 58100eb80dc20283e3b4de178082ef17f6213551 (patch) | |
tree | d88135e35f7aa13c7deb3d85aad3e323bdee2618 /src/error.rs | |
parent | 209fcdbebf6354fe3246938e46dac516c6205630 (diff) | |
download | mt_rudp-58100eb80dc20283e3b4de178082ef17f6213551.tar.xz |
files
Diffstat (limited to 'src/error.rs')
-rw-r--r-- | src/error.rs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..02080c7 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,57 @@ +use crate::{CtlType, InPkt, PktType}; +use num_enum::TryFromPrimitiveError; +use std::{fmt, io, sync::mpsc}; + +#[derive(Debug)] +pub enum Error { + IoError(io::Error), + InvalidProtoId(u32), + InvalidPeerID, + InvalidChannel(u8), + InvalidType(u8), + InvalidCtlType(u8), + RemoteDisco, + LocalDisco, +} + +impl From<io::Error> for Error { + fn from(err: io::Error) -> Self { + Self::IoError(err) + } +} + +impl From<TryFromPrimitiveError<PktType>> for Error { + fn from(err: TryFromPrimitiveError<PktType>) -> Self { + Self::InvalidType(err.number) + } +} + +impl From<TryFromPrimitiveError<CtlType>> for Error { + fn from(err: TryFromPrimitiveError<CtlType>) -> Self { + Self::InvalidType(err.number) + } +} + +impl From<mpsc::SendError<InPkt>> for Error { + fn from(_err: mpsc::SendError<InPkt>) -> Self { + Self::LocalDisco // technically not a disconnect but a local drop + } +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + use Error::*; + write!(f, "RUDP Error: ")?; + + match self { + IoError(err) => write!(f, "IO Error: {}", err), + InvalidProtoId(id) => write!(f, "Invalid Protocol ID: {id}"), + InvalidPeerID => write!(f, "Invalid Peer ID"), + InvalidChannel(ch) => write!(f, "Invalid Channel: {ch}"), + InvalidType(tp) => write!(f, "Invalid Type: {tp}"), + InvalidCtlType(tp) => write!(f, "Invalid Control Type: {tp}"), + RemoteDisco => write!(f, "Remote Disconnected"), + LocalDisco => write!(f, "Local Disconnected"), + } + } +} |