Skip to content

Commit

Permalink
Simplify generated enum code -- write it in terms of public APIs (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex authored Nov 18, 2024
1 parent cbe549b commit 8ad493d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions asn1_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,31 +566,31 @@ fn generate_enum_read_block(
OpType::Explicit(arg) => {
let tag = arg.value;
read_blocks.push(quote::quote! {
if tlv.tag() == asn1::explicit_tag(#tag) {
if asn1::Explicit::<#ty, #tag>::can_parse(tlv.tag()) {
return Ok(#name::#ident(asn1::parse(
tlv.full_data(),
|p| Ok(p.read_element::<asn1::Explicit<_, #tag>>()#add_error_location?.into_inner())
)?))
}
});
can_parse_blocks.push(quote::quote! {
if tag == asn1::explicit_tag(#tag) {
if asn1::Explicit::<#ty, #tag>::can_parse(tag) {
return true;
}
});
}
OpType::Implicit(arg) => {
let tag = arg.value;
read_blocks.push(quote::quote! {
if tlv.tag() == asn1::implicit_tag(#tag, <#ty as asn1::SimpleAsn1Readable>::TAG) {
if asn1::Implicit::<#ty, #tag>::can_parse(tlv.tag()) {
return Ok(#name::#ident(asn1::parse(
tlv.full_data(),
|p| Ok(p.read_element::<asn1::Implicit<_, #tag>>()#add_error_location?.into_inner())
)?))
}
});
can_parse_blocks.push(quote::quote! {
if tag == asn1::implicit_tag(#tag, <#ty as asn1::SimpleAsn1Readable>::TAG) {
if asn1::Implicit::<#ty, #tag>::can_parse(tag) {
return true;
}
});
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,14 @@ pub fn write_defined_by<T: Asn1Writable, U: Asn1DefinedByWritable<T>>(
pub fn writable_defined_by_item<T: Asn1Writable, U: Asn1DefinedByWritable<T>>(v: &U) -> &T {
v.item()
}

#[cfg(test)]
mod tests {
#[test]
fn test_implicit_tag() {
let t = crate::implicit_tag(3, crate::Tag::primitive(2));
assert_eq!(t.as_u8(), Some(0x83));
let t = crate::implicit_tag(3, crate::Tag::constructed(2));
assert_eq!(t.as_u8(), Some(0xa3));
}
}

0 comments on commit 8ad493d

Please sign in to comment.