]> git.lizzy.rs Git - mt_rudp.git/blob - src/error.rs
Rework structure
[mt_rudp.git] / src / error.rs
1 use super::*;
2 use num_enum::TryFromPrimitiveError;
3 use thiserror::Error;
4 use tokio::sync::mpsc::error::SendError;
5
6 #[derive(Error, Debug)]
7 pub enum Error {
8     #[error("io error: {0}")]
9     IoError(#[from] std::io::Error),
10     #[error("invalid protocol ID: {0}")]
11     InvalidProtoId(u32),
12     #[error("invalid channel: {0}")]
13     InvalidChannel(u8),
14     #[error("invalid type: {0}")]
15     InvalidType(u8),
16     #[error("invalid control type: {0}")]
17     InvalidCtlType(u8),
18     #[error("peer ID already set")]
19     PeerIDAlreadySet,
20     #[error("chunk index {0} bigger than chunk count {1}")]
21     InvalidChunkIndex(usize, usize),
22     #[error("chunk count changed from {0} to {1}")]
23     InvalidChunkCount(usize, usize),
24     #[error("remote disconnected (timeout = {0})")]
25     RemoteDisco(bool),
26     #[error("local disconnected")]
27     LocalDisco,
28 }
29
30 impl From<TryFromPrimitiveError<PktType>> for Error {
31     fn from(err: TryFromPrimitiveError<PktType>) -> Self {
32         Self::InvalidType(err.number)
33     }
34 }
35
36 impl From<TryFromPrimitiveError<CtlType>> for Error {
37     fn from(err: TryFromPrimitiveError<CtlType>) -> Self {
38         Self::InvalidCtlType(err.number)
39     }
40 }
41
42 impl From<SendError<InPkt>> for Error {
43     fn from(_err: SendError<InPkt>) -> Self {
44         Self::LocalDisco
45     }
46 }