1use core::fmt;
2
3use crate::detect::{BlockFormat, FatVariant, PartitionTableKind};
4
5#[derive(Debug)]
7#[non_exhaustive]
8pub enum Error {
9 Io(hadris_io::Error),
11 UnknownFormat,
13 PartitionedDisk(PartitionTableKind),
15 UnsupportedFormat(BlockFormat),
17 DetectedFormatMismatch {
19 detected: FatVariant,
21 opened: FatVariant,
23 },
24 Fat(hadris_fat::Error),
26}
27
28pub 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}