1#![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 #[derive(Clone, Copy, PartialEq, Eq)]
32 #[non_exhaustive]
33 #[repr(u16)]
34 pub enum Kind {
35 $($kind = $id,)*
36 }
37
38 impl Kind {
39 pub const ALL: &'static [Self] = &[$(Self::$kind,)*];
41
42 pub const fn id(self) -> u16 {
44 self as u16
45 }
46
47 pub const fn posix_errno(self) -> PosixErrno {
50 match self {
51 $(Self::$kind => PosixErrno::$errno,)*
52 }
53 }
54
55 #[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#[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 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};