asn1_rs/
tag.rs

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