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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use core::convert::TryFrom;
use core::fmt;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct BerClassFromIntError(pub(crate) ());

/// BER Object class of tag
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[repr(u8)]
pub enum Class {
    /// `Universal` class of tags (`0b00`)
    Universal = 0b00,
    /// `Application` class of tags (`0b01`)
    Application = 0b01,
    /// `Context-Specific` class of tags (`0b10`)
    ContextSpecific = 0b10,
    /// `Private` class of tags (`0b11`)
    Private = 0b11,
}

impl Class {
    /// `Universal` class of tags (`0b00`)
    pub const UNIVERSAL: u8 = 0b00;
    /// `Application` class of tags (`0b01`)
    pub const APPLICATION: u8 = 0b01;
    /// `Context-Specific` class of tags (`0b10`)
    pub const CONTEXT_SPECIFIC: u8 = 0b10;
    /// `Private` class of tags (`0b11`)
    pub const PRIVATE: u8 = 0b11;

    pub const fn assert_eq(&self, class: Class) -> Result<(), crate::error::Error> {
        if *self as u8 == class as u8 {
            Ok(())
        } else {
            Err(crate::error::Error::UnexpectedClass {
                expected: Some(class),
                actual: *self,
            })
        }
    }
}

impl fmt::Display for Class {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            Class::Universal => "UNIVERSAL",
            Class::Application => "APPLICATION",
            Class::ContextSpecific => "CONTEXT-SPECIFIC",
            Class::Private => "PRIVATE",
        };
        write!(f, "{}", s)
    }
}

impl TryFrom<u8> for Class {
    type Error = BerClassFromIntError;

    #[inline]
    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            0b00 => Ok(Class::Universal),
            0b01 => Ok(Class::Application),
            0b10 => Ok(Class::ContextSpecific),
            0b11 => Ok(Class::Private),
            _ => Err(BerClassFromIntError(())),
        }
    }
}

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

    #[test]
    fn methods_class() {
        let c = Class::Universal;
        assert!(c.assert_eq(Class::Universal).is_ok());
        assert!(c.assert_eq(Class::Private).is_err());

        assert_eq!(Class::Universal.to_string().as_str(), "UNIVERSAL");
        assert_eq!(Class::Application.to_string().as_str(), "APPLICATION");
        assert_eq!(
            Class::ContextSpecific.to_string().as_str(),
            "CONTEXT-SPECIFIC"
        );
        assert_eq!(Class::Private.to_string().as_str(), "PRIVATE");

        assert!(Class::try_from(0b00).is_ok());
        assert!(Class::try_from(0b01).is_ok());
        assert!(Class::try_from(0b10).is_ok());
        assert!(Class::try_from(0b11).is_ok());
        assert!(Class::try_from(4).is_err());
    }
}