Skip to main content

asn1_rs/
tag.rs

1use crate::{Error, Result};
2#[cfg(not(feature = "std"))]
3use alloc::string::ToString;
4use rusticata_macros::newtype_enum;
5
6/// BER/DER Tag as defined in X.680 section 8.4
7///
8/// X.690 doesn't specify the maximum tag size so we're assuming that people
9/// aren't going to need anything more than a u32.
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11pub struct Tag(pub u32);
12
13newtype_enum! {
14impl display Tag {
15    EndOfContent = 0,
16    Boolean = 1,
17    Integer = 2,
18    BitString = 3,
19    OctetString = 4,
20    Null = 5,
21    Oid = 6,
22    ObjectDescriptor = 7,
23    External = 8,
24    RealType = 9,
25    Enumerated = 10,
26    EmbeddedPdv = 11,
27    Utf8String = 12,
28    RelativeOid = 13,
29
30    Sequence = 16,
31    Set = 17,
32    NumericString = 18,
33    PrintableString = 19,
34    TeletexString = 20,
35    VideotexString = 21,
36
37    Ia5String = 22,
38    UtcTime = 23,
39    GeneralizedTime = 24,
40
41    GraphicString = 25,
42    VisibleString = 26,
43    GeneralString = 27,
44
45    UniversalString = 28,
46    CharacterString = 29,
47    BmpString = 30,
48}
49}
50
51impl Tag {
52    #[allow(non_upper_case_globals)]
53    pub const T61String: Tag = Self::TeletexString;
54
55    pub const fn assert_eq(&self, tag: Tag) -> Result<()> {
56        if self.0 == tag.0 {
57            Ok(())
58        } else {
59            Err(Error::UnexpectedTag {
60                expected: Some(tag),
61                actual: *self,
62            })
63        }
64    }
65
66    pub fn invalid_value(&self, msg: &str) -> Error {
67        Error::InvalidValue {
68            tag: *self,
69            msg: msg.to_string(),
70        }
71    }
72}
73
74impl From<u32> for Tag {
75    fn from(v: u32) -> Self {
76        Tag(v)
77    }
78}