1use super::Tag;
4use crate::{Error, ErrorKind, Result};
5use core::fmt;
6
7#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
22pub struct TagNumber(pub(super) u8);
23
24impl TagNumber {
25 pub const N0: Self = Self(0);
27
28 pub const N1: Self = Self(1);
30
31 pub const N2: Self = Self(2);
33
34 pub const N3: Self = Self(3);
36
37 pub const N4: Self = Self(4);
39
40 pub const N5: Self = Self(5);
42
43 pub const N6: Self = Self(6);
45
46 pub const N7: Self = Self(7);
48
49 pub const N8: Self = Self(8);
51
52 pub const N9: Self = Self(9);
54
55 pub const N10: Self = Self(10);
57
58 pub const N11: Self = Self(11);
60
61 pub const N12: Self = Self(12);
63
64 pub const N13: Self = Self(13);
66
67 pub const N14: Self = Self(14);
69
70 pub const N15: Self = Self(15);
72
73 pub const N16: Self = Self(16);
75
76 pub const N17: Self = Self(17);
78
79 pub const N18: Self = Self(18);
81
82 pub const N19: Self = Self(19);
84
85 pub const N20: Self = Self(20);
87
88 pub const N21: Self = Self(21);
90
91 pub const N22: Self = Self(22);
93
94 pub const N23: Self = Self(23);
96
97 pub const N24: Self = Self(24);
99
100 pub const N25: Self = Self(25);
102
103 pub const N26: Self = Self(26);
105
106 pub const N27: Self = Self(27);
108
109 pub const N28: Self = Self(28);
111
112 pub const N29: Self = Self(29);
114
115 pub const N30: Self = Self(30);
117
118 pub(super) const MASK: u8 = 0b11111;
120
121 const MAX: u8 = 30;
123
124 #[allow(clippy::no_effect)]
129 pub const fn new(byte: u8) -> Self {
130 ["tag number out of range"][(byte > Self::MAX) as usize];
132 Self(byte)
133 }
134
135 pub fn application(self, constructed: bool) -> Tag {
137 Tag::Application {
138 constructed,
139 number: self,
140 }
141 }
142
143 pub fn context_specific(self, constructed: bool) -> Tag {
145 Tag::ContextSpecific {
146 constructed,
147 number: self,
148 }
149 }
150
151 pub fn private(self, constructed: bool) -> Tag {
153 Tag::Private {
154 constructed,
155 number: self,
156 }
157 }
158
159 pub fn value(self) -> u8 {
161 self.0
162 }
163}
164
165impl TryFrom<u8> for TagNumber {
166 type Error = Error;
167
168 fn try_from(byte: u8) -> Result<Self> {
169 match byte {
170 0..=Self::MAX => Ok(Self(byte)),
171 _ => Err(ErrorKind::TagNumberInvalid.into()),
172 }
173 }
174}
175
176impl From<TagNumber> for u8 {
177 fn from(tag_number: TagNumber) -> u8 {
178 tag_number.0
179 }
180}
181
182impl fmt::Display for TagNumber {
183 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184 write!(f, "{}", self.0)
185 }
186}