Skip to main content

msrtc_rans_core/
error.rs

1// Licensed under the MIT license.
2// Author: Riaan de Beer - github.com/infinityabundance - rdebeer.infinityabundance@gmail.com
3
4//! Error types for msrtc-rans-core.
5
6use core::fmt;
7
8/// Errors that can occur during raw rANS operations.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum RawRansError {
11    /// `scale_bits` is out of the valid range for the variant.
12    ///
13    /// For RansByte: valid range is `[2, 30]`.
14    /// For Rans64: the defined safe range is `[2, 31]`. `scale_bits == 32` causes
15    /// undefined-shift behavior in the C++ oracle and is rejected by this implementation.
16    InvalidScaleBits {
17        /// The provided scale_bits value.
18        provided: u32,
19        /// The maximum safe value for this variant.
20        max_safe: u32,
21    },
22    /// Frequency or start values are out of range.
23    InvalidParameters,
24}
25
26impl fmt::Display for RawRansError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            RawRansError::InvalidScaleBits { provided, max_safe } => {
30                write!(
31                    f,
32                    "invalid scale_bits {}: must be in [2, {}]",
33                    provided, max_safe
34                )
35            }
36            RawRansError::InvalidParameters => {
37                write!(f, "invalid rANS parameters")
38            }
39        }
40    }
41}
42
43#[cfg(feature = "std")]
44impl std::error::Error for RawRansError {}