use crate::*;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::convert::TryFrom;
use core::fmt::{Debug, Display};
use core::iter::FromIterator;
use core::ops::{Deref, DerefMut};
use self::debug::{trace, trace_generic};
#[derive(Debug, PartialEq)]
pub struct SetOf<T> {
items: Vec<T>,
}
impl<T> SetOf<T> {
#[inline]
pub const fn new(items: Vec<T>) -> Self {
SetOf { items }
}
#[inline]
pub fn into_vec(self) -> Vec<T> {
self.items
}
#[inline]
pub fn push(&mut self, item: T) {
self.items.push(item)
}
}
impl<T> AsRef<[T]> for SetOf<T> {
fn as_ref(&self) -> &[T] {
&self.items
}
}
impl<T> AsMut<[T]> for SetOf<T> {
fn as_mut(&mut self) -> &mut [T] {
&mut self.items
}
}
impl<T> Deref for SetOf<T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
&self.items
}
}
impl<T> DerefMut for SetOf<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.items
}
}
impl<T> From<SetOf<T>> for Vec<T> {
fn from(set: SetOf<T>) -> Self {
set.items
}
}
impl<T> FromIterator<T> for SetOf<T> {
fn from_iter<IT: IntoIterator<Item = T>>(iter: IT) -> Self {
let items = Vec::from_iter(iter);
SetOf::new(items)
}
}
impl<'a, T> TryFrom<Any<'a>> for SetOf<T>
where
T: FromBer<'a>,
{
type Error = Error;
fn try_from(any: Any<'a>) -> Result<Self> {
any.tag().assert_eq(Self::TAG)?;
if !any.header.is_constructed() {
return Err(Error::ConstructExpected);
}
let items = SetIterator::<T, BerParser>::new(any.data).collect::<Result<Vec<T>>>()?;
Ok(SetOf::new(items))
}
}
impl<T> CheckDerConstraints for SetOf<T>
where
T: CheckDerConstraints,
{
fn check_constraints(any: &Any) -> Result<()> {
any.tag().assert_eq(Self::TAG)?;
any.header.assert_constructed()?;
for item in SetIterator::<Any, DerParser>::new(any.data) {
let item = item?;
T::check_constraints(&item)?;
}
Ok(())
}
}
impl<'a, T, E> FromDer<'a, E> for SetOf<T>
where
T: FromDer<'a, E>,
E: From<Error> + Display + Debug,
{
fn from_der(bytes: &'a [u8]) -> ParseResult<Self, E> {
trace_generic(
core::any::type_name::<Self>(),
"SetOf::from_der",
|bytes| {
let (rem, any) = trace(core::any::type_name::<Self>(), Any::from_der, bytes)
.map_err(Err::convert)?;
any.header
.assert_tag(Self::TAG)
.map_err(|e| Err::Error(e.into()))?;
let items = SetIterator::<T, DerParser, E>::new(any.data)
.collect::<Result<Vec<T>, E>>()
.map_err(Err::Error)?;
Ok((rem, SetOf::new(items)))
},
bytes,
)
}
}
impl<T> Tagged for SetOf<T> {
const TAG: Tag = Tag::Set;
}
#[cfg(feature = "std")]
impl<T> ToDer for SetOf<T>
where
T: ToDer,
{
fn to_der_len(&self) -> Result<usize> {
self.items.to_der_len()
}
fn write_der_header(&self, writer: &mut dyn std::io::Write) -> SerializeResult<usize> {
let mut len = 0;
for t in self.items.iter() {
len += t.to_der_len().map_err(|_| SerializeError::InvalidLength)?;
}
let header = Header::new(Class::Universal, true, Self::TAG, Length::Definite(len));
header.write_der_header(writer).map_err(Into::into)
}
fn write_der_content(&self, writer: &mut dyn std::io::Write) -> SerializeResult<usize> {
self.items.write_der_content(writer)
}
}