Skip to content

Commit c83ba75

Browse files
committed
der: fix derive for Option<&[u8]> and other Asn1Type's
1 parent 58b8a27 commit c83ba75

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

der/tests/derive.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,9 @@ mod sequence {
478478
#[test]
479479
fn type_combinations_instance() {
480480
let obj = TypeCheckExpandedSequenceFieldAttributeCombinations {
481+
optional: Some(true),
482+
optional_octet_string: Some(&[0xAA, 0xBB]),
483+
optional_bit_string: Some(&[0xCC, 0xDD]),
481484
context_specific_optional: Some(true),
482485
typed_context_specific: &[0, 1],
483486
typed_context_specific_optional_bits: Some(&[2, 3]),
@@ -488,6 +491,7 @@ mod sequence {
488491
};
489492

490493
let der_encoded = obj.to_der().unwrap();
494+
491495
let obj_decoded =
492496
TypeCheckExpandedSequenceFieldAttributeCombinations::from_der(&der_encoded).unwrap();
493497
assert_eq!(obj, obj_decoded);

der_derive/src/asn1_type.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,13 @@ impl Asn1Type {
5454

5555
/// Get a `der::Decoder` object for a particular ASN.1 type
5656
pub fn decoder(self) -> TokenStream {
57-
match self {
58-
Asn1Type::BitString => quote!(::der::asn1::BitStringRef::decode(reader)?),
59-
Asn1Type::Ia5String => quote!(::der::asn1::Ia5StringRef::decode(reader)?),
60-
Asn1Type::GeneralizedTime => quote!(::der::asn1::GeneralizedTime::decode(reader)?),
61-
Asn1Type::OctetString => quote!(::der::asn1::OctetStringRef::decode(reader)?),
62-
Asn1Type::PrintableString => quote!(::der::asn1::PrintableStringRef::decode(reader)?),
63-
Asn1Type::TeletexString => quote!(::der::asn1::TeletexStringRef::decode(reader)?),
64-
Asn1Type::VideotexString => quote!(::der::asn1::VideotexStringRef::decode(reader)?),
65-
Asn1Type::UtcTime => quote!(::der::asn1::UtcTime::decode(reader)?),
66-
Asn1Type::Utf8String => quote!(::der::asn1::Utf8StringRef::decode(reader)?),
67-
}
57+
let type_path = self.type_path();
58+
quote!(#type_path::decode(reader)?)
59+
}
60+
/// Get a `der::Decoder` optional object for a particular ASN.1 type
61+
pub fn decoder_optional(self) -> TokenStream {
62+
let type_path = self.type_path();
63+
quote!(Option::<#type_path>::decode(reader)?)
6864
}
6965

7066
/// Get a `der::Encoder` object for a particular ASN.1 type

der_derive/src/attributes.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,15 @@ impl FieldAttrs {
300300
}
301301
})
302302
} else {
303-
self.asn1_type
304-
.map(|ty| ty.decoder())
305-
.unwrap_or_else(|| quote!(reader.decode()?))
303+
if self.is_optional() {
304+
self.asn1_type
305+
.map(|ty| ty.decoder_optional())
306+
.unwrap_or_else(|| quote!(reader.decode()?))
307+
} else {
308+
self.asn1_type
309+
.map(|ty| ty.decoder())
310+
.unwrap_or_else(|| quote!(reader.decode()?))
311+
}
306312
}
307313
}
308314

0 commit comments

Comments
 (0)