1
1
//! ASN.1 `ANY` type.
2
+
2
3
#![ cfg_attr( feature = "arbitrary" , allow( clippy:: integer_arithmetic) ) ]
3
4
4
5
use crate :: {
5
- asn1 :: * , BytesRef , Choice , Decode , DecodeValue , DerOrd , EncodeValue , Error , ErrorKind ,
6
- FixedTag , Header , Length , Reader , Result , SliceReader , Tag , Tagged , ValueOrd , Writer ,
6
+ BytesRef , Choice , Decode , DecodeValue , DerOrd , EncodeValue , Error , ErrorKind , Header , Length ,
7
+ Reader , Result , SliceReader , Tag , Tagged , ValueOrd , Writer ,
7
8
} ;
8
9
use core:: cmp:: Ordering ;
9
10
10
11
#[ cfg( feature = "alloc" ) ]
11
12
use { crate :: BytesOwned , alloc:: boxed:: Box } ;
12
13
13
- #[ cfg( feature = "oid" ) ]
14
- use crate :: asn1:: ObjectIdentifier ;
15
-
16
14
/// ASN.1 `ANY`: represents any explicitly tagged ASN.1 value.
17
15
///
18
16
/// This is a zero-copy reference type which borrows from the input data.
@@ -58,11 +56,14 @@ impl<'a> AnyRef<'a> {
58
56
}
59
57
60
58
/// Attempt to decode this [`AnyRef`] type into the inner value.
61
- pub fn decode_into < T > ( self ) -> Result < T >
59
+ pub fn decode_as < T > ( self ) -> Result < T >
62
60
where
63
- T : DecodeValue < ' a > + FixedTag ,
61
+ T : Choice < ' a > + DecodeValue < ' a > ,
64
62
{
65
- self . tag . assert_eq ( T :: TAG ) ?;
63
+ if !T :: can_decode ( self . tag ) {
64
+ return Err ( self . tag . unexpected_error ( None ) ) ;
65
+ }
66
+
66
67
let header = Header {
67
68
tag : self . tag ,
68
69
length : self . value . len ( ) ,
@@ -78,48 +79,6 @@ impl<'a> AnyRef<'a> {
78
79
self == Self :: NULL
79
80
}
80
81
81
- /// Attempt to decode an ASN.1 `BIT STRING`.
82
- pub fn bit_string ( self ) -> Result < BitStringRef < ' a > > {
83
- self . try_into ( )
84
- }
85
-
86
- /// Attempt to decode an ASN.1 `CONTEXT-SPECIFIC` field.
87
- pub fn context_specific < T > ( self ) -> Result < ContextSpecific < T > >
88
- where
89
- T : Decode < ' a > ,
90
- {
91
- self . try_into ( )
92
- }
93
-
94
- /// Attempt to decode an ASN.1 `GeneralizedTime`.
95
- pub fn generalized_time ( self ) -> Result < GeneralizedTime > {
96
- self . try_into ( )
97
- }
98
-
99
- /// Attempt to decode an ASN.1 `OCTET STRING`.
100
- pub fn octet_string ( self ) -> Result < OctetStringRef < ' a > > {
101
- self . try_into ( )
102
- }
103
-
104
- /// Attempt to decode an ASN.1 `OBJECT IDENTIFIER`.
105
- #[ cfg( feature = "oid" ) ]
106
- #[ cfg_attr( docsrs, doc( cfg( feature = "oid" ) ) ) ]
107
- pub fn oid ( self ) -> Result < ObjectIdentifier > {
108
- self . try_into ( )
109
- }
110
-
111
- /// Attempt to decode an ASN.1 `OPTIONAL` value.
112
- pub fn optional < T > ( self ) -> Result < Option < T > >
113
- where
114
- T : Choice < ' a > + TryFrom < Self , Error = Error > ,
115
- {
116
- if T :: can_decode ( self . tag ) {
117
- T :: try_from ( self ) . map ( Some )
118
- } else {
119
- Ok ( None )
120
- }
121
- }
122
-
123
82
/// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
124
83
/// nested reader and calling the provided argument with it.
125
84
pub fn sequence < F , T > ( self , f : F ) -> Result < T >
@@ -131,11 +90,6 @@ impl<'a> AnyRef<'a> {
131
90
let result = f ( & mut reader) ?;
132
91
reader. finish ( result)
133
92
}
134
-
135
- /// Attempt to decode an ASN.1 `UTCTime`.
136
- pub fn utc_time ( self ) -> Result < UtcTime > {
137
- self . try_into ( )
138
- }
139
93
}
140
94
141
95
impl < ' a > Choice < ' a > for AnyRef < ' a > {
@@ -231,19 +185,20 @@ impl Any {
231
185
}
232
186
233
187
/// Attempt to decode this [`Any`] type into the inner value.
234
- pub fn decode_into < ' a , T > ( & ' a self ) -> Result < T >
188
+ pub fn decode_as < ' a , T > ( & ' a self ) -> Result < T >
235
189
where
236
- T : DecodeValue < ' a > + FixedTag ,
190
+ T : Choice < ' a > + DecodeValue < ' a > ,
237
191
{
238
- self . tag . assert_eq ( T :: TAG ) ?;
239
- let header = Header {
240
- tag : self . tag ,
241
- length : self . value . len ( ) ,
242
- } ;
192
+ AnyRef :: from ( self ) . decode_as ( )
193
+ }
243
194
244
- let mut decoder = SliceReader :: new ( self . value . as_slice ( ) ) ?;
245
- let result = T :: decode_value ( & mut decoder, header) ?;
246
- decoder. finish ( result)
195
+ /// Attempt to decode this value an ASN.1 `SEQUENCE`, creating a new
196
+ /// nested reader and calling the provided argument with it.
197
+ pub fn sequence < ' a , F , T > ( & ' a self , f : F ) -> Result < T >
198
+ where
199
+ F : FnOnce ( & mut SliceReader < ' a > ) -> Result < T > ,
200
+ {
201
+ AnyRef :: from ( self ) . sequence ( f)
247
202
}
248
203
}
249
204
0 commit comments