1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use std::{error, fmt};

#[derive(Debug, Eq, PartialEq)]
pub struct TryFromByteError(u8);

impl fmt::Display for TryFromByteError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "invalid rANS order: expected 0 or 1, got {}", self.0)
    }
}

impl error::Error for TryFromByteError {}

/// The number of bytes of context used to compute frequencies.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Order {
    /// Order-0.
    Zero,
    /// Order-1.
    One,
}

impl TryFrom<u8> for Order {
    type Error = TryFromByteError;
    fn try_from(b: u8) -> Result<Self, Self::Error> {
        match b {
            0 => Ok(Self::Zero),
            1 => Ok(Self::One),
            _ => Err(TryFromByteError(b)),
        }
    }
}

impl From<Order> for u8 {
    fn from(order: Order) -> Self {
        order as Self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_try_from_u8_for_order() {
        assert_eq!(Order::try_from(0), Ok(Order::Zero));
        assert_eq!(Order::try_from(1), Ok(Order::One));
        assert_eq!(Order::try_from(2), Err(TryFromByteError(2)));
    }

    #[test]
    fn test_from_order_for_u8() {
        assert_eq!(u8::from(Order::Zero), 0);
        assert_eq!(u8::from(Order::One), 1);
    }
}