Skip to content

Commit 5449fd5

Browse files
committed
fix: derive(Sequence) on field: Option<&'a [u8]>
1 parent 2beb265 commit 5449fd5

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

der/src/asn1/octet_string.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,21 @@ impl<'a> From<OctetStringRef<'a>> for &'a [u8] {
9696
}
9797
}
9898

99-
impl<'a> TryFrom<&'a [u8]> for OctetStringRef<'a> {
100-
type Error = Error;
99+
impl<'a> From<&'a [u8]> for OctetStringRef<'a> {
100+
fn from(byte_slice: &'a [u8]) -> Self {
101+
OctetStringRef::new(byte_slice).expect("byte_slice to be sane length")
102+
}
103+
}
104+
105+
impl<'a> From<&&'a [u8]> for OctetStringRef<'a> {
106+
fn from(byte_slice: &&'a [u8]) -> Self {
107+
From::<&'a [u8]>::from(byte_slice)
108+
}
109+
}
101110

102-
fn try_from(byte_slice: &'a [u8]) -> Result<Self, Error> {
103-
OctetStringRef::new(byte_slice)
111+
impl<'a> From<&&&'a [u8]> for OctetStringRef<'a> {
112+
fn from(byte_slice: &&&'a [u8]) -> Self {
113+
From::<&'a [u8]>::from(byte_slice)
104114
}
105115
}
106116

der/tests/derive.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ mod sequence {
404404
const ALGORITHM_IDENTIFIER_DER: &[u8] =
405405
&hex!("30 13 06 07 2a 86 48 ce 3d 02 01 06 08 2a 86 48 ce 3d 03 01 07");
406406

407-
#[derive(Sequence)]
407+
#[derive(Sequence, Default, Eq, PartialEq, Debug)]
408408
#[asn1(tag_mode = "IMPLICIT")]
409409
pub struct TypeCheckExpandedSequenceFieldAttributeCombinations<'a> {
410410
pub simple: bool,
@@ -423,7 +423,34 @@ mod sequence {
423423
#[asn1(context_specific = "3", default = "default_false_example")]
424424
pub context_specific_default: bool,
425425
#[asn1(type = "BIT STRING", context_specific = "4", optional = "true")]
426-
pub typed_context_specific_optional: Option<&'a [u8]>,
426+
pub typed_context_specific_optional_bits: Option<&'a [u8]>,
427+
#[asn1(type = "OCTET STRING", context_specific = "5", optional = "true")]
428+
pub typed_context_specific_optional_implicit: Option<&'a [u8]>,
429+
#[asn1(
430+
type = "OCTET STRING",
431+
context_specific = "6",
432+
optional = "true",
433+
tag_mode = "EXPLICIT"
434+
)]
435+
pub typed_context_specific_optional_explicit: Option<&'a [u8]>,
436+
}
437+
438+
#[test]
439+
fn type_combinations_instance() {
440+
let obj = TypeCheckExpandedSequenceFieldAttributeCombinations {
441+
context_specific_optional: Some(true),
442+
typed_context_specific: &[0, 1],
443+
typed_context_specific_optional_bits: Some(&[2, 3]),
444+
typed_context_specific_optional_implicit: Some(&[4, 5, 6]),
445+
typed_context_specific_optional_explicit: Some(&[7, 8]),
446+
447+
..Default::default()
448+
};
449+
450+
let der_encoded = obj.to_der().unwrap();
451+
let obj_decoded =
452+
TypeCheckExpandedSequenceFieldAttributeCombinations::from_der(&der_encoded).unwrap();
453+
assert_eq!(obj, obj_decoded);
427454
}
428455

429456
#[derive(Sequence)]

0 commit comments

Comments
 (0)