Skip to content

Commit

Permalink
fix: derive(Sequence) on field: Option<&'a [u8]> (#1675)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* docs: TryFrom<&&'a [u8]>

Co-authored-by: Arthur Gautier <[email protected]>

* OctetStringRef::new

Co-authored-by: Arthur Gautier <[email protected]>

* fmt space

---------

Co-authored-by: Arthur Gautier <[email protected]>
  • Loading branch information
dishmaker and baloo authored Feb 25, 2025
1 parent 42b178a commit ebf3961
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
9 changes: 9 additions & 0 deletions der/src/asn1/octet_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ impl<'a> TryFrom<&'a [u8]> for OctetStringRef<'a> {
}
}

/// Hack for simplifying the custom derive use case.
impl<'a> TryFrom<&&'a [u8]> for OctetStringRef<'a> {
type Error = Error;

fn try_from(byte_slice: &&'a [u8]) -> Result<Self, Error> {
OctetStringRef::new(byte_slice)
}
}

#[cfg(feature = "alloc")]
pub use self::allocating::OctetString;

Expand Down
31 changes: 29 additions & 2 deletions der/tests/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ mod sequence {
const ALGORITHM_IDENTIFIER_DER: &[u8] =
&hex!("30 13 06 07 2a 86 48 ce 3d 02 01 06 08 2a 86 48 ce 3d 03 01 07");

#[derive(Sequence)]
#[derive(Sequence, Default, Eq, PartialEq, Debug)]
#[asn1(tag_mode = "IMPLICIT")]
pub struct TypeCheckExpandedSequenceFieldAttributeCombinations<'a> {
pub simple: bool,
Expand All @@ -423,7 +423,34 @@ mod sequence {
#[asn1(context_specific = "3", default = "default_false_example")]
pub context_specific_default: bool,
#[asn1(type = "BIT STRING", context_specific = "4", optional = "true")]
pub typed_context_specific_optional: Option<&'a [u8]>,
pub typed_context_specific_optional_bits: Option<&'a [u8]>,
#[asn1(type = "OCTET STRING", context_specific = "5", optional = "true")]
pub typed_context_specific_optional_implicit: Option<&'a [u8]>,
#[asn1(
type = "OCTET STRING",
context_specific = "6",
optional = "true",
tag_mode = "EXPLICIT"
)]
pub typed_context_specific_optional_explicit: Option<&'a [u8]>,
}

#[test]
fn type_combinations_instance() {
let obj = TypeCheckExpandedSequenceFieldAttributeCombinations {
context_specific_optional: Some(true),
typed_context_specific: &[0, 1],
typed_context_specific_optional_bits: Some(&[2, 3]),
typed_context_specific_optional_implicit: Some(&[4, 5, 6]),
typed_context_specific_optional_explicit: Some(&[7, 8]),

..Default::default()
};

let der_encoded = obj.to_der().unwrap();
let obj_decoded =
TypeCheckExpandedSequenceFieldAttributeCombinations::from_der(&der_encoded).unwrap();
assert_eq!(obj, obj_decoded);
}

#[derive(Sequence)]
Expand Down

0 comments on commit ebf3961

Please sign in to comment.