Skip to content

Commit

Permalink
Remove now unneeded aliases (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex authored Nov 17, 2024
1 parent 8573264 commit 6ffe269
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 98 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions asn1_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,23 +626,27 @@ 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))?;
}
}
}
}
OpType::Implicit(arg) => {
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))?;
}
}
}
}
Expand Down Expand Up @@ -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"),
Expand Down
7 changes: 7 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,13 @@ impl<T: Asn1Writable, const TAG: u32> SimpleAsn1Writable for Explicit<T, { TAG }
}
}

impl<const TAG: u32> 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<U, { TAG }>
{
Expand Down
93 changes: 1 addition & 92 deletions src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::types::{Asn1Writable, SimpleAsn1Writable};
use crate::types::Asn1Writable;
use crate::Tag;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
Expand Down Expand Up @@ -107,50 +107,6 @@ impl Writer<'_> {
val.write(self)
}

/// This is an alias for `write_element::<Explicit<T, tag>>`.
pub fn write_explicit_element<T: Asn1Writable>(&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::<Option<Explicit<T, tag>>>`.
pub fn write_optional_explicit_element<T: Asn1Writable>(
&mut self,
val: &Option<T>,
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::<Implicit<T, tag>>`.
pub fn write_implicit_element<T: SimpleAsn1Writable>(
&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::<Option<Implicit<T, tag>>>`.
pub fn write_optional_implicit_element<T: SimpleAsn1Writable>(
&mut self,
val: &Option<T>,
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.
Expand Down Expand Up @@ -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::<u8>(&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::<SequenceWriter<'_>>(&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]
Expand All @@ -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::<u8>(&None, 2) }).unwrap(),
b""
);

assert_eq!(
write(|w| { w.write_explicit_element(&true, 2) }).unwrap(),
b"\xa2\x03\x01\x01\xff"
);
}

#[test]
Expand Down
19 changes: 19 additions & 0 deletions tests/derive_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<asn1::Tlv<'a>>,
}

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)]
Expand Down

0 comments on commit 6ffe269

Please sign in to comment.