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 mod checksum;
9pub mod chunk_index;
10pub mod global_heap;
11pub mod local_heap;
12pub mod messages;
13pub mod object_header;
14pub mod superblock;
15pub mod symbol_table;
16pub mod szip;
17
18/// Format context carrying file-level encoding parameters
19#[derive(Debug, Clone, Copy)]
20pub struct FormatContext {
21    pub sizeof_addr: u8,
22    pub sizeof_size: u8,
23}
24
25impl FormatContext {
26    pub fn default_v3() -> Self {
27        Self {
28            sizeof_addr: 8,
29            sizeof_size: 8,
30        }
31    }
32}
33
34/// UNDEF address constant
35pub const UNDEF_ADDR: u64 = u64::MAX;
36
37/// Encode/decode error
38#[derive(Debug)]
39pub enum FormatError {
40    InvalidSignature,
41    InvalidVersion(u8),
42    BufferTooShort { needed: usize, available: usize },
43    ChecksumMismatch { expected: u32, computed: u32 },
44    UnsupportedFeature(String),
45    InvalidData(String),
46}
47
48impl std::fmt::Display for FormatError {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        match self {
51            Self::InvalidSignature => write!(f, "invalid HDF5 signature"),
52            Self::InvalidVersion(v) => write!(f, "unsupported version: {}", v),
53            Self::BufferTooShort { needed, available } => {
54                write!(
55                    f,
56                    "buffer too short: need {} bytes, have {}",
57                    needed, available
58                )
59            }
60            Self::ChecksumMismatch { expected, computed } => {
61                write!(
62                    f,
63                    "checksum mismatch: expected 0x{:08x}, computed 0x{:08x}",
64                    expected, computed
65                )
66            }
67            Self::UnsupportedFeature(s) => write!(f, "unsupported feature: {}", s),
68            Self::InvalidData(s) => write!(f, "invalid data: {}", s),
69        }
70    }
71}
72
73impl std::error::Error for FormatError {}
74
75pub type FormatResult<T> = Result<T, FormatError>;