Skip to main content

file_system/
error.rs

1//! Error vocabulary shared by file systems, the VFS and network transport.
2//!
3//! Every fallible I/O operation reports an [`IoErrorKind`]: one fieldless,
4//! two-byte kind with a stable ID. Errors owned by other subsystems enter the
5//! I/O path through [`IntoIoErrorKind`], an explicit mapping at the boundary
6//! that never leaks the subsystem's internal step names into user messages.
7
8pub use io_error::{self, Kind as IoErrorKind};
9
10/// Result type of every file system, VFS and device operation.
11pub type Result<T> = core::result::Result<T, IoErrorKind>;
12
13/// Explicit translation of a subsystem error into an I/O kind.
14///
15/// Implemented by the crate that owns the source error, so the shared
16/// `io_error` crate stays free of subsystem dependencies.
17pub trait IntoIoErrorKind {
18    /// Return the I/O kind that best describes this failure for a caller.
19    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            // A stale owner or group is a broken invariant, not a caller mistake.
54            InvalidGroupIdentifier | InvalidUserIdentifier | PoisonedLock => {
55                IoErrorKind::InputOutput
56            }
57            TooManyGroups | TooManyUsers => IoErrorKind::NoSpace,
58            NotInitialized => IoErrorKind::DeviceUnavailable,
59            AlreadyInitialized => IoErrorKind::InvalidState,
60        }
61    }
62}
63
64/// Convert a result whose error implements [`IntoIoErrorKind`].
65pub trait ResultExt<T> {
66    /// Translate the error into an [`IoErrorKind`].
67    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            // Generic I/O must never ask a caller to retry with `Interrupted`.
90            (IoErrorKind::InputOutput, ErrorKind::Other),
91            // Storage exhaustion is not memory exhaustion.
92            (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}