diff --git a/README.md b/README.md index d53ee81..2d38f54 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,14 @@ asn1 = { version = "0.19", default-features = false } ## Changelog +### [0.20.0] + +#### :rotating_light: Breaking changes + +- Removed `Writer::{write_explicit_element, write_optional_explicit_element, write_implicit_element, write_optional_implicit_element}`. + These can all be better accomplished with the `asn1::Explicit` and + `asn1::Implicit` types. + ### [0.19.0] #### :rotating_light: Breaking changes diff --git a/asn1_derive/src/lib.rs b/asn1_derive/src/lib.rs index be093a6..47370d7 100644 --- a/asn1_derive/src/lib.rs +++ b/asn1_derive/src/lib.rs @@ -626,11 +626,13 @@ fn generate_write_element( let value = arg.value; if arg.required { quote::quote_spanned! {f.span() => - w.write_explicit_element(#field_read, #value)?; + w.write_element(&asn1::Explicit::<_, #value>::new(#field_read))?; } } else { quote::quote_spanned! {f.span() => - w.write_optional_explicit_element(#field_read, #value)?; + if let Some(v) = #field_read { + w.write_element(&asn1::Explicit::<_, #value>::new(v))?; + } } } } @@ -638,11 +640,13 @@ fn generate_write_element( let value = arg.value; if arg.required { quote::quote_spanned! {f.span() => - w.write_implicit_element(#field_read, #value)?; + w.write_element(&asn1::Implicit::<_, #value>::new(#field_read))?; } } else { quote::quote_spanned! {f.span() => - w.write_optional_implicit_element(#field_read, #value)?; + if let Some(v) = #field_read { + w.write_element(&asn1::Implicit::<_, #value>::new(v))?; + } } } } @@ -731,13 +735,13 @@ fn generate_enum_write_block(name: &syn::Ident, data: &syn::DataEnum) -> proc_ma OpType::Explicit(arg) => { let tag = arg.value; quote::quote! { - #name::#ident(value) => w.write_explicit_element(&value, #tag), + #name::#ident(value) => w.write_element(&asn1::Explicit::<_, #tag>::new(value)), } } OpType::Implicit(arg) => { let tag = arg.value; quote::quote! { - #name::#ident(value) => w.write_implicit_element(&value, #tag), + #name::#ident(value) => w.write_element(&asn1::Implicit::<_, #tag>::new(value)), } } OpType::DefinedBy(_) => panic!("Can't use #[defined_by] in an Asn1Write on an enum"), diff --git a/src/types.rs b/src/types.rs index 550df8f..aa79703 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1809,6 +1809,13 @@ impl SimpleAsn1Writable for Explicit SimpleAsn1Writable for Explicit<&'_ Tlv<'_>, { TAG }> { + const TAG: Tag = crate::explicit_tag(TAG); + fn write_data(&self, dest: &mut WriteBuf) -> WriteResult { + self.inner.write(&mut Writer::new(dest)) + } +} + impl<'a, T: Asn1Readable<'a>, U: Asn1DefinedByReadable<'a, T>, const TAG: u32> Asn1DefinedByReadable<'a, T> for Explicit { diff --git a/src/writer.rs b/src/writer.rs index 11fcb41..d2fe14e 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -1,4 +1,4 @@ -use crate::types::{Asn1Writable, SimpleAsn1Writable}; +use crate::types::Asn1Writable; use crate::Tag; #[cfg(not(feature = "std"))] use alloc::vec::Vec; @@ -107,50 +107,6 @@ impl Writer<'_> { val.write(self) } - /// This is an alias for `write_element::>`. - pub fn write_explicit_element(&mut self, val: &T, tag: u32) -> WriteResult { - let tag = crate::explicit_tag(tag); - self.write_tlv(tag, |dest| Writer::new(dest).write_element(val)) - } - - /// This is an alias for `write_element::>>`. - pub fn write_optional_explicit_element( - &mut self, - val: &Option, - tag: u32, - ) -> WriteResult { - if let Some(v) = val { - let tag = crate::explicit_tag(tag); - self.write_tlv(tag, |dest| Writer::new(dest).write_element(v)) - } else { - Ok(()) - } - } - - /// This is an alias for `write_element::>`. - pub fn write_implicit_element( - &mut self, - val: &T, - tag: u32, - ) -> WriteResult { - let tag = crate::implicit_tag(tag, T::TAG); - self.write_tlv(tag, |dest| val.write_data(dest)) - } - - /// This is an alias for `write_element::>>`. - pub fn write_optional_implicit_element( - &mut self, - val: &Option, - tag: u32, - ) -> WriteResult { - if let Some(v) = val { - let tag = crate::implicit_tag(tag, T::TAG); - self.write_tlv(tag, |dest| v.write_data(dest)) - } else { - Ok(()) - } - } - /// Writes a TLV with the specified tag where the value is any bytes /// written to the `Vec` in the callback. The length portion of the /// TLV is automatically computed. @@ -716,39 +672,6 @@ mod tests { (Implicit::new(true), b"\x82\x01\xff"), (Implicit::new(false), b"\x82\x01\x00"), ]); - - assert_eq!( - write(|w| { w.write_optional_implicit_element(&Some(true), 2) }).unwrap(), - b"\x82\x01\xff" - ); - assert_eq!( - write(|w| { w.write_optional_explicit_element::(&None, 2) }).unwrap(), - b"" - ); - - assert_eq!( - write(|w| { - w.write_optional_implicit_element(&Some(SequenceWriter::new(&|_w| Ok(()))), 2) - }) - .unwrap(), - b"\xa2\x00" - ); - assert_eq!( - write(|w| { w.write_optional_explicit_element::>(&None, 2) }) - .unwrap(), - b"" - ); - - assert_eq!( - write(|w| { w.write_implicit_element(&true, 2) }).unwrap(), - b"\x82\x01\xff" - ); - - assert_eq!( - write(|w| { w.write_implicit_element(&SequenceWriter::new(&|_w| { Ok(()) }), 2) }) - .unwrap(), - b"\xa2\x00" - ); } #[test] @@ -757,20 +680,6 @@ mod tests { (Explicit::new(true), b"\xa2\x03\x01\x01\xff"), (Explicit::new(false), b"\xa2\x03\x01\x01\x00"), ]); - - assert_eq!( - write(|w| { w.write_optional_explicit_element(&Some(true), 2) }).unwrap(), - b"\xa2\x03\x01\x01\xff" - ); - assert_eq!( - write(|w| { w.write_optional_explicit_element::(&None, 2) }).unwrap(), - b"" - ); - - assert_eq!( - write(|w| { w.write_explicit_element(&true, 2) }).unwrap(), - b"\xa2\x03\x01\x01\xff" - ); } #[test] diff --git a/tests/derive_test.rs b/tests/derive_test.rs index 07538fd..8d58493 100644 --- a/tests/derive_test.rs +++ b/tests/derive_test.rs @@ -117,6 +117,25 @@ fn test_explicit() { ]); } +#[test] +fn test_explicit_tlv() { + #[derive(asn1::Asn1Read, asn1::Asn1Write, Debug, PartialEq, Eq)] + struct ExplicitTlv<'a> { + #[explicit(5)] + a: Option>, + } + + assert_roundtrips(&[ + (Ok(ExplicitTlv { a: None }), b"\x30\x00"), + ( + Ok(ExplicitTlv { + a: asn1::parse_single(b"\x05\x00").unwrap(), + }), + b"\x30\x04\xa5\x02\x05\x00", + ), + ]); +} + #[test] fn test_implicit() { #[derive(asn1::Asn1Read, asn1::Asn1Write, Debug, PartialEq, Eq)]