aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 02080c7de5156eb5fa5549e7b22e0a10c454fe9d (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
47
48
49
50
51
52
53
54
55
56
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<io::Error> for Error {
    fn from(err: io::Error) -> Self {
        Self::IoError(err)
    }
}

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

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 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"),
        }
    }
}