Skip to main content

hadris_block/
error.rs

1use core::fmt;
2
3use crate::detect::{BlockFormat, FatVariant, PartitionTableKind};
4
5/// Error returned by category-level block operations.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum Error {
9    /// The source could not be read or repositioned.
10    Io(hadris_io::Error),
11    /// No supported format was recognized.
12    UnknownFormat,
13    /// The source is a partitioned disk rather than a directly openable volume.
14    PartitionedDisk(PartitionTableKind),
15    /// The detected format has no category-level opener enabled.
16    UnsupportedFormat(BlockFormat),
17    /// Cheap detection and full filesystem validation disagreed.
18    DetectedFormatMismatch {
19        /// Format reported by lightweight detection.
20        detected: FatVariant,
21        /// Format reported after the filesystem was fully opened.
22        opened: FatVariant,
23    },
24    /// FAT validation failed.
25    Fat(hadris_fat::Error),
26}
27
28/// Result type for category-level block operations.
29pub type Result<T> = core::result::Result<T, Error>;
30
31impl fmt::Display for Error {
32    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            Self::Io(error) => write!(formatter, "block detection I/O error: {error}"),
35            Self::UnknownFormat => formatter.write_str("unknown block volume format"),
36            Self::PartitionedDisk(kind) => write!(
37                formatter,
38                "{kind:?} disk must be opened through a partition view"
39            ),
40            Self::UnsupportedFormat(format) => {
41                write!(formatter, "unsupported block format: {format:?}")
42            }
43            Self::DetectedFormatMismatch { detected, opened } => write!(
44                formatter,
45                "detected {detected:?}, but full validation opened {opened:?}"
46            ),
47            Self::Fat(error) => write!(formatter, "FAT open failed: {error}"),
48        }
49    }
50}
51
52#[cfg(feature = "std")]
53impl std::error::Error for Error {}
54
55impl From<hadris_io::Error> for Error {
56    fn from(error: hadris_io::Error) -> Self {
57        Self::Io(error)
58    }
59}
60
61impl From<hadris_fat::Error> for Error {
62    fn from(error: hadris_fat::Error) -> Self {
63        Self::Fat(error)
64    }
65}