Skip to main content

tufazip_arith/
error.rs

1use thiserror::Error;
2
3/// Errors returned by arithmetic coding and bit stream operations.
4#[derive(Debug, Error)]
5pub enum ArithError {
6    /// State bit width is outside the supported `[1, 62]` range.
7    #[error("arithmetic coder state bit width {0} must be in 1..=62")]
8    InvalidStateBits(u32),
9
10    /// Attempted to write a bit value other than `0` or `1`.
11    #[error("bit value must be 0 or 1, got {0}")]
12    InvalidBit(u8),
13
14    /// Frequency table is empty.
15    #[error("cumulative frequency table cannot be empty")]
16    EmptyFreqTable,
17
18    /// Symbol index is out of bounds for the frequency table.
19    #[error("symbol index {symbol} out of bounds for table of length {len}")]
20    InvalidSymbol {
21        /// Requested symbol index.
22        symbol: usize,
23        /// Number of symbols in the frequency table.
24        len: usize,
25    },
26
27    /// Symbol has zero probability mass in the cumulative frequency table.
28    #[error("symbol {0} has zero frequency")]
29    ZeroFrequencySymbol(usize),
30
31    /// Total frequency count exceeds the coder's maximum allowed total.
32    #[error("frequency total {total} exceeds maximum allowed {max}")]
33    TotalTooLarge {
34        /// Observed cumulative frequency total.
35        total: u64,
36        /// Coder maximum allowed total (`minimum_range`).
37        max: u64,
38    },
39
40    /// Restored checkpoint state does not match the coder's configured bit width.
41    #[error("arithmetic coder state width mismatch: expected {expected}, found {found}")]
42    StateWidthMismatch {
43        /// Configured state width.
44        expected: u32,
45        /// Restored state width.
46        found: u32,
47    },
48
49    /// Restored bit stream state contains invalid bit counts.
50    #[error("invalid bit stream state: {0}")]
51    InvalidStreamState(String),
52
53    /// Unexpected end of input bit stream when a non-EOF bit was requested.
54    #[error("unexpected end of bit stream")]
55    UnexpectedEof,
56
57    /// Underlying I/O error.
58    #[error(transparent)]
59    Io(#[from] std::io::Error),
60}