1pub use io_error::{self, Kind as IoErrorKind};
9
10pub type Result<T> = core::result::Result<T, IoErrorKind>;
12
13pub trait IntoIoErrorKind {
18 fn into_io_error_kind(self) -> IoErrorKind;
20}
21
22impl IntoIoErrorKind for IoErrorKind {
23 fn into_io_error_kind(self) -> IoErrorKind {
24 self
25 }
26}
27
28impl IntoIoErrorKind for task::Error {
29 fn into_io_error_kind(self) -> IoErrorKind {
30 use task::Error::*;
31 match self {
32 InvalidTaskIdentifier | ThreadNotRegistered | NoThreadForTask => {
33 IoErrorKind::NoSuchTask
34 }
35 InvalidSpawnerIdentifier | InvalidEnvironmentVariable => IoErrorKind::InvalidArgument,
36 ThreadAlreadyRegistered | AlreadyInitialized | AlreadySet => IoErrorKind::InvalidState,
37 FailedToCreateThread | FailedToSpawnThread | TooManyTasks | TooManySpawners => {
38 IoErrorKind::OutOfMemory
39 }
40 NotInitialized | NoSpawnerAvailable => IoErrorKind::DeviceUnavailable,
41 }
42 }
43}
44
45impl IntoIoErrorKind for users::Error {
46 fn into_io_error_kind(self) -> IoErrorKind {
47 use users::Error::*;
48 match self {
49 DuplicateGroupIdentifier
50 | DuplicateGroupName
51 | DuplicateUserIdentifier
52 | DuplicateUserName => IoErrorKind::AlreadyExists,
53 InvalidGroupIdentifier | InvalidUserIdentifier | PoisonedLock => {
55 IoErrorKind::InputOutput
56 }
57 TooManyGroups | TooManyUsers => IoErrorKind::NoSpace,
58 NotInitialized => IoErrorKind::DeviceUnavailable,
59 AlreadyInitialized => IoErrorKind::InvalidState,
60 }
61 }
62}
63
64pub trait ResultExt<T> {
66 fn map_io_error(self) -> Result<T>;
68}
69
70impl<T, E: IntoIoErrorKind> ResultExt<T> for core::result::Result<T, E> {
71 fn map_io_error(self) -> Result<T> {
72 self.map_err(IntoIoErrorKind::into_io_error_kind)
73 }
74}
75
76#[cfg(test)]
77mod tests {
78 use super::*;
79 use embedded_io_async::{Error as _, ErrorKind};
80
81 #[test]
82 fn a_kind_keeps_its_meaning_through_embedded_io() {
83 for (kind, expected) in [
84 (IoErrorKind::NotFound, ErrorKind::NotFound),
85 (IoErrorKind::PermissionDenied, ErrorKind::PermissionDenied),
86 (IoErrorKind::AlreadyExists, ErrorKind::AlreadyExists),
87 (IoErrorKind::OutOfMemory, ErrorKind::OutOfMemory),
88 (IoErrorKind::Unsupported, ErrorKind::Unsupported),
89 (IoErrorKind::InputOutput, ErrorKind::Other),
91 (IoErrorKind::NoSpace, ErrorKind::Other),
93 ] {
94 assert_eq!(kind.kind(), expected, "{kind:?}");
95 }
96 }
97
98 #[test]
99 fn task_errors_keep_their_cause() {
100 use task::Error::*;
101 for (error, kind) in [
102 (InvalidTaskIdentifier, IoErrorKind::NoSuchTask),
103 (NoThreadForTask, IoErrorKind::NoSuchTask),
104 (InvalidSpawnerIdentifier, IoErrorKind::InvalidArgument),
105 (AlreadySet, IoErrorKind::InvalidState),
106 (TooManyTasks, IoErrorKind::OutOfMemory),
107 (NoSpawnerAvailable, IoErrorKind::DeviceUnavailable),
108 ] {
109 assert_eq!(error.into_io_error_kind(), kind);
110 }
111 }
112
113 #[test]
114 fn users_errors_keep_their_cause() {
115 use users::Error::*;
116 for (error, kind) in [
117 (DuplicateUserName, IoErrorKind::AlreadyExists),
118 (InvalidUserIdentifier, IoErrorKind::InputOutput),
119 (PoisonedLock, IoErrorKind::InputOutput),
120 (TooManyUsers, IoErrorKind::NoSpace),
121 (NotInitialized, IoErrorKind::DeviceUnavailable),
122 (AlreadyInitialized, IoErrorKind::InvalidState),
123 ] {
124 assert_eq!(error.into_io_error_kind(), kind);
125 }
126 }
127
128 #[test]
129 fn results_convert_at_the_call_site() {
130 let failed: core::result::Result<(), task::Error> = Err(task::Error::InvalidTaskIdentifier);
131 assert_eq!(failed.map_io_error(), Err(IoErrorKind::NoSuchTask));
132 let passed: core::result::Result<u8, users::Error> = Ok(3);
133 assert_eq!(passed.map_io_error(), Ok(3));
134 }
135}