/rust/registry/src/index.crates.io-6f17d22bba15001f/asn1-rs-0.6.2/src/class.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use core::convert::TryFrom; |
2 | | use core::fmt; |
3 | | |
4 | | #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
5 | | pub struct BerClassFromIntError(pub(crate) ()); |
6 | | |
7 | | /// BER Object class of tag |
8 | | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
9 | | #[repr(u8)] |
10 | | pub enum Class { |
11 | | /// `Universal` class of tags (`0b00`) |
12 | | Universal = 0b00, |
13 | | /// `Application` class of tags (`0b01`) |
14 | | Application = 0b01, |
15 | | /// `Context-Specific` class of tags (`0b10`) |
16 | | ContextSpecific = 0b10, |
17 | | /// `Private` class of tags (`0b11`) |
18 | | Private = 0b11, |
19 | | } |
20 | | |
21 | | impl Class { |
22 | | /// `Universal` class of tags (`0b00`) |
23 | | pub const UNIVERSAL: u8 = 0b00; |
24 | | /// `Application` class of tags (`0b01`) |
25 | | pub const APPLICATION: u8 = 0b01; |
26 | | /// `Context-Specific` class of tags (`0b10`) |
27 | | pub const CONTEXT_SPECIFIC: u8 = 0b10; |
28 | | /// `Private` class of tags (`0b11`) |
29 | | pub const PRIVATE: u8 = 0b11; |
30 | | |
31 | 1.13M | pub const fn assert_eq(&self, class: Class) -> Result<(), crate::error::Error> { |
32 | 1.13M | if *self as u8 == class as u8 { |
33 | 1.12M | Ok(()) |
34 | | } else { |
35 | 6.81k | Err(crate::error::Error::UnexpectedClass { |
36 | 6.81k | expected: Some(class), |
37 | 6.81k | actual: *self, |
38 | 6.81k | }) |
39 | | } |
40 | 1.13M | } |
41 | | } |
42 | | |
43 | | impl fmt::Display for Class { |
44 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
45 | 0 | let s = match self { |
46 | 0 | Class::Universal => "UNIVERSAL", |
47 | 0 | Class::Application => "APPLICATION", |
48 | 0 | Class::ContextSpecific => "CONTEXT-SPECIFIC", |
49 | 0 | Class::Private => "PRIVATE", |
50 | | }; |
51 | 0 | write!(f, "{}", s) |
52 | 0 | } |
53 | | } |
54 | | |
55 | | impl TryFrom<u8> for Class { |
56 | | type Error = BerClassFromIntError; |
57 | | |
58 | | #[inline] |
59 | 25.0M | fn try_from(value: u8) -> Result<Self, Self::Error> { |
60 | 25.0M | match value { |
61 | 0 | 0b00 => Ok(Class::Universal), |
62 | 0 | 0b01 => Ok(Class::Application), |
63 | 2.24M | 0b10 => Ok(Class::ContextSpecific), |
64 | 0 | 0b11 => Ok(Class::Private), |
65 | 0 | _ => Err(BerClassFromIntError(())), |
66 | | } |
67 | 2.24M | } |
68 | | } |
69 | | |
70 | | #[cfg(test)] |
71 | | mod tests { |
72 | | use super::*; |
73 | | |
74 | | #[test] |
75 | | fn methods_class() { |
76 | | let c = Class::Universal; |
77 | | assert!(c.assert_eq(Class::Universal).is_ok()); |
78 | | assert!(c.assert_eq(Class::Private).is_err()); |
79 | | |
80 | | assert_eq!(Class::Universal.to_string().as_str(), "UNIVERSAL"); |
81 | | assert_eq!(Class::Application.to_string().as_str(), "APPLICATION"); |
82 | | assert_eq!( |
83 | | Class::ContextSpecific.to_string().as_str(), |
84 | | "CONTEXT-SPECIFIC" |
85 | | ); |
86 | | assert_eq!(Class::Private.to_string().as_str(), "PRIVATE"); |
87 | | |
88 | | assert!(Class::try_from(0b00).is_ok()); |
89 | | assert!(Class::try_from(0b01).is_ok()); |
90 | | assert!(Class::try_from(0b10).is_ok()); |
91 | | assert!(Class::try_from(0b11).is_ok()); |
92 | | assert!(Class::try_from(4).is_err()); |
93 | | } |
94 | | } |