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