format: add the Storage trait; make the error enums non-exhaustive

Range-read milestone M1, first step (docs/design/range-reads.md §3(a)):
a synchronous, no_std read interface with u64 offsets, read_at returning
Cow<[u8]>, read_ranges, len and an as_contiguous fast path. Implemented
for [u8], Vec<u8>, &T, Box<T> and Arc<T>; slices serve borrowed bytes.
read_exact_at reproduces the parsers' UnexpectedEof bounds error exactly,
so converted modules keep their error values.

FormatError gains Storage(String) and ContiguousStorageRequired; it and
the facade Error are now #[non_exhaustive] (breaking for exhaustive
matches, noted in the changelog; the Python bindings' match gets a
wildcard arm).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
osobh
2026-09-26 12:44:36 -05:00
co-authored by Claude Opus 5.5
parent f2ff2c424f
commit 6a4707d791
6 changed files with 399 additions and 0 deletions
+21
View File
@@ -12,7 +12,11 @@ use std::string::String;
use core::fmt;
/// Errors that can occur when parsing HDF5 binary format structures.
///
/// Non-exhaustive: new failure modes (new storage backends, new file
/// features) add variants, so a `match` needs a wildcard arm.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum FormatError {
/// The HDF5 magic signature was not found at any valid offset.
SignatureNotFound,
@@ -243,6 +247,13 @@ pub enum FormatError {
/// A metadata cache image block libhdf5 refuses to load (the reason is
/// libhdf5's own error text).
InvalidCacheImage(&'static str),
/// The [`Storage`](crate::storage::Storage) backend failed to serve a
/// read (an I/O or network error, or a short read inside the file).
Storage(String),
/// The operation still needs the whole file as one slice and the
/// [`Storage`](crate::storage::Storage) backend has no contiguous view
/// (`as_contiguous()` is `None`); the text names the operation.
ContiguousStorageRequired(&'static str),
}
impl fmt::Display for FormatError {
@@ -529,6 +540,16 @@ impl fmt::Display for FormatError {
FormatError::InvalidCacheImage(why) => {
write!(f, "invalid metadata cache image: {why}")
}
FormatError::Storage(why) => {
write!(f, "storage read failed: {why}")
}
FormatError::ContiguousStorageRequired(what) => {
write!(
f,
"{what} needs the whole file in memory, which this storage backend does \
not provide"
)
}
}
}
}