Skip to main content

alice_zip/
error.rs

1//! Error type for ALICE-Zip
2
3/// Errors returned by the `no_std` primitives (`lz77` / `dictionary`)
4/// The zlib wrappers in [`crate::compression`] return `std::io::Error`
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[non_exhaustive]
7pub enum ZipError {
8    /// A token stream references bytes that do not exist yet
9    InvalidData,
10    /// Generic decompression failure
11    DecompressFailed,
12    /// [`crate::Dictionary`] cannot accept another phrase (capacity `0` or
13    /// `u32` arena offset range exhausted)
14    DictionaryFull,
15    /// A generator was given a parameter outside its domain (non-positive
16    /// `scale`, `octaves == 0`, texture size overflow)
17    InvalidParameter,
18}
19
20impl core::fmt::Display for ZipError {
21    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22        match self {
23            Self::InvalidData => write!(f, "invalid data"),
24            Self::DecompressFailed => write!(f, "decompress failed"),
25            Self::DictionaryFull => write!(f, "dictionary full"),
26            Self::InvalidParameter => write!(f, "invalid generator parameter"),
27        }
28    }
29}
30
31#[cfg(feature = "std")]
32impl std::error::Error for ZipError {}
33
34#[cfg(test)]
35#[allow(
36    clippy::doc_markdown,
37    clippy::unreadable_literal,
38    clippy::cast_possible_truncation,
39    clippy::cast_sign_loss,
40    clippy::cast_precision_loss,
41    clippy::cast_possible_wrap,
42    clippy::uninlined_format_args
43)]
44mod tests {
45    use super::*;
46
47    /// Display出力の確認
48    #[test]
49    fn zip_error_display_invalid_data() {
50        let e = ZipError::InvalidData;
51        let s = alloc::format!("{e}");
52        assert_eq!(s, "invalid data");
53    }
54
55    #[test]
56    fn zip_error_display_decompress_failed() {
57        let e = ZipError::DecompressFailed;
58        let s = alloc::format!("{e}");
59        assert_eq!(s, "decompress failed");
60    }
61
62    #[test]
63    fn zip_error_display_dictionary_full() {
64        let e = ZipError::DictionaryFull;
65        let s = alloc::format!("{e}");
66        assert_eq!(s, "dictionary full");
67    }
68
69    /// Copy
70    #[test]
71    fn zip_error_copy() {
72        let e = ZipError::InvalidData;
73        let e2 = e;
74        assert_eq!(e, e2);
75        assert_eq!(
76            alloc::format!("{}", ZipError::InvalidParameter),
77            "invalid generator parameter"
78        );
79    }
80
81    /// PartialEq
82    #[test]
83    fn zip_error_eq() {
84        assert_eq!(ZipError::InvalidData, ZipError::InvalidData);
85        assert_ne!(ZipError::InvalidData, ZipError::DecompressFailed);
86        assert_ne!(ZipError::DecompressFailed, ZipError::DictionaryFull);
87    }
88
89    /// Debug出力
90    #[test]
91    fn zip_error_debug() {
92        let s = alloc::format!("{:?}", ZipError::InvalidData);
93        assert!(s.contains("InvalidData"));
94    }
95}