rasn/types/strings/
bmp.rs

1use super::*;
2
3use crate::error::strings::InvalidBmpString;
4use alloc::{boxed::Box, string::String, vec::Vec};
5use once_cell::race::OnceBox;
6
7/// A Basic Multilingual Plane (BMP) string, which is a subtype of [`UniversalString`]
8/// containing only the BMP set of characters.
9#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
10pub struct BmpString(Vec<u16>);
11static CHARACTER_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();
12static INDEX_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();
13
14impl BmpString {
15    /// Converts the string into a set of big endian bytes.
16    pub fn to_bytes(&self) -> Vec<u8> {
17        self.0.iter().flat_map(|ch| ch.to_be_bytes()).collect()
18    }
19}
20
21impl StaticPermittedAlphabet for BmpString {
22    const CHARACTER_SET: &'static [u32] = &{
23        let mut array = [0u32; 0xFFFE];
24        let mut i = 0;
25        while i < 0xFFFE {
26            array[i as usize] = i;
27            i += 1;
28        }
29        array
30    };
31
32    fn push_char(&mut self, ch: u32) {
33        debug_assert!(
34            Self::CHARACTER_SET.contains(&ch),
35            "{} not in character set",
36            ch
37        );
38        self.0.push(ch as u16);
39    }
40
41    fn chars(&self) -> Box<dyn Iterator<Item = u32> + '_> {
42        Box::from(self.0.iter().map(|ch| *ch as u32))
43    }
44
45    fn index_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
46        INDEX_MAP.get_or_init(Self::build_index_map)
47    }
48
49    fn character_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
50        CHARACTER_MAP.get_or_init(Self::build_character_map)
51    }
52}
53
54impl TryFrom<String> for BmpString {
55    type Error = InvalidBmpString;
56
57    fn try_from(value: String) -> Result<Self, Self::Error> {
58        Self::try_from(&*value)
59    }
60}
61
62impl TryFrom<&'_ str> for BmpString {
63    type Error = InvalidBmpString;
64    fn try_from(value: &str) -> Result<Self, Self::Error> {
65        let mut vec = Vec::with_capacity(value.len());
66        for ch in value.chars() {
67            if matches!(ch as u16, 0x0..=0xFFFF) {
68                vec.push(ch as u16);
69            } else {
70                return Err(InvalidBmpString {
71                    character: ch as u16,
72                });
73            }
74        }
75
76        Ok(Self(vec))
77    }
78}
79
80impl AsnType for BmpString {
81    const TAG: Tag = Tag::BMP_STRING;
82}
83
84impl Encode for BmpString {
85    fn encode_with_tag_and_constraints<E: Encoder>(
86        &self,
87        encoder: &mut E,
88        tag: Tag,
89        constraints: Constraints,
90    ) -> Result<(), E::Error> {
91        encoder.encode_bmp_string(tag, constraints, self).map(drop)
92    }
93}
94
95impl Decode for BmpString {
96    fn decode_with_tag_and_constraints<D: Decoder>(
97        decoder: &mut D,
98        tag: Tag,
99        constraints: Constraints,
100    ) -> Result<Self, D::Error> {
101        decoder.decode_bmp_string(tag, constraints)
102    }
103}