Skip to content

Commit ebf3961

Browse files
dishmakerbaloo
andauthored
fix: derive(Sequence) on field: Option<&'a [u8]> (#1675)
* fix: derive(Sequence) on field: Option<&'a [u8]> * undo TryFrom<&[u8]> * fix: add TryFrom<&&'a [u8]> for OctetStringRef * rm &&&'a [u8] Co-authored-by: Arthur Gautier <superbaloo+registrations.github@superbaloo.net> * docs: TryFrom<&&'a [u8]> Co-authored-by: Arthur Gautier <superbaloo+registrations.github@superbaloo.net> * OctetStringRef::new Co-authored-by: Arthur Gautier <superbaloo+registrations.github@superbaloo.net> * fmt space --------- Co-authored-by: Arthur Gautier <superbaloo+registrations.github@superbaloo.net>
1 parent 42b178a commit ebf3961

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

der/src/asn1/octet_string.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ impl<'a> TryFrom<&'a [u8]> for OctetStringRef<'a> {
104104
}
105105
}
106106

107+
/// Hack for simplifying the custom derive use case.
108+
impl<'a> TryFrom<&&'a [u8]> for OctetStringRef<'a> {
109+
type Error = Error;
110+
111+
fn try_from(byte_slice: &&'a [u8]) -> Result<Self, Error> {
112+
OctetStringRef::new(byte_slice)
113+
}
114+
}
115+
107116
#[cfg(feature = "alloc")]
108117
pub use self::allocating::OctetString;
109118

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)