cryptographic_message_syntax/asn1/
rfc4210.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! ASN.1 types defined by RFC 4210.
6
7use bcder::{
8    decode::{Constructed, DecodeError, Source},
9    encode::{self, Values},
10    Tag, Utf8String,
11};
12
13/// PKI free text.
14///
15/// ```ASN.1
16/// PKIFreeText ::= SEQUENCE SIZE (1..MAX) OF UTF8String
17/// ```
18#[derive(Clone, Debug, Eq, PartialEq)]
19pub struct PkiFreeText(Vec<Utf8String>);
20
21impl PkiFreeText {
22    pub fn take_opt_from<S: Source>(
23        cons: &mut Constructed<S>,
24    ) -> Result<Option<Self>, DecodeError<S::Error>> {
25        cons.take_opt_sequence(|cons| Self::from_sequence(cons))
26    }
27
28    pub fn take_from<S: Source>(cons: &mut Constructed<S>) -> Result<Self, DecodeError<S::Error>> {
29        cons.take_sequence(|cons| Self::from_sequence(cons))
30    }
31
32    pub fn from_sequence<S: Source>(
33        cons: &mut Constructed<S>,
34    ) -> Result<Self, DecodeError<S::Error>> {
35        let mut res = vec![];
36
37        while let Some(s) = cons.take_opt_value_if(Tag::UTF8_STRING, |content| {
38            Utf8String::from_content(content)
39        })? {
40            res.push(s);
41        }
42
43        Ok(Self(res))
44    }
45
46    pub fn encode_ref(&self) -> impl Values + '_ {
47        encode::sequence(encode::slice(&self.0, |x| x.clone().encode()))
48    }
49}