2
2
3
3
use core:: fmt;
4
4
5
+ /// `UNIVERSAL`: built-in types whose meaning is the same in all
6
+ /// applications.
7
+ pub const CLASS_UNIVERSAL : u8 = 0b00000000 ;
8
+ /// `APPLICATION`: types whose meaning is specific to an application.
9
+ pub const CLASS_APPLICATION : u8 = 0b01000000 ;
10
+ /// `CONTEXT-SPECIFIC`: types whose meaning is specific to a given
11
+ /// structured type.
12
+ pub const CLASS_CONTEXT_SPECIFIC : u8 = 0b10000000 ;
13
+ /// `PRIVATE`: types whose meaning is specific to a given enterprise.
14
+ pub const CLASS_PRIVATE : u8 = 0b11000000 ;
15
+
5
16
/// Class of an ASN.1 tag.
6
17
#[ derive( Copy , Clone , Debug , Eq , PartialEq , PartialOrd , Ord ) ]
7
18
#[ repr( u8 ) ]
8
19
pub enum Class {
9
20
/// `UNIVERSAL`: built-in types whose meaning is the same in all
10
21
/// applications.
11
- Universal = 0b00000000 ,
22
+ Universal = CLASS_UNIVERSAL ,
12
23
13
24
/// `APPLICATION`: types whose meaning is specific to an application,
14
25
///
15
26
/// Types in two different applications may have the same
16
27
/// application-specific tag and different meanings.
17
- Application = 0b01000000 ,
28
+ Application = CLASS_APPLICATION ,
18
29
19
30
/// `CONTEXT-SPECIFIC`: types whose meaning is specific to a given
20
31
/// structured type.
@@ -23,10 +34,10 @@ pub enum Class {
23
34
/// with the same underlying tag within the context of a given structured
24
35
/// type, and component types in two different structured types may have
25
36
/// the same tag and different meanings.
26
- ContextSpecific = 0b10000000 ,
37
+ ContextSpecific = CLASS_CONTEXT_SPECIFIC ,
27
38
28
39
/// `PRIVATE`: types whose meaning is specific to a given enterprise.
29
- Private = 0b11000000 ,
40
+ Private = CLASS_PRIVATE ,
30
41
}
31
42
32
43
impl fmt:: Display for Class {
@@ -39,3 +50,26 @@ impl fmt::Display for Class {
39
50
} )
40
51
}
41
52
}
53
+
54
+ impl Class {
55
+ /// Returns class as 2 most-significant bits (mask 0b11000000)
56
+ pub const fn bits ( & self ) -> u8 {
57
+ * self as u8
58
+ }
59
+
60
+ /// Returns class extracted from 2 most-significant bits (mask 0b11000000)
61
+ pub const fn from_bits ( bits : u8 ) -> Self {
62
+ match ( bits >> 6 ) & 0b11 {
63
+ 0b00 => Class :: Universal ,
64
+ 0b01 => Class :: Application ,
65
+ 0b10 => Class :: ContextSpecific ,
66
+ 0b11 => Class :: Private ,
67
+ _ => unreachable ! ( ) ,
68
+ }
69
+ }
70
+ }
71
+ impl From < u8 > for Class {
72
+ fn from ( value : u8 ) -> Self {
73
+ Class :: from_bits ( value)
74
+ }
75
+ }
0 commit comments