]> git.lizzy.rs Git - mt_rudp.git/blob - src/error.rs
timeouts
[mt_rudp.git] / src / error.rs
1 use crate::{CtlType, InPkt, PktType};
2 use num_enum::TryFromPrimitiveError;
3 use std::{fmt, io};
4 use tokio::sync::mpsc::error::SendError;
5
6 #[derive(Debug)]
7 pub enum Error {
8     IoError(io::Error),
9     InvalidProtoId(u32),
10     InvalidChannel(u8),
11     InvalidType(u8),
12     InvalidCtlType(u8),
13     PeerIDAlreadySet,
14     InvalidChunkIndex(usize, usize),
15     InvalidChunkCount(usize, usize),
16     RemoteDisco(bool),
17     LocalDisco,
18 }
19
20 impl From<io::Error> for Error {
21     fn from(err: io::Error) -> Self {
22         Self::IoError(err)
23     }
24 }
25
26 impl From<TryFromPrimitiveError<PktType>> for Error {
27     fn from(err: TryFromPrimitiveError<PktType>) -> Self {
28         Self::InvalidType(err.number)
29     }
30 }
31
32 impl From<TryFromPrimitiveError<CtlType>> for Error {
33     fn from(err: TryFromPrimitiveError<CtlType>) -> Self {
34         Self::InvalidType(err.number)
35     }
36 }
37
38 impl From<SendError<InPkt>> for Error {
39     fn from(_err: SendError<InPkt>) -> Self {
40         Self::LocalDisco
41     }
42 }
43
44 impl fmt::Display for Error {
45     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46         use Error::*;
47         write!(f, "rudp: ")?;
48
49         match self {
50             IoError(err) => write!(f, "IO error: {}", err),
51             InvalidProtoId(id) => write!(f, "invalid protocol ID: {id}"),
52             InvalidChannel(ch) => write!(f, "invalid channel: {ch}"),
53             InvalidType(tp) => write!(f, "invalid type: {tp}"),
54             InvalidCtlType(tp) => write!(f, "invalid control type: {tp}"),
55             PeerIDAlreadySet => write!(f, "peer ID already set"),
56             InvalidChunkIndex(i, n) => write!(f, "chunk index {i} bigger than chunk count {n}"),
57             InvalidChunkCount(o, n) => write!(f, "chunk count changed from {o} to {n}"),
58             RemoteDisco(to) => write!(
59                 f,
60                 "remote disconnected{}",
61                 if *to { " (timeout)" } else { "" }
62             ),
63             LocalDisco => write!(f, "local disconnected"),
64         }
65     }
66 }