Skip to main content

hdf5_reader/
error.rs

1/// Errors produced by the HDF5 reader.
2#[derive(Debug, thiserror::Error)]
3pub enum Error {
4    #[error("I/O error: {0}")]
5    Io(#[from] std::io::Error),
6
7    #[error("invalid HDF5 magic bytes")]
8    InvalidMagic,
9
10    #[error("unsupported superblock version {0}")]
11    UnsupportedSuperblockVersion(u8),
12
13    #[error("unsupported object header version {0}")]
14    UnsupportedObjectHeaderVersion(u8),
15
16    #[error("unsupported B-tree version {0}")]
17    UnsupportedBTreeVersion(u8),
18
19    #[error("unsupported symbol table node version {0}")]
20    UnsupportedSymbolTableNodeVersion(u8),
21
22    #[error("unsupported local heap version {0}")]
23    UnsupportedLocalHeapVersion(u8),
24
25    #[error("unsupported global heap version {0}")]
26    UnsupportedGlobalHeapVersion(u8),
27
28    #[error("unsupported fractal heap version {0}")]
29    UnsupportedFractalHeapVersion(u8),
30
31    #[error("unsupported dataspace version {0}")]
32    UnsupportedDataspaceVersion(u8),
33
34    #[error("unsupported datatype class {0}")]
35    UnsupportedDatatypeClass(u8),
36
37    #[error("unsupported layout class {0}")]
38    UnsupportedLayoutClass(u8),
39
40    #[error("unsupported layout version {0}")]
41    UnsupportedLayoutVersion(u8),
42
43    #[error("unsupported filter pipeline version {0}")]
44    UnsupportedFilterPipelineVersion(u8),
45
46    #[error("unsupported fill value version {0}")]
47    UnsupportedFillValueVersion(u8),
48
49    #[error("unsupported link message version {0}")]
50    UnsupportedLinkVersion(u8),
51
52    #[error("unsupported link type {0}")]
53    UnsupportedLinkType(u8),
54
55    #[error("unsupported attribute message version {0}")]
56    UnsupportedAttributeVersion(u8),
57
58    #[error("unsupported B-tree v2 record type {0}")]
59    UnsupportedBTreeV2RecordType(u8),
60
61    #[error("unsupported chunk indexing type {0}")]
62    UnsupportedChunkIndexType(u8),
63
64    #[error("unsupported size of offsets: {0}")]
65    UnsupportedOffsetSize(u8),
66
67    #[error("unsupported size of lengths: {0}")]
68    UnsupportedLengthSize(u8),
69
70    #[error("invalid B-tree signature")]
71    InvalidBTreeSignature,
72
73    #[error("invalid symbol table node signature")]
74    InvalidSymbolTableNodeSignature,
75
76    #[error("invalid local heap signature")]
77    InvalidLocalHeapSignature,
78
79    #[error("invalid global heap signature")]
80    InvalidGlobalHeapSignature,
81
82    #[error("invalid fractal heap signature")]
83    InvalidFractalHeapSignature,
84
85    #[error("invalid object header signature")]
86    InvalidObjectHeaderSignature,
87
88    #[error("invalid B-tree v2 signature: {context}")]
89    InvalidBTreeV2Signature { context: &'static str },
90
91    #[error("invalid fixed array signature: {context}")]
92    InvalidFixedArraySignature { context: &'static str },
93
94    #[error("invalid extensible array signature: {context}")]
95    InvalidExtensibleArraySignature { context: &'static str },
96
97    #[error("checksum mismatch: expected {expected:#010x}, got {actual:#010x}")]
98    ChecksumMismatch { expected: u32, actual: u32 },
99
100    #[error("unexpected end of data at offset {offset} (need {needed} bytes, have {available})")]
101    UnexpectedEof {
102        offset: u64,
103        needed: u64,
104        available: u64,
105    },
106
107    #[error("offset {0:#x} is out of bounds")]
108    OffsetOutOfBounds(u64),
109
110    #[error("group not found: {0}")]
111    GroupNotFound(String),
112
113    #[error("dataset not found: {0}")]
114    DatasetNotFound(String),
115
116    #[error("attribute not found: {0}")]
117    AttributeNotFound(String),
118
119    #[error("type mismatch: expected {expected}, got {actual}")]
120    TypeMismatch { expected: String, actual: String },
121
122    #[error("unsupported filter: {0}")]
123    UnsupportedFilter(String),
124
125    #[error("decompression error: {0}")]
126    DecompressionError(String),
127
128    #[error("filter pipeline error: {0}")]
129    FilterError(String),
130
131    #[error("invalid data: {0}")]
132    InvalidData(String),
133
134    #[error("slice out of bounds: dimension {dim}, index {index}, size {size}")]
135    SliceOutOfBounds { dim: usize, index: u64, size: u64 },
136
137    #[error("undefined address (0xFFFFFFFFFFFFFFFF)")]
138    UndefinedAddress,
139
140    #[error("{0}")]
141    Other(String),
142
143    #[error("{path}: {source}")]
144    Context { path: String, source: Box<Error> },
145}
146
147impl Error {
148    /// Wrap this error with an object path for additional context.
149    pub fn with_context(self, path: impl Into<String>) -> Self {
150        Error::Context {
151            path: path.into(),
152            source: Box::new(self),
153        }
154    }
155}
156
157pub type Result<T> = std::result::Result<T, Error>;
158
159pub use hdf5_core::ByteOrder;
160
161impl From<hdf5_core::Error> for Error {
162    fn from(err: hdf5_core::Error) -> Self {
163        match err {
164            hdf5_core::Error::InvalidData(message) => Error::InvalidData(message),
165        }
166    }
167}