Skip to main content

hdf5_reader/
error.rs

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