From d127e38bde91b8a4149b8427b2c5232c42600348 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Tue, 21 Nov 2023 13:56:20 +0000 Subject: Import asn1-rs crate. Request Document: go/android-rust-importing-crates For CL Reviewers: go/android3p#cl-review For Build Team: go/ab-third-party-imports Bug: 312432424 Test: Treehugger Change-Id: I85bc232ad9135f53080ca8b0f5f3458aaf374d38 --- src/asn1_types/set/set_of.rs | 150 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 src/asn1_types/set/set_of.rs (limited to 'src/asn1_types/set/set_of.rs') diff --git a/src/asn1_types/set/set_of.rs b/src/asn1_types/set/set_of.rs new file mode 100644 index 0000000..af6c0a7 --- /dev/null +++ b/src/asn1_types/set/set_of.rs @@ -0,0 +1,150 @@ +use crate::*; +use alloc::vec::Vec; +use core::convert::TryFrom; +use core::iter::FromIterator; + +/// The `SET OF` object is an unordered list of homogeneous types. +/// +/// # Examples +/// +/// ``` +/// use asn1_rs::SetOf; +/// use std::iter::FromIterator; +/// +/// // build set +/// let it = [2, 3, 4].iter(); +/// let set = SetOf::from_iter(it); +/// +/// // `set` now contains the serialized DER representation of the array +/// +/// // iterate objects +/// let mut sum = 0; +/// for item in set.iter() { +/// // item has type `Result`, since parsing the serialized bytes could fail +/// sum += *item; +/// } +/// assert_eq!(sum, 9); +/// +/// ``` +#[derive(Debug)] +pub struct SetOf { + items: Vec, +} + +impl SetOf { + /// Builds a `SET OF` from the provided content + #[inline] + pub const fn new(items: Vec) -> Self { + SetOf { items } + } + + /// Returns the length of this `SET` (the number of items). + #[inline] + pub fn len(&self) -> usize { + self.items.len() + } + + /// Returns `true` if this `SET` is empty. + #[inline] + pub fn is_empty(&self) -> bool { + self.items.is_empty() + } + + /// Returns an iterator over the items of the `SET`. + #[inline] + pub fn iter(&self) -> impl Iterator { + self.items.iter() + } +} + +impl AsRef<[T]> for SetOf { + fn as_ref(&self) -> &[T] { + &self.items + } +} + +impl<'a, T> IntoIterator for &'a SetOf { + type Item = &'a T; + type IntoIter = core::slice::Iter<'a, T>; + + fn into_iter(self) -> core::slice::Iter<'a, T> { + self.items.iter() + } +} + +impl<'a, T> IntoIterator for &'a mut SetOf { + type Item = &'a mut T; + type IntoIter = core::slice::IterMut<'a, T>; + + fn into_iter(self) -> core::slice::IterMut<'a, T> { + self.items.iter_mut() + } +} + +impl From> for Vec { + fn from(set: SetOf) -> Self { + set.items + } +} + +impl FromIterator for SetOf { + fn from_iter>(iter: IT) -> Self { + let items = iter.into_iter().collect(); + SetOf::new(items) + } +} + +impl<'a, T> TryFrom> for SetOf +where + T: FromBer<'a>, +{ + type Error = Error; + + fn try_from(any: Any<'a>) -> Result { + any.tag().assert_eq(Self::TAG)?; + if !any.header.is_constructed() { + return Err(Error::ConstructExpected); + } + let items = SetIterator::::new(any.data).collect::>>()?; + Ok(SetOf::new(items)) + } +} + +impl CheckDerConstraints for SetOf +where + T: CheckDerConstraints, +{ + fn check_constraints(any: &Any) -> Result<()> { + any.tag().assert_eq(Self::TAG)?; + any.header.assert_constructed()?; + for item in SetIterator::::new(any.data) { + let item = item?; + T::check_constraints(&item)?; + } + Ok(()) + } +} + +impl DerAutoDerive for SetOf {} + +impl Tagged for SetOf { + const TAG: Tag = Tag::Set; +} + +#[cfg(feature = "std")] +impl ToDer for SetOf +where + T: ToDer, +{ + fn to_der_len(&self) -> Result { + self.items.to_der_len() + } + + fn write_der_header(&self, writer: &mut dyn std::io::Write) -> SerializeResult { + self.items.write_der_header(writer) + } + + fn write_der_content(&self, writer: &mut dyn std::io::Write) -> SerializeResult { + self.items.write_der_content(writer) + } +} -- cgit v1.2.3