Skip to main content

io_error/
resolve.rs

1//! Typed resolver failures.
2//!
3//! DNS lookups report their own fieldless error rather than a bare [`Kind`]:
4//! callers can tell a malformed name from an unavailable network, and a
5//! confirmed negative answer from an unknown failure. Each variant carries the
6//! stable ID of the [`Kind`] it projects to.
7
8use core::fmt;
9
10use crate::{Kind, UnknownKind};
11
12macro_rules! define_resolve_errors {
13    ($($name:ident = $kind:ident => $gai:ident;)*) => {
14        /// A resolver failure with a stable Xila ID (equal to its [`Kind`]'s).
15        #[derive(Clone, Copy, PartialEq, Eq)]
16        #[non_exhaustive]
17        #[repr(u16)]
18        pub enum ResolveError {
19            $($name = Kind::$kind as u16,)*
20        }
21
22        impl ResolveError {
23            /// All resolver errors, in declaration order.
24            pub const ALL: &'static [Self] = &[$(Self::$name,)*];
25
26            /// Return the stable Xila ID, identical to the projected [`Kind`] ID.
27            pub const fn id(self) -> u16 {
28                self as u16
29            }
30
31            /// Project to the general I/O kind used for device files and errno.
32            pub const fn kind(self) -> Kind {
33                match self {
34                    $(Self::$name => Kind::$kind,)*
35                }
36            }
37
38            /// Project to the `getaddrinfo` return domain.
39            pub const fn getaddrinfo(self) -> GetAddrInfoError {
40                match self {
41                    $(Self::$name => GetAddrInfoError::$gai,)*
42                }
43            }
44        }
45
46        impl TryFrom<u16> for ResolveError {
47            type Error = UnknownKind;
48
49            fn try_from(id: u16) -> Result<Self, Self::Error> {
50                $(if id == Kind::$kind as u16 { return Ok(Self::$name); })*
51                Err(UnknownKind(id))
52            }
53        }
54    };
55}
56
57/// Symbolic `getaddrinfo` failure codes (`EAI_*`). Numeric values belong to
58/// the target libc.
59#[derive(Clone, Copy, Debug, PartialEq, Eq)]
60#[non_exhaustive]
61#[allow(non_camel_case_types, reason = "spell EAI symbols as in libc headers")]
62pub enum GetAddrInfoError {
63    /// `EAI_NONAME`
64    EAI_NONAME,
65    /// `EAI_FAIL`
66    EAI_FAIL,
67    /// `EAI_FAMILY`
68    EAI_FAMILY,
69    /// `EAI_AGAIN`
70    EAI_AGAIN,
71    /// `EAI_MEMORY`
72    EAI_MEMORY,
73    /// `EAI_SYSTEM`: the cause is the errno of [`ResolveError::kind`].
74    EAI_SYSTEM,
75}
76
77define_resolve_errors! {
78    InvalidName = InvalidDnsName => EAI_NONAME;
79    NameTooLong = NameTooLong => EAI_NONAME;
80    UnsupportedQuery = UnsupportedDnsQuery => EAI_FAIL;
81    UnsupportedFamily = AddressFamilyUnsupported => EAI_FAMILY;
82    NoAnswer = NameNotResolved => EAI_NONAME;
83    TemporaryFailure = ResolverTemporaryFailure => EAI_AGAIN;
84    Failure = ResolverFailure => EAI_FAIL;
85    OutOfMemory = OutOfMemory => EAI_MEMORY;
86    NoQuerySlots = NoBuffers => EAI_AGAIN;
87    NoInterface = DeviceUnavailable => EAI_SYSTEM;
88    NetworkDown = NetworkDown => EAI_SYSTEM;
89    NetworkUnreachable = NetworkUnreachable => EAI_SYSTEM;
90    TimedOut = TimedOut => EAI_AGAIN;
91    SystemIo = InputOutput => EAI_SYSTEM;
92}
93
94impl From<ResolveError> for Kind {
95    fn from(error: ResolveError) -> Self {
96        error.kind()
97    }
98}
99
100impl fmt::Display for ResolveError {
101    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
102        fmt::Display::fmt(&self.kind(), f)
103    }
104}
105
106impl fmt::Debug for ResolveError {
107    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108        // Like `Kind`, keep the variant-name table out of firmware.
109        write!(f, "ResolveError({:#06x})", self.id())
110    }
111}
112
113impl core::error::Error for ResolveError {}
114
115const _: () = assert!(size_of::<ResolveError>() == 2);