protoflow_core/
port_error.rs

1// This is free and unencumbered software released into the public domain.
2
3use crate::{
4    prelude::{fmt, String, ToString},
5    DecodeError, PortID,
6};
7
8#[cfg(feature = "std")]
9extern crate std;
10
11pub type PortResult<T> = Result<T, PortError>;
12
13#[derive(Clone, Debug, Eq, PartialEq)]
14pub enum PortError {
15    Invalid(PortID),
16    Closed,
17    Disconnected,
18    RecvFailed,
19    SendFailed,
20    DecodeFailed(DecodeError),
21    Other(String),
22}
23
24impl fmt::Display for PortError {
25    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26        match self {
27            Self::Invalid(port) => write!(f, "Port #{} is invalid", port),
28            Self::Closed => write!(f, "Port is closed"),
29            Self::Disconnected => write!(f, "Port is not connected"),
30            Self::RecvFailed => write!(f, "Port receive failed"),
31            Self::SendFailed => write!(f, "Port send failed"),
32            Self::DecodeFailed(error) => write!(f, "Port decode failed: {}", error),
33            Self::Other(message) => write!(f, "{}", message),
34        }
35    }
36}
37
38#[cfg(feature = "std")]
39impl std::error::Error for PortError {}
40
41#[cfg(feature = "std")]
42impl From<std::io::Error> for PortError {
43    fn from(error: std::io::Error) -> Self {
44        Self::Other(error.to_string())
45    }
46}
47
48impl From<DecodeError> for PortError {
49    fn from(error: DecodeError) -> Self {
50        Self::DecodeFailed(error)
51    }
52}