kacrab_protocol/compression/error.rs
1//! Error types for [`crate::compression`].
2
3use super::Compression;
4
5/// Error from compression dispatch / encode / decode.
6#[derive(Debug, thiserror::Error)]
7#[error("compression failed for codec {codec:?}")]
8#[non_exhaustive]
9pub struct CompressionError {
10 /// Which codec was involved (or attempted).
11 pub codec: Compression,
12 /// What specifically went wrong.
13 #[source]
14 pub kind: CompressionErrorKind,
15}
16
17impl CompressionError {
18 /// Construct a `CompressionError` for a given codec.
19 #[must_use]
20 pub const fn new(codec: Compression, kind: CompressionErrorKind) -> Self {
21 Self { codec, kind }
22 }
23}
24
25/// Specific reason a compression operation failed.
26#[derive(Debug, thiserror::Error)]
27#[non_exhaustive]
28pub enum CompressionErrorKind {
29 /// `attributes` bits 0–2 produced a value not in 0..=4.
30 #[error("unknown compression type: {0}")]
31 UnknownCodec(i16),
32
33 /// Codec is recognised but its Cargo feature is not enabled.
34 #[error("codec not enabled; rebuild with the corresponding feature")]
35 CodecDisabled,
36
37 /// Encoding (compress) failed in the underlying codec.
38 #[error("encode failed: {message}")]
39 EncodeFailed {
40 /// Message from the underlying codec.
41 message: String,
42 },
43
44 /// Decoding (decompress) failed in the underlying codec.
45 #[error("decode failed: {message}")]
46 DecodeFailed {
47 /// Message from the underlying codec.
48 message: String,
49 },
50
51 /// Decompressed output exceeded the safety limit — the payload is treated
52 /// as a decompression bomb rather than allowed to exhaust memory.
53 #[error("decompressed size exceeds the {limit}-byte limit")]
54 DecompressedTooLarge {
55 /// The enforced output limit in bytes.
56 limit: usize,
57 },
58}