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
// This is free and unencumbered software released into the public domain.

use crate::{
    prelude::{fmt, String, ToString},
    DecodeError, PortID,
};

#[cfg(feature = "std")]
extern crate std;

pub type PortResult<T> = Result<T, PortError>;

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PortError {
    Invalid(PortID),
    Closed,
    Disconnected,
    RecvFailed,
    SendFailed,
    DecodeFailed(DecodeError),
    Other(String),
}

impl fmt::Display for PortError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Invalid(port) => write!(f, "Port #{} is invalid", port),
            Self::Closed => write!(f, "Port is closed"),
            Self::Disconnected => write!(f, "Port is not connected"),
            Self::RecvFailed => write!(f, "Port receive failed"),
            Self::SendFailed => write!(f, "Port send failed"),
            Self::DecodeFailed(error) => write!(f, "Port decode failed: {}", error),
            Self::Other(message) => write!(f, "{}", message),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for PortError {}

#[cfg(feature = "std")]
impl From<std::io::Error> for PortError {
    fn from(error: std::io::Error) -> Self {
        Self::Other(error.to_string())
    }
}

impl From<DecodeError> for PortError {
    fn from(error: DecodeError) -> Self {
        Self::DecodeFailed(error)
    }
}