Skip to content

Commit 8ad493d

Browse files
authored
Simplify generated enum code -- write it in terms of public APIs (#503)
1 parent cbe549b commit 8ad493d

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

asn1_derive/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,31 +566,31 @@ fn generate_enum_read_block(
566566
OpType::Explicit(arg) => {
567567
let tag = arg.value;
568568
read_blocks.push(quote::quote! {
569-
if tlv.tag() == asn1::explicit_tag(#tag) {
569+
if asn1::Explicit::<#ty, #tag>::can_parse(tlv.tag()) {
570570
return Ok(#name::#ident(asn1::parse(
571571
tlv.full_data(),
572572
|p| Ok(p.read_element::<asn1::Explicit<_, #tag>>()#add_error_location?.into_inner())
573573
)?))
574574
}
575575
});
576576
can_parse_blocks.push(quote::quote! {
577-
if tag == asn1::explicit_tag(#tag) {
577+
if asn1::Explicit::<#ty, #tag>::can_parse(tag) {
578578
return true;
579579
}
580580
});
581581
}
582582
OpType::Implicit(arg) => {
583583
let tag = arg.value;
584584
read_blocks.push(quote::quote! {
585-
if tlv.tag() == asn1::implicit_tag(#tag, <#ty as asn1::SimpleAsn1Readable>::TAG) {
585+
if asn1::Implicit::<#ty, #tag>::can_parse(tlv.tag()) {
586586
return Ok(#name::#ident(asn1::parse(
587587
tlv.full_data(),
588588
|p| Ok(p.read_element::<asn1::Implicit<_, #tag>>()#add_error_location?.into_inner())
589589
)?))
590590
}
591591
});
592592
can_parse_blocks.push(quote::quote! {
593-
if tag == asn1::implicit_tag(#tag, <#ty as asn1::SimpleAsn1Readable>::TAG) {
593+
if asn1::Implicit::<#ty, #tag>::can_parse(tag) {
594594
return true;
595595
}
596596
});

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,14 @@ pub fn write_defined_by<T: Asn1Writable, U: Asn1DefinedByWritable<T>>(
231231
pub fn writable_defined_by_item<T: Asn1Writable, U: Asn1DefinedByWritable<T>>(v: &U) -> &T {
232232
v.item()
233233
}
234+
235+
#[cfg(test)]
236+
mod tests {
237+
#[test]
238+
fn test_implicit_tag() {
239+
let t = crate::implicit_tag(3, crate::Tag::primitive(2));
240+
assert_eq!(t.as_u8(), Some(0x83));
241+
let t = crate::implicit_tag(3, crate::Tag::constructed(2));
242+
assert_eq!(t.as_u8(), Some(0xa3));
243+
}
244+
}

0 commit comments

Comments
 (0)