From e4e74a9d9331d25159dd8176f09e5ae679ddb263 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Wed, 4 Mar 2026 22:28:01 +0100 Subject: [PATCH] block: Add BlockErrorKind classification enum Add a small, stable enum that classifies block errors into broad categories - I/O, invalid format, unsupported feature, corrupt image, out of bounds, not found, overflow. Callers match on this for control flow rather than on format specific error variants. Signed-off-by: Anatol Belski --- block/src/error.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/block/src/error.rs b/block/src/error.rs index a8dc2c1ad..3b7ae1f29 100644 --- a/block/src/error.rs +++ b/block/src/error.rs @@ -15,3 +15,42 @@ //! |-- VhdError / RawError / ... //! +-- io::Error / etc. //! ``` + +use std::fmt::{self, Display, Formatter}; + +/// Small, stable classification of block errors. +/// +/// Callers match on this for control flow. Adding new format specific +/// errors does not require new variants here. +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +#[non_exhaustive] +pub enum BlockErrorKind { + /// An underlying I/O operation failed. + Io, + /// The disk image format is structurally invalid. + InvalidFormat, + /// The disk image requires a feature that is not implemented. + UnsupportedFeature, + /// The image is marked or detected as corrupt. + CorruptImage, + /// An address, offset, or index is outside the valid range. + OutOfBounds, + /// A file or required internal structure could not be found. + NotFound, + /// An internal counter or limit was exceeded. + Overflow, +} + +impl Display for BlockErrorKind { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + Self::Io => write!(f, "I/O error"), + Self::InvalidFormat => write!(f, "invalid format"), + Self::UnsupportedFeature => write!(f, "unsupported feature"), + Self::CorruptImage => write!(f, "corrupt image"), + Self::OutOfBounds => write!(f, "out of bounds"), + Self::NotFound => write!(f, "not found"), + Self::Overflow => write!(f, "overflow"), + } + } +}