Skip to main content

rustyhdf5_netcdf4/
error.rs

1//! Error types for the NetCDF-4 reader.
2
3use std::fmt;
4
5/// Errors that can occur when reading NetCDF-4 files.
6#[derive(Debug)]
7pub enum Error {
8    /// I/O error from the filesystem.
9    Io(std::io::Error),
10    /// Low-level HDF5 format error.
11    Hdf5(rustyhdf5::Error),
12    /// The file is not a valid NetCDF-4 file (missing _NCProperties or conventions).
13    NotNetCDF4(String),
14    /// A required dimension was not found.
15    DimensionNotFound(String),
16    /// A required variable was not found.
17    VariableNotFound(String),
18    /// A required group was not found.
19    GroupNotFound(String),
20    /// Data type conversion error.
21    TypeError(String),
22}
23
24impl fmt::Display for Error {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            Error::Io(e) => write!(f, "I/O error: {e}"),
28            Error::Hdf5(e) => write!(f, "HDF5 error: {e}"),
29            Error::NotNetCDF4(msg) => write!(f, "not a NetCDF-4 file: {msg}"),
30            Error::DimensionNotFound(name) => write!(f, "dimension not found: {name}"),
31            Error::VariableNotFound(name) => write!(f, "variable not found: {name}"),
32            Error::GroupNotFound(name) => write!(f, "group not found: {name}"),
33            Error::TypeError(msg) => write!(f, "type error: {msg}"),
34        }
35    }
36}
37
38impl std::error::Error for Error {
39    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
40        match self {
41            Error::Io(e) => Some(e),
42            Error::Hdf5(e) => Some(e),
43            _ => None,
44        }
45    }
46}
47
48impl From<std::io::Error> for Error {
49    fn from(e: std::io::Error) -> Self {
50        Error::Io(e)
51    }
52}
53
54impl From<rustyhdf5::Error> for Error {
55    fn from(e: rustyhdf5::Error) -> Self {
56        Error::Hdf5(e)
57    }
58}