Skip to main content

onc_rpcbind/
error.rs

1use std::error::Error as StdError;
2use std::fmt;
3
4use onc_rpc_client::RpcError;
5
6/// Error from portmapper operations.
7///
8/// Returned by [`PortmapperClient::getport`](crate::PortmapperClient::getport).
9#[derive(Debug)]
10#[non_exhaustive]
11pub enum PortmapError {
12    /// The RPC call failed.
13    Rpc(RpcError),
14    /// The requested program is not registered with the portmapper.
15    ProgramUnavailable,
16    /// The portmapper returned a port number that does not fit in a `u16`.
17    InvalidPortValue(u32),
18}
19
20impl fmt::Display for PortmapError {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Self::Rpc(e) => e.fmt(f),
24            Self::ProgramUnavailable => write!(f, "Program unavailable"),
25            Self::InvalidPortValue(value) => write!(f, "Invalid port value: {value}"),
26        }
27    }
28}
29
30impl StdError for PortmapError {
31    fn source(&self) -> Option<&(dyn StdError + 'static)> {
32        match self {
33            Self::Rpc(e) => Some(e),
34            Self::ProgramUnavailable | Self::InvalidPortValue(_) => None,
35        }
36    }
37}
38
39impl From<RpcError> for PortmapError {
40    fn from(e: RpcError) -> Self {
41        Self::Rpc(e)
42    }
43}
44
45impl PortmapError {
46    /// Returns `true` if the connection that produced this error is still in
47    /// a clean state and may be reused.
48    #[must_use]
49    pub const fn is_connection_reusable(&self) -> bool {
50        match self {
51            Self::Rpc(e) => e.is_connection_reusable(),
52            Self::ProgramUnavailable | Self::InvalidPortValue(_) => true,
53        }
54    }
55}