|
| 1 | +use crate::fs::{PathBuf, PathError}; |
| 2 | +use alloc::string::FromUtf8Error; |
| 3 | +use core::fmt::Debug; |
| 4 | +use derive_more::Display; |
| 5 | + |
| 6 | +/// All errors that can happen when working with the [`FileSystem`]. |
| 7 | +/// |
| 8 | +/// [`FileSystem`]: super::FileSystem |
| 9 | +#[derive(Debug, Clone, Display, PartialEq, Eq)] |
| 10 | +pub enum FileSystemError { |
| 11 | + /// Logical errors. See [`LogicError`]. |
| 12 | + Logic(LogicError), |
| 13 | + /// IO (low-level UEFI-errors) errors. See [`FileSystemIOError`]. |
| 14 | + IO(FileSystemIOError), |
| 15 | + /// Path-related errors. See [`PathError`]. |
| 16 | + Path(PathError), |
| 17 | + /// Can't parse file content as UTF-8. See [`FromUtf8Error`]. |
| 18 | + Utf8Encoding(FromUtf8Error), |
| 19 | +} |
| 20 | + |
| 21 | +/// UEFI-error with context when working with the underlying UEFI file protocol. |
| 22 | +#[derive(Debug, Clone, Display, PartialEq, Eq)] |
| 23 | +#[display(fmt = "FileSystemIOError({},{})", context, path)] |
| 24 | +pub struct FileSystemIOError { |
| 25 | + /// The path that led to the error. |
| 26 | + pub path: PathBuf, |
| 27 | + /// The context in which the path was used. |
| 28 | + pub context: FileSystemIOErrorContext, |
| 29 | + /// The underlying UEFI error. |
| 30 | + pub uefi_error: crate::Error, |
| 31 | +} |
| 32 | + |
| 33 | +/// Logical errors when working with files. |
| 34 | +#[derive(Debug, Clone, Display, PartialEq, Eq)] |
| 35 | +pub enum LogicError { |
| 36 | + /// The path exists but does not correspond to a directory when a directory |
| 37 | + /// was expected. |
| 38 | + NotADirectory(PathBuf), |
| 39 | + /// The path exists but does not correspond to a file when a file was |
| 40 | + /// expected. |
| 41 | + NotAFile(PathBuf), |
| 42 | +} |
| 43 | + |
| 44 | +/// Enum that further specifies the context in that a [`FileSystemError`] |
| 45 | +/// occurred. |
| 46 | +#[derive(Debug, Clone, Display, PartialEq, Eq)] |
| 47 | +pub enum FileSystemIOErrorContext { |
| 48 | + /// Can't delete the directory. |
| 49 | + CantDeleteDirectory, |
| 50 | + /// Can't delete the file. |
| 51 | + CantDeleteFile, |
| 52 | + /// Error flushing file. |
| 53 | + FlushFailure, |
| 54 | + /// Can't open the root directory of the underlying volume. |
| 55 | + CantOpenVolume, |
| 56 | + /// Error while reading the metadata of the file. |
| 57 | + Metadata, |
| 58 | + /// Could not open the given path. One possible reason is that the file does |
| 59 | + /// not exist. |
| 60 | + OpenError, |
| 61 | + /// Error reading file. |
| 62 | + ReadFailure, |
| 63 | + /// Error writing bytes. |
| 64 | + WriteFailure, |
| 65 | +} |
| 66 | + |
| 67 | +impl From<PathError> for FileSystemError { |
| 68 | + fn from(value: PathError) -> Self { |
| 69 | + Self::Path(value) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(feature = "unstable")] |
| 74 | +impl core::error::Error for FileSystemError { |
| 75 | + fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { |
| 76 | + match self { |
| 77 | + FileSystemError::IO(err) => Some(err), |
| 78 | + FileSystemError::Path(err) => Some(err), |
| 79 | + FileSystemError::Utf8Encoding(err) => Some(err), |
| 80 | + FileSystemError::Logic(err) => Some(err), |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +#[cfg(feature = "unstable")] |
| 86 | +impl core::error::Error for FileSystemIOError { |
| 87 | + fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { |
| 88 | + Some(&self.uefi_error) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[cfg(feature = "unstable")] |
| 93 | +impl core::error::Error for LogicError {} |
0 commit comments