Skip to main content

partitions/
error.rs

1use std::fmt;
2use std::io;
3
4#[derive(Debug)]
5pub enum Error {
6    /// Block-layer error (read failure, short read, etc.). Lifted from
7    /// `fs_core::Error` so any `BlockRead` failure flows up unchanged.
8    Block(fs_core::Error),
9    /// Underlying I/O failure that didn't go through fs-core (rare —
10    /// retained for direct `std::io::Error` sources).
11    Io(io::Error),
12    /// No GPT signature and no MBR signature found.
13    NoPartitionTable,
14    /// GPT header was located but failed CRC validation.
15    GptHeaderCrc,
16    /// GPT partition-entry array failed CRC validation.
17    GptEntriesCrc,
18    /// GPT header field combination is internally inconsistent.
19    GptCorrupt(&'static str),
20    /// MBR signature missing or extended-partition chain broken.
21    MbrCorrupt(&'static str),
22    /// GPT primary header and backup header disagree on the partition list,
23    /// header fields, or entry-array CRC. Carries a short reason string. The
24    /// probe path treats this as advisory by default — the variant only
25    /// surfaces if a caller explicitly asks for backup validation.
26    GptBackupMismatch(&'static str),
27    /// Mutation API rejected an input: overlap, out-of-bounds, alignment
28    /// impossible, etc. Carries a short reason string.
29    Invalid(&'static str),
30    /// Mutation API tried to write a partition table that does not fit the
31    /// device, or the device is too small for the chosen table type.
32    DeviceTooSmall,
33}
34
35impl fmt::Display for Error {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Error::Block(e) => write!(f, "{e}"),
39            Error::Io(e) => write!(f, "io: {e}"),
40            Error::NoPartitionTable => write!(f, "no GPT or MBR signature found"),
41            Error::GptHeaderCrc => write!(f, "GPT header CRC32 mismatch"),
42            Error::GptEntriesCrc => write!(f, "GPT partition-entry array CRC32 mismatch"),
43            Error::GptCorrupt(s) => write!(f, "GPT corrupt: {s}"),
44            Error::MbrCorrupt(s) => write!(f, "MBR corrupt: {s}"),
45            Error::GptBackupMismatch(s) => write!(f, "GPT backup mismatch: {s}"),
46            Error::Invalid(s) => write!(f, "invalid argument: {s}"),
47            Error::DeviceTooSmall => write!(f, "device too small for the requested table"),
48        }
49    }
50}
51
52impl std::error::Error for Error {
53    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
54        match self {
55            Error::Block(e) => Some(e),
56            Error::Io(e) => Some(e),
57            _ => None,
58        }
59    }
60}
61
62impl From<io::Error> for Error {
63    fn from(e: io::Error) -> Self {
64        Error::Io(e)
65    }
66}
67
68impl From<fs_core::Error> for Error {
69    fn from(e: fs_core::Error) -> Self {
70        Error::Block(e)
71    }
72}
73
74pub type Result<T> = std::result::Result<T, Error>;