Skip to main content

io_error/
lib.rs

1//! Common errors for file systems, device I/O and network transport.
2//!
3//! [`Kind`] is the single failure vocabulary: two bytes, an explicit stable ID,
4//! a symbolic POSIX projection ([`Kind::posix_errno`]), an `embedded_io`
5//! classification and a localized message. [`ResolveError`] is the typed DNS
6//! result. See the crate README for the ID bands and the rules for choosing a kind.
7#![no_std]
8
9#[cfg(feature = "std")]
10extern crate std;
11
12use core::fmt;
13use embedded_io::ErrorKind;
14
15embedded_i18n::i18n!("locales");
16
17mod display;
18mod posix;
19mod resolve;
20#[cfg(feature = "std")]
21mod std_error;
22
23pub use posix::PosixErrno;
24pub use resolve::{GetAddrInfoError, ResolveError};
25#[cfg(feature = "std")]
26pub use std_error::map_std_error;
27
28macro_rules! define_kinds {
29    ($($kind:ident = $id:literal => $errno:ident, $embedded:ident;)*) => {
30        /// A semantic I/O failure with a stable Xila ID.
31        #[derive(Clone, Copy, PartialEq, Eq)]
32        #[non_exhaustive]
33        #[repr(u16)]
34        pub enum Kind {
35            $($kind = $id,)*
36        }
37
38        impl Kind {
39            /// All assigned kinds, in ID order.
40            pub const ALL: &'static [Self] = &[$(Self::$kind,)*];
41
42            /// Return the stable Xila ID. Zero is reserved for success.
43            pub const fn id(self) -> u16 {
44                self as u16
45            }
46
47            /// Project to a symbolic POSIX errno. The caller supplies target
48            /// numbers and applies any operation-specific policy.
49            pub const fn posix_errno(self) -> PosixErrno {
50                match self {
51                    $(Self::$kind => PosixErrno::$errno,)*
52                }
53            }
54
55            /// Variant spelling for the native ABI generator.
56            #[doc(hidden)]
57            pub const fn variant_name(self) -> &'static str {
58                match self {
59                    $(Self::$kind => stringify!($kind),)*
60                }
61            }
62        }
63
64        impl TryFrom<u16> for Kind {
65            type Error = UnknownKind;
66
67            fn try_from(id: u16) -> Result<Self, Self::Error> {
68                match id {
69                    $($id => Ok(Self::$kind),)*
70                    _ => Err(UnknownKind(id)),
71                }
72            }
73        }
74
75        impl embedded_io::Error for Kind {
76            fn kind(&self) -> ErrorKind {
77                match self {
78                    $(Self::$kind => ErrorKind::$embedded,)*
79                }
80            }
81        }
82    };
83}
84
85include!("catalog.rs");
86
87/// A reserved or unknown Xila error ID.
88#[derive(Clone, Copy, Debug, PartialEq, Eq)]
89pub struct UnknownKind(pub u16);
90
91impl fmt::Display for UnknownKind {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        write!(f, "Unknown I/O error ID {:#06x}", self.0)
94    }
95}
96
97impl core::error::Error for UnknownKind {}
98
99impl fmt::Debug for Kind {
100    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101        // Keep the variant-name table out of firmware using Debug.
102        write!(f, "IoErrorKind({:#06x})", self.id())
103    }
104}
105
106impl core::error::Error for Kind {}
107
108const _: () = {
109    assert!(size_of::<Kind>() == 2);
110    assert!(size_of::<Result<(), Kind>>() == 2);
111    #[cfg(target_pointer_width = "32")]
112    {
113        assert!(size_of::<Result<usize, Kind>>() == 8);
114        assert!(size_of::<Result<u64, Kind>>() <= 16);
115    }
116};