diff --git a/block/src/error.rs b/block/src/error.rs index ebaa33ec5..afd4b5533 100644 --- a/block/src/error.rs +++ b/block/src/error.rs @@ -231,4 +231,56 @@ impl From for BlockError { } } +/// Temporary scaffolding: classify a `qcow::Error` into the appropriate +/// `BlockErrorKind`. +/// +/// This impl exists only to allow an incremental migration of the qcow +/// parse/construct chain from `qcow::Result` to `BlockResult`. Each +/// subsequent commit replaces bare `?` sites with explicit +/// `BlockError::new(kind, e)` calls. Once every site is migrated this +/// impl will be removed. +impl From for BlockError { + fn from(e: crate::qcow::Error) -> Self { + use crate::qcow::Error as E; + let kind = match &e { + // Structural / format violations + E::InvalidMagic + | E::BackingFileTooLong(_) + | E::InvalidBackingFileName(_) + | E::InvalidClusterSize + | E::InvalidL1TableSize(_) + | E::InvalidL1TableOffset + | E::InvalidOffset(_) + | E::InvalidRefcountTableOffset + | E::InvalidRefcountTableSize(_) + | E::FileTooBig(_) + | E::NoRefcountClusters + | E::RefcountTableOffEnd + | E::RefcountTableTooLarge + | E::TooManyL1Entries(_) + | E::TooManyRefcounts(_) + | E::SizeTooSmallForNumberOfClusters => BlockErrorKind::InvalidFormat, + + // Unsupported features / versions + E::UnsupportedVersion(_) + | E::UnsupportedFeature(_) + | E::UnsupportedCompressionType + | E::UnsupportedBackingFileFormat(_) + | E::UnsupportedRefcountOrder + | E::BackingFilesDisabled + | E::ShrinkNotSupported => BlockErrorKind::UnsupportedFeature, + + // Corrupt image + E::CorruptImage => BlockErrorKind::CorruptImage, + + // Nesting depth overflow + E::MaxNestingDepthExceeded => BlockErrorKind::Overflow, + + // Everything else is I/O + _ => BlockErrorKind::Io, + }; + Self::new(kind, e) + } +} + pub type BlockResult = Result;