1#[derive(Debug)]
3pub enum FpZipError {
4 InvalidMagic(u32),
6 UnsupportedVersion(u16),
8 InvalidDataType(u8),
10 DimensionMismatch {
12 actual: usize,
13 expected: u64,
14 nx: u32,
15 ny: u32,
16 nz: u32,
17 nf: u32,
18 },
19 TypeMismatch {
21 expected: crate::header::FpZipType,
22 actual: crate::header::FpZipType,
23 },
24 BufferTooSmall { needed: usize, available: usize },
26 UnexpectedEof,
28 #[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
87pub type Result<T> = core::result::Result<T, FpZipError>;