aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 7cfc0578572766d985e5ac6869cfa2c0a1c1d8bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use super::*;
use num_enum::TryFromPrimitiveError;
use thiserror::Error;
use tokio::sync::mpsc::error::SendError;

#[derive(Error, Debug)]
pub enum Error {
    #[error("io error: {0}")]
    IoError(#[from] std::io::Error),
    #[error("invalid protocol ID: {0}")]
    InvalidProtoId(u32),
    #[error("invalid channel: {0}")]
    InvalidChannel(u8),
    #[error("invalid type: {0}")]
    InvalidType(u8),
    #[error("invalid control type: {0}")]
    InvalidCtlType(u8),
    #[error("peer ID already set")]
    PeerIDAlreadySet,
    #[error("chunk index {0} bigger than chunk count {1}")]
    InvalidChunkIndex(usize, usize),
    #[error("chunk count changed from {0} to {1}")]
    InvalidChunkCount(usize, usize),
    #[error("remote disconnected (timeout = {0})")]
    RemoteDisco(bool),
    #[error("local disconnected")]
    LocalDisco,
}

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::InvalidCtlType(err.number)
    }
}

impl From<SendError<InPkt>> for Error {
    fn from(_err: SendError<InPkt>) -> Self {
        Self::LocalDisco
    }
}