]> git.lizzy.rs Git - mt_rudp.git/blobdiff - src/error.rs
impl std::error:Error for Error
[mt_rudp.git] / src / error.rs
index 02080c7de5156eb5fa5549e7b22e0a10c454fe9d..e0481205b071f3ff7ea739fbec486e31644fe71d 100644 (file)
@@ -1,16 +1,19 @@
-use crate::{CtlType, InPkt, PktType};
+use crate::prelude::*;
 use num_enum::TryFromPrimitiveError;
-use std::{fmt, io, sync::mpsc};
+use std::{fmt, io};
+use tokio::sync::mpsc::error::SendError;
 
 #[derive(Debug)]
 pub enum Error {
     IoError(io::Error),
     InvalidProtoId(u32),
-    InvalidPeerID,
     InvalidChannel(u8),
     InvalidType(u8),
     InvalidCtlType(u8),
-    RemoteDisco,
+    PeerIDAlreadySet,
+    InvalidChunkIndex(usize, usize),
+    InvalidChunkCount(usize, usize),
+    RemoteDisco(bool),
     LocalDisco,
 }
 
@@ -32,26 +35,34 @@ impl From<TryFromPrimitiveError<CtlType>> for Error {
     }
 }
 
-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 From<SendError<InPkt>> for Error {
+    fn from(_err: SendError<InPkt>) -> Self {
+        Self::LocalDisco
     }
 }
 
 impl fmt::Display for Error {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         use Error::*;
-        write!(f, "RUDP Error: ")?;
+        write!(f, "rudp: ")?;
 
         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"),
+            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 {}