rasn/ber/
identifier.rs

1use crate::types::{Class, Tag};
2
3/// A BER Identifier.
4#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
5pub struct Identifier {
6    /// The ASN.1 tag.
7    pub tag: Tag,
8    /// Whether a type is using `constructed` or `primitive` encoding.
9    pub(crate) is_constructed: bool,
10}
11
12impl Identifier {
13    /// Instantiates a new instance of `Identifier` from its components.
14    #[must_use]
15    pub fn new(class: Class, is_constructed: bool, tag: u32) -> Self {
16        Self {
17            tag: Tag::new(class, tag),
18            is_constructed,
19        }
20    }
21
22    /// Instantiates a new instance of `Identifier` from its components.
23    #[must_use]
24    pub fn from_tag(tag: Tag, is_constructed: bool) -> Self {
25        Self {
26            tag,
27            is_constructed,
28        }
29    }
30
31    /// Instantiates a new tag from `self` with `tag` overwritten.
32    #[must_use]
33    pub fn tag(self, tag: u32) -> Self {
34        Self {
35            tag: self.tag.set_value(tag),
36            is_constructed: self.is_constructed,
37        }
38    }
39
40    /// Returns whether the identifier is for a type that is using
41    /// "constructed" encoding.
42    #[must_use]
43    pub fn is_constructed(&self) -> bool {
44        self.is_constructed
45    }
46
47    /// Returns whether the identifier is for a type that is using
48    /// "primitive" encoding.
49    #[must_use]
50    pub fn is_primitive(&self) -> bool {
51        !self.is_constructed()
52    }
53}
54
55impl core::ops::Deref for Identifier {
56    type Target = Tag;
57
58    fn deref(&self) -> &Self::Target {
59        &self.tag
60    }
61}