1#[derive(Debug)]
5pub enum Hdf5Error {
6 Io(std::io::Error),
8 Format(crate::format::FormatError),
10 IoLayer(crate::io::IoError),
12 NotFound(String),
16 InvalidState(String),
18 TypeMismatch(String),
20 Unsupported(String),
25 DanglingLink { link: String, target: String },
30 #[cfg(feature = "mmap")]
33 NotViewable(crate::mapped::ViewRefusal),
34 ExternalFileNotFound {
37 link: String,
38 file: String,
39 searched: Vec<String>,
40 },
41}
42
43impl From<std::io::Error> for Hdf5Error {
44 fn from(e: std::io::Error) -> Self {
45 Self::Io(e)
46 }
47}
48
49impl From<crate::format::FormatError> for Hdf5Error {
50 fn from(e: crate::format::FormatError) -> Self {
51 Self::Format(e)
52 }
53}
54
55impl From<crate::io::IoError> for Hdf5Error {
56 fn from(e: crate::io::IoError) -> Self {
57 match e {
63 crate::io::IoError::NotFound(s) => Self::NotFound(s),
64 crate::io::IoError::Unsupported(s) => Self::Unsupported(s),
65 crate::io::IoError::DanglingLink { link, target } => {
66 Self::DanglingLink { link, target }
67 }
68 crate::io::IoError::ExternalFileNotFound {
69 link,
70 file,
71 searched,
72 } => Self::ExternalFileNotFound {
73 link,
74 file,
75 searched,
76 },
77 other => Self::IoLayer(other),
78 }
79 }
80}
81
82impl std::fmt::Display for Hdf5Error {
83 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84 match self {
85 Self::Io(e) => write!(f, "I/O error: {}", e),
86 Self::Format(e) => write!(f, "format error: {}", e),
87 Self::IoLayer(e) => write!(f, "hdf5-io error: {}", e),
88 Self::NotFound(s) => write!(f, "dataset '{}' not found", s),
89 Self::InvalidState(s) => write!(f, "invalid state: {}", s),
90 Self::TypeMismatch(s) => write!(f, "type mismatch: {}", s),
91 Self::Unsupported(s) => write!(f, "unsupported: {}", s),
92 Self::DanglingLink { link, target } => write!(
93 f,
94 "link '{}' points to '{}', which does not exist",
95 link, target
96 ),
97 #[cfg(feature = "mmap")]
98 Self::NotViewable(reason) => {
99 write!(f, "the dataset cannot be viewed in place: {reason}")
100 }
101 Self::ExternalFileNotFound {
102 link,
103 file,
104 searched,
105 } => write!(
106 f,
107 "external link '{}' names the file '{}', which could not be opened (tried: {})",
108 link,
109 file,
110 searched.join(", ")
111 ),
112 }
113 }
114}
115
116impl std::error::Error for Hdf5Error {}
117
118pub type Result<T> = std::result::Result<T, Hdf5Error>;
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124
125 #[test]
126 fn display_not_found() {
127 let err = Hdf5Error::NotFound("my_dataset".into());
128 assert!(format!("{}", err).contains("my_dataset"));
129 }
130
131 #[test]
132 fn display_invalid_state() {
133 let err = Hdf5Error::InvalidState("file already closed".into());
134 assert!(format!("{}", err).contains("file already closed"));
135 }
136
137 #[test]
138 fn display_type_mismatch() {
139 let err = Hdf5Error::TypeMismatch("expected f64, got u8".into());
140 assert!(format!("{}", err).contains("expected f64, got u8"));
141 }
142
143 #[test]
144 fn from_io_error() {
145 let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "gone");
146 let err: Hdf5Error = io_err.into();
147 match err {
148 Hdf5Error::Io(_) => {}
149 other => panic!("expected Io variant, got: {:?}", other),
150 }
151 }
152}