indexbus_inspect/
errors.rs

1//! Error type for `indexbus-inspect`.
2//!
3//! This error is primarily surfaced as a process exit via `main() -> Result<(), Error>`.
4//! It is also re-exported so callers/tests can match on structured error kinds.
5
6use core::fmt;
7
8/// Errors returned by `indexbus-inspect`.
9#[derive(Debug)]
10pub enum Error {
11    /// A filesystem, mapping, or other I/O error.
12    Io(std::io::Error),
13    /// CLI arguments were invalid (unknown flag, parse failure, missing value).
14    InvalidArgs,
15    /// The mapped file was smaller than the minimum required to read requested fields.
16    TooSmall {
17        /// Bytes needed to continue.
18        needed: usize,
19        /// Bytes actually available.
20        got: usize,
21    },
22    /// The region header magic/version is not compatible with IndexBus v1.
23    IncompatibleLayout,
24    /// The header was readable but did not describe a supported/known layout.
25    UnsupportedLayout {
26        /// Capabilities bitset from the header.
27        capabilities: u32,
28        /// Total layout bytes from the header.
29        layout_bytes: u32,
30    },
31}
32
33impl fmt::Display for Error {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self {
36            Error::Io(e) => write!(f, "io error: {e}"),
37            Error::InvalidArgs => write!(f, "invalid arguments"),
38            Error::TooSmall { needed, got } => {
39                write!(f, "mapping too small (needed {needed} bytes, got {got})")
40            }
41            Error::IncompatibleLayout => write!(f, "incompatible layout header (magic/version)"),
42            Error::UnsupportedLayout {
43                capabilities,
44                layout_bytes,
45            } => write!(
46                f,
47                "unsupported/unknown layout (capabilities=0x{capabilities:08x}, layout_bytes={layout_bytes})"
48            ),
49        }
50    }
51}
52
53impl std::error::Error for Error {}
54
55impl From<std::io::Error> for Error {
56    fn from(value: std::io::Error) -> Self {
57        Self::Io(value)
58    }
59}