1use super::*;
2
3use crate::error::strings::PermittedAlphabetError;
4use alloc::vec::Vec;
5use once_cell::race::OnceBox;
6
7#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
11#[allow(clippy::module_name_repetitions)]
12pub struct PrintableString(pub(super) Vec<u8>);
13static CHARACTER_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();
14static INDEX_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();
15
16impl PrintableString {
17 pub fn from_bytes(bytes: &[u8]) -> Result<Self, PermittedAlphabetError> {
23 Ok(Self(Self::try_from_slice(bytes)?))
24 }
25
26 #[must_use]
28 pub fn as_bytes(&self) -> &[u8] {
29 &self.0
30 }
31}
32
33impl StaticPermittedAlphabet for PrintableString {
34 type T = u8;
35 const CHARACTER_SET: &'static [u32] = &bytes_to_chars([
38 b'A', b'B', b'C', b'D', b'E', b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O',
39 b'P', b'Q', b'R', b'S', b'T', b'U', b'V', b'W', b'X', b'Y', b'Z', b'a', b'b', b'c', b'd',
40 b'e', b'f', b'g', b'h', b'i', b'j', b'k', b'l', b'm', b'n', b'o', b'p', b'q', b'r', b's',
41 b't', b'u', b'v', b'w', b'x', b'y', b'z', b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7',
42 b'8', b'9', b' ', b'\'', b'(', b')', b'+', b',', b'-', b'.', b'/', b':', b'=', b'?',
43 ]);
44 const CHARACTER_SET_NAME: constrained::CharacterSetName =
45 constrained::CharacterSetName::Printable;
46
47 fn push_char(&mut self, ch: u32) {
48 self.0.push(ch as u8);
49 }
50
51 fn chars(&self) -> impl Iterator<Item = u32> + '_ {
52 self.0.iter().map(|&byte| byte as u32)
53 }
54
55 fn index_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
56 INDEX_MAP.get_or_init(Self::build_index_map)
57 }
58
59 fn character_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
60 CHARACTER_MAP.get_or_init(Self::build_character_map)
61 }
62}
63
64impl AsnType for PrintableString {
65 const TAG: Tag = Tag::PRINTABLE_STRING;
66 const IDENTIFIER: Identifier = Identifier::PRINTABLE_STRING;
67}
68
69impl Encode for PrintableString {
70 fn encode_with_tag_and_constraints<'b, E: Encoder<'b>>(
71 &self,
72 encoder: &mut E,
73 tag: Tag,
74 constraints: Constraints,
75 identifier: Identifier,
76 ) -> Result<(), E::Error> {
77 encoder
78 .encode_printable_string(tag, constraints, self, identifier)
79 .map(drop)
80 }
81}
82
83impl Decode for PrintableString {
84 fn decode_with_tag_and_constraints<D: Decoder>(
85 decoder: &mut D,
86 tag: Tag,
87 constraints: Constraints,
88 ) -> Result<Self, D::Error> {
89 decoder.decode_printable_string(tag, constraints)
90 }
91}