Skip to main content

array_format/
error.rs

1//! Error types for the array-format crate.
2
3use crate::dtype::DType;
4
5/// The error type for array-format operations.
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8    /// I/O error from the underlying storage backend.
9    #[error("I/O error: {0}")]
10    Io(#[from] std::io::Error),
11
12    /// Footer serialization or deserialization failed.
13    #[error("serialization error: {0}")]
14    Serialization(String),
15
16    /// The file footer is invalid or corrupt.
17    #[error("invalid footer: {0}")]
18    InvalidFooter(String),
19
20    /// The requested array was not found.
21    #[error("array not found: {name}")]
22    ArrayNotFound {
23        /// Name of the array that was not found.
24        name: String,
25    },
26
27    /// A referenced block id is out of range.
28    #[error("block out of range: {block_id}")]
29    BlockOutOfRange {
30        /// The invalid block id.
31        block_id: u32,
32    },
33
34    /// Compression or decompression failed.
35    #[error("codec error: {0}")]
36    Codec(String),
37
38    /// Storage backend error.
39    #[error("storage error: {0}")]
40    Storage(String),
41
42    /// The array already exists.
43    #[error("array already exists: {name}")]
44    ArrayAlreadyExists {
45        /// Name of the duplicate array.
46        name: String,
47    },
48
49    /// The dtype of the data does not match the dtype of the array.
50    #[error("dtype mismatch: expected {expected:?}, got {actual:?}")]
51    DTypeMismatch {
52        /// The dtype declared in the array metadata.
53        expected: DType,
54        /// The dtype of the data being read or written.
55        actual: DType,
56    },
57}
58
59/// A specialized [`Result`] type for array-format operations.
60pub type Result<T> = std::result::Result<T, Error>;
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn error_display() {
68        let e = Error::ArrayNotFound {
69            name: "temp".into(),
70        };
71        assert_eq!(e.to_string(), "array not found: temp");
72    }
73
74    #[test]
75    fn io_error_conversion() {
76        let io_err = std::io::Error::new(std::io::ErrorKind::NotFound, "gone");
77        let e: Error = io_err.into();
78        assert!(matches!(e, Error::Io(_)));
79    }
80}