Skip to main content

rust_hdf5/format/
mod.rs

1//! Pure Rust HDF5 on-disk format codec.
2//!
3//! This crate handles encoding and decoding of HDF5 binary structures
4//! (superblock, object headers, messages, chunk indices) without performing
5//! any file I/O. It is used by `hdf5-io` and `hdf5` crates.
6
7pub mod btree_v1;
8pub(crate) mod bytes;
9pub mod checksum;
10pub mod chunk_index;
11pub mod fractal_heap;
12pub mod global_heap;
13pub mod local_heap;
14pub mod messages;
15pub mod nbit_scaleoffset;
16pub mod object_header;
17pub mod superblock;
18pub mod symbol_table;
19pub mod szip;
20
21/// Format context carrying file-level encoding parameters
22#[derive(Debug, Clone, Copy)]
23pub struct FormatContext {
24    pub sizeof_addr: u8,
25    pub sizeof_size: u8,
26}
27
28impl FormatContext {
29    pub fn default_v3() -> Self {
30        Self {
31            sizeof_addr: 8,
32            sizeof_size: 8,
33        }
34    }
35}
36
37/// UNDEF address constant
38pub const UNDEF_ADDR: u64 = u64::MAX;
39
40/// Encode/decode error
41#[derive(Debug)]
42pub enum FormatError {
43    InvalidSignature,
44    InvalidVersion(u8),
45    BufferTooShort { needed: usize, available: usize },
46    ChecksumMismatch { expected: u32, computed: u32 },
47    UnsupportedFeature(String),
48    InvalidData(String),
49}
50
51impl std::fmt::Display for FormatError {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        match self {
54            Self::InvalidSignature => write!(f, "invalid HDF5 signature"),
55            Self::InvalidVersion(v) => write!(f, "unsupported version: {}", v),
56            Self::BufferTooShort { needed, available } => {
57                write!(
58                    f,
59                    "buffer too short: need {} bytes, have {}",
60                    needed, available
61                )
62            }
63            Self::ChecksumMismatch { expected, computed } => {
64                write!(
65                    f,
66                    "checksum mismatch: expected 0x{:08x}, computed 0x{:08x}",
67                    expected, computed
68                )
69            }
70            Self::UnsupportedFeature(s) => write!(f, "unsupported feature: {}", s),
71            Self::InvalidData(s) => write!(f, "invalid data: {}", s),
72        }
73    }
74}
75
76impl std::error::Error for FormatError {}
77
78pub type FormatResult<T> = Result<T, FormatError>;