Skip to main content

compression/
error.rs

1use crate::Algorithm;
2use std::{error::Error, fmt};
3
4pub type Result<T> = std::result::Result<T, CompressionError>;
5
6#[derive(Clone, Debug, Eq, PartialEq)]
7pub enum CompressionError {
8    BufferOperationFailed {
9        operation: &'static str,
10        algorithm: Algorithm,
11        input_len: usize,
12        output_capacity: usize,
13    },
14    StreamInitFailed {
15        operation: &'static str,
16        algorithm: Algorithm,
17    },
18    StreamProcessFailed {
19        operation: &'static str,
20        algorithm: Algorithm,
21    },
22    StreamStalled {
23        operation: &'static str,
24        algorithm: Algorithm,
25    },
26    StreamFinished {
27        operation: &'static str,
28        algorithm: Algorithm,
29    },
30}
31
32impl fmt::Display for CompressionError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::BufferOperationFailed {
36                operation,
37                algorithm,
38                input_len,
39                output_capacity,
40            } => write!(
41                f,
42                "{operation} failed for {algorithm:?} (input_len={input_len}, output_capacity={output_capacity})"
43            ),
44            Self::StreamInitFailed {
45                operation,
46                algorithm,
47            }
48            | Self::StreamProcessFailed {
49                operation,
50                algorithm,
51            } => write!(f, "{operation} failed for {algorithm:?}"),
52            Self::StreamStalled {
53                operation,
54                algorithm,
55            } => write!(
56                f,
57                "{operation} made no progress for {algorithm:?}; the input may be truncated or corrupt"
58            ),
59            Self::StreamFinished {
60                operation,
61                algorithm,
62            } => write!(f, "{operation} called after the {algorithm:?} stream already finished"),
63        }
64    }
65}
66
67impl Error for CompressionError {}