use std::error::Error as StdError;
use std::fmt;
use super::RpcError;
#[derive(Debug)]
pub enum PortmapError {
Rpc(RpcError),
ProgramUnavailable,
InvalidPortValue(u32),
}
impl fmt::Display for PortmapError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Rpc(e) => e.fmt(f),
Self::ProgramUnavailable => write!(f, "Program unavailable"),
Self::InvalidPortValue(value) => write!(f, "Invalid port value: {value}"),
}
}
}
impl StdError for PortmapError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Self::Rpc(e) => Some(e),
Self::ProgramUnavailable | Self::InvalidPortValue(_) => None,
}
}
}
impl From<RpcError> for PortmapError {
fn from(e: RpcError) -> Self {
Self::Rpc(e)
}
}
impl PortmapError {
#[must_use]
pub const fn is_connection_reusable(&self) -> bool {
match self {
Self::Rpc(e) => e.is_connection_reusable(),
Self::ProgramUnavailable | Self::InvalidPortValue(_) => true,
}
}
}