der_derive/
tag.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//! Tag-related functionality.

use crate::Asn1Type;
use proc_macro2::TokenStream;
use quote::quote;
use std::{
    fmt::{self, Display},
    str::FromStr,
};

/// Tag "IR" type.
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub(crate) enum Tag {
    /// Universal tags with an associated [`Asn1Type`].
    Universal(Asn1Type),

    /// Context-specific tags with an associated [`TagNumber`].
    ContextSpecific {
        /// Is the inner ASN.1 type constructed?
        constructed: bool,

        /// Context-specific tag number
        number: TagNumber,
    },
}

impl Tag {
    /// Lower this [`Tag`] to a [`TokenStream`].
    pub fn to_tokens(self) -> TokenStream {
        match self {
            Tag::Universal(ty) => ty.tag(),
            Tag::ContextSpecific {
                constructed,
                number,
            } => {
                let constructed = if constructed {
                    quote!(true)
                } else {
                    quote!(false)
                };

                let number = number.to_tokens();

                quote! {
                    ::der::Tag::ContextSpecific {
                        constructed: #constructed,
                        number: #number,
                    }
                }
            }
        }
    }
}

/// Tagging modes: `EXPLICIT` versus `IMPLICIT`.
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, PartialOrd, Ord)]
pub(crate) enum TagMode {
    /// `EXPLICIT` tagging.
    ///
    /// Tag is added in addition to the inner tag of the type.
    #[default]
    Explicit,

    /// `IMPLICIT` tagging.
    ///
    /// Tag replaces the existing tag of the inner type.
    Implicit,
}

impl TagMode {
    /// Lower this [`TagMode`] to a [`TokenStream`] with the `der`
    /// crate's corresponding enum variant for this tag mode.
    pub fn to_tokens(self) -> TokenStream {
        match self {
            TagMode::Explicit => quote!(::der::TagMode::Explicit),
            TagMode::Implicit => quote!(::der::TagMode::Implicit),
        }
    }
}

impl FromStr for TagMode {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, ParseError> {
        match s {
            "EXPLICIT" | "explicit" => Ok(TagMode::Explicit),
            "IMPLICIT" | "implicit" => Ok(TagMode::Implicit),
            _ => Err(ParseError),
        }
    }
}

impl Display for TagMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TagMode::Explicit => f.write_str("EXPLICIT"),
            TagMode::Implicit => f.write_str("IMPLICIT"),
        }
    }
}

/// ASN.1 tag numbers (i.e. lower 5 bits of a [`Tag`]).
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub(crate) struct TagNumber(pub u8);

impl TagNumber {
    /// Maximum tag number supported (inclusive).
    pub const MAX: u8 = 30;

    /// Get tokens describing this tag.
    pub fn to_tokens(self) -> TokenStream {
        match self.0 {
            0 => quote!(::der::TagNumber::N0),
            1 => quote!(::der::TagNumber::N1),
            2 => quote!(::der::TagNumber::N2),
            3 => quote!(::der::TagNumber::N3),
            4 => quote!(::der::TagNumber::N4),
            5 => quote!(::der::TagNumber::N5),
            6 => quote!(::der::TagNumber::N6),
            7 => quote!(::der::TagNumber::N7),
            8 => quote!(::der::TagNumber::N8),
            9 => quote!(::der::TagNumber::N9),
            10 => quote!(::der::TagNumber::N10),
            11 => quote!(::der::TagNumber::N11),
            12 => quote!(::der::TagNumber::N12),
            13 => quote!(::der::TagNumber::N13),
            14 => quote!(::der::TagNumber::N14),
            15 => quote!(::der::TagNumber::N15),
            16 => quote!(::der::TagNumber::N16),
            17 => quote!(::der::TagNumber::N17),
            18 => quote!(::der::TagNumber::N18),
            19 => quote!(::der::TagNumber::N19),
            20 => quote!(::der::TagNumber::N20),
            21 => quote!(::der::TagNumber::N21),
            22 => quote!(::der::TagNumber::N22),
            23 => quote!(::der::TagNumber::N23),
            24 => quote!(::der::TagNumber::N24),
            25 => quote!(::der::TagNumber::N25),
            26 => quote!(::der::TagNumber::N26),
            27 => quote!(::der::TagNumber::N27),
            28 => quote!(::der::TagNumber::N28),
            29 => quote!(::der::TagNumber::N29),
            30 => quote!(::der::TagNumber::N30),
            _ => unreachable!("tag number out of range: {}", self),
        }
    }
}

impl FromStr for TagNumber {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, ParseError> {
        let n = s.parse::<u8>().map_err(|_| ParseError)?;

        if n <= Self::MAX {
            Ok(Self(n))
        } else {
            Err(ParseError)
        }
    }
}

impl Display for TagNumber {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Error type
#[derive(Debug)]
pub(crate) struct ParseError;