Skip to main content

compression/
error.rs

1use crate::Algorithm;
2use std::{error::Error, fmt};
3
4/// Wraps crate results returned by Compression and AppleArchive helpers.
5pub type Result<T> = std::result::Result<T, CompressionError>;
6
7/// Wraps failures returned by Compression and AppleArchive helpers.
8#[derive(Clone, Debug, Eq, PartialEq)]
9pub enum CompressionError {
10    /// Wraps the `BufferOperationFailed` failure case reported by Compression or AppleArchive helpers.
11    BufferOperationFailed {
12        /// Wraps the failing Compression or AppleArchive entry-point name.
13        operation: &'static str,
14        /// Wraps the algorithm associated with the failing Compression call.
15        algorithm: Algorithm,
16        /// Wraps the input length passed to the failing buffer operation.
17        input_len: usize,
18        /// Wraps the destination capacity passed to the failing buffer operation.
19        output_capacity: usize,
20    },
21    /// Wraps the `StreamInitFailed` failure case reported by Compression or AppleArchive helpers.
22    StreamInitFailed {
23        /// Wraps the failing Compression or AppleArchive entry-point name.
24        operation: &'static str,
25        /// Wraps the algorithm associated with the failing Compression call.
26        algorithm: Algorithm,
27    },
28    /// Wraps the `StreamProcessFailed` failure case reported by Compression or AppleArchive helpers.
29    StreamProcessFailed {
30        /// Wraps the failing Compression or AppleArchive entry-point name.
31        operation: &'static str,
32        /// Wraps the algorithm associated with the failing Compression call.
33        algorithm: Algorithm,
34        /// Wraps the `compression_stream_process` status code.
35        status: i32,
36    },
37    /// Wraps the `StreamStalled` failure case reported by Compression or AppleArchive helpers.
38    StreamStalled {
39        /// Wraps the failing Compression or AppleArchive entry-point name.
40        operation: &'static str,
41        /// Wraps the algorithm associated with the failing Compression call.
42        algorithm: Algorithm,
43    },
44    /// Wraps the `StreamFinished` failure case reported by Compression or AppleArchive helpers.
45    StreamFinished {
46        /// Wraps the failing Compression or AppleArchive entry-point name.
47        operation: &'static str,
48        /// Wraps the algorithm associated with the failing Compression call.
49        algorithm: Algorithm,
50    },
51    /// Wraps the `OperationFailed` failure case reported by Compression or AppleArchive helpers.
52    OperationFailed {
53        /// Wraps the failing Compression or AppleArchive entry-point name.
54        operation: &'static str,
55        /// Wraps the failing Compression or AppleArchive status code.
56        code: i32,
57    },
58    /// Wraps the `NullHandle` failure case reported by Compression or AppleArchive helpers.
59    NullHandle {
60        /// Wraps the failing Compression or AppleArchive entry-point name.
61        operation: &'static str,
62    },
63    /// Wraps the `Closed` failure case reported by Compression or AppleArchive helpers.
64    Closed {
65        /// Wraps the closed resource name reported by AppleArchive helpers.
66        resource: &'static str,
67    },
68    /// Wraps the `InvalidFieldKey` failure case reported by Compression or AppleArchive helpers.
69    InvalidFieldKey {
70        /// Wraps the invalid AppleArchive field key.
71        key: String,
72    },
73    /// Wraps the `InvalidHashLength` failure case reported by Compression or AppleArchive helpers.
74    InvalidHashLength {
75        /// Wraps the expected hash length.
76        expected: usize,
77        /// Wraps the actual hash length.
78        actual: usize,
79    },
80    /// Wraps the `NulByte` failure case reported by Compression or AppleArchive helpers.
81    NulByte {
82        /// Wraps the argument name that contained an interior NUL byte.
83        argument: &'static str,
84    },
85    /// Wraps the `Utf8Error` failure case reported by Compression or AppleArchive helpers.
86    Utf8Error {
87        /// Wraps the failing Compression or AppleArchive entry-point name.
88        operation: &'static str,
89    },
90}
91
92impl fmt::Display for CompressionError {
93    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94        match self {
95            Self::BufferOperationFailed {
96                operation,
97                algorithm,
98                input_len,
99                output_capacity,
100            } => write!(
101                f,
102                "{operation} failed for {algorithm:?} (input_len={input_len}, output_capacity={output_capacity})"
103            ),
104            Self::StreamInitFailed {
105                operation,
106                algorithm,
107            } => write!(f, "{operation} failed for {algorithm:?}"),
108            Self::StreamProcessFailed {
109                operation,
110                algorithm,
111                status,
112            } => write!(
113                f,
114                "{operation} failed for {algorithm:?} (status={status})"
115            ),
116            Self::StreamStalled {
117                operation,
118                algorithm,
119            } => write!(
120                f,
121                "{operation} made no progress for {algorithm:?}; the input may be truncated or corrupt"
122            ),
123            Self::StreamFinished {
124                operation,
125                algorithm,
126            } => write!(f, "{operation} called after the {algorithm:?} stream already finished"),
127            Self::OperationFailed { operation, code } => {
128                if *code < 0 {
129                    let os_code = -*code;
130                    write!(
131                        f,
132                        "{operation} failed with {code} ({})",
133                        std::io::Error::from_raw_os_error(os_code)
134                    )
135                } else {
136                    write!(f, "{operation} failed with {code}")
137                }
138            }
139            Self::NullHandle { operation } => write!(f, "{operation} returned a null handle"),
140            Self::Closed { resource } => write!(f, "{resource} is already closed"),
141            Self::InvalidFieldKey { key } => write!(f, "invalid AppleArchive field key: {key:?}"),
142            Self::InvalidHashLength { expected, actual } => {
143                write!(f, "invalid hash length: expected {expected} bytes, got {actual}")
144            }
145            Self::NulByte { argument } => write!(f, "{argument} contains an interior NUL byte"),
146            Self::Utf8Error { operation } => write!(f, "{operation} returned data that is not valid UTF-8"),
147        }
148    }
149}
150
151impl Error for CompressionError {}