From 58100eb80dc20283e3b4de178082ef17f6213551 Mon Sep 17 00:00:00 2001 From: Lizzy Fleckenstein Date: Thu, 22 Dec 2022 23:02:33 +0100 Subject: files --- src/error.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/error.rs (limited to 'src/error.rs') 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 for Error { + fn from(err: io::Error) -> Self { + Self::IoError(err) + } +} + +impl From> for Error { + fn from(err: TryFromPrimitiveError) -> Self { + Self::InvalidType(err.number) + } +} + +impl From> for Error { + fn from(err: TryFromPrimitiveError) -> Self { + Self::InvalidType(err.number) + } +} + +impl From> for Error { + fn from(_err: mpsc::SendError) -> 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"), + } + } +} -- cgit v1.2.3