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    T61String = 20,
35    TeletexString = 20,
36    VideotexString = 21,
37
38    Ia5String = 22,
39    UtcTime = 23,
40    GeneralizedTime = 24,
41
42    GraphicString = 25,
43    VisibleString = 26,
44    GeneralString = 27,
45
46    UniversalString = 28,
47    CharacterString = 29,
48    BmpString = 30,
49}
50}
51
52impl Tag {
53    pub const fn assert_eq(&self, tag: Tag) -> Result<()> {
54        if self.0 == tag.0 {
55            Ok(())
56        } else {
57            Err(Error::UnexpectedTag {
58                expected: Some(tag),
59                actual: *self,
60            })
61        }
62    }
63
64    pub fn invalid_value(&self, msg: &str) -> Error {
65        Error::InvalidValue {
66            tag: *self,
67            msg: msg.to_string(),
68        }
69    }
70}
71
72impl From<u32> for Tag {
73    fn from(v: u32) -> Self {
74        Tag(v)
75    }
76}