Skip to main content

fpzip_rs/
error.rs

1/// Errors that can occur during FPZip compression or decompression.
2#[derive(Debug)]
3pub enum FpZipError {
4    /// The compressed data does not start with the fpzip magic number.
5    InvalidMagic(u32),
6    /// The format version in the header is not supported.
7    UnsupportedVersion(u16),
8    /// The data type byte in the header is not 0 (float) or 1 (double).
9    InvalidDataType(u8),
10    /// The input data length does not match `nx * ny * nz * nf`.
11    DimensionMismatch {
12        actual: usize,
13        expected: u64,
14        nx: u32,
15        ny: u32,
16        nz: u32,
17        nf: u32,
18    },
19    /// Attempted to decompress float data as double, or vice versa.
20    TypeMismatch {
21        expected: crate::header::FpZipType,
22        actual: crate::header::FpZipType,
23    },
24    /// The output buffer is too small for the result.
25    BufferTooSmall { needed: usize, available: usize },
26    /// The compressed data ended unexpectedly.
27    UnexpectedEof,
28    /// An I/O error occurred during stream-based operations.
29    #[cfg(feature = "std")]
30    Io(std::io::Error),
31}
32
33impl core::fmt::Display for FpZipError {
34    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
35        match self {
36            Self::InvalidMagic(m) => {
37                write!(f, "invalid magic number: expected 0x007A7066, got 0x{m:08X}")
38            }
39            Self::UnsupportedVersion(v) => {
40                write!(f, "unsupported version {v}, maximum supported: 1")
41            }
42            Self::InvalidDataType(t) => write!(f, "invalid data type: {t}"),
43            Self::DimensionMismatch {
44                actual,
45                expected,
46                nx,
47                ny,
48                nz,
49                nf,
50            } => write!(
51                f,
52                "dimension mismatch: data length {actual} != {expected} (nx={nx} * ny={ny} * nz={nz} * nf={nf})"
53            ),
54            Self::TypeMismatch { expected, actual } => {
55                write!(f, "type mismatch: expected {expected:?}, got {actual:?}")
56            }
57            Self::BufferTooSmall { needed, available } => {
58                write!(
59                    f,
60                    "buffer too small: need {needed} bytes, got {available}"
61                )
62            }
63            Self::UnexpectedEof => write!(f, "unexpected end of input"),
64            #[cfg(feature = "std")]
65            Self::Io(e) => write!(f, "I/O error: {e}"),
66        }
67    }
68}
69
70#[cfg(feature = "std")]
71impl std::error::Error for FpZipError {
72    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
73        match self {
74            Self::Io(e) => Some(e),
75            _ => None,
76        }
77    }
78}
79
80#[cfg(feature = "std")]
81impl From<std::io::Error> for FpZipError {
82    fn from(e: std::io::Error) -> Self {
83        Self::Io(e)
84    }
85}
86
87/// A specialized `Result` type for fpzip operations.
88pub type Result<T> = core::result::Result<T, FpZipError>;