rasn/types/strings/
numeric.rs1use super::*;
2
3use crate::error::strings::{InvalidNumericString, PermittedAlphabetError};
4use alloc::{borrow::ToOwned, boxed::Box, string::String, vec::Vec};
5use once_cell::race::OnceBox;
6
7#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
9pub struct NumericString(Vec<u8>);
10static CHARACTER_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();
11static INDEX_MAP: OnceBox<alloc::collections::BTreeMap<u32, u32>> = OnceBox::new();
12
13impl NumericString {
14 pub fn from_bytes(bytes: &[u8]) -> Result<Self, InvalidNumericString> {
15 bytes.iter().try_for_each(|byte| {
16 if Self::CHARACTER_SET.contains(&(*byte as u32)) {
17 Ok(())
18 } else {
19 Err(InvalidNumericString { character: *byte })
20 }
21 })?;
22
23 Ok(Self(bytes.to_owned()))
24 }
25
26 pub fn as_bytes(&self) -> &[u8] {
27 &self.0
28 }
29}
30
31impl TryFrom<BitString> for NumericString {
32 type Error = PermittedAlphabetError;
33
34 fn try_from(string: BitString) -> Result<Self, Self::Error> {
35 Self::try_from_permitted_alphabet(&string, None)
36 }
37}
38
39impl TryFrom<String> for NumericString {
40 type Error = InvalidNumericString;
41
42 fn try_from(string: String) -> Result<Self, Self::Error> {
43 Self::from_bytes(string.as_bytes())
44 }
45}
46
47impl TryFrom<&'_ str> for NumericString {
48 type Error = InvalidNumericString;
49
50 fn try_from(string: &str) -> Result<Self, Self::Error> {
51 Self::from_bytes(string.as_bytes())
52 }
53}
54
55impl TryFrom<Vec<u8>> for NumericString {
56 type Error = InvalidNumericString;
57
58 fn try_from(string: Vec<u8>) -> Result<Self, Self::Error> {
59 Self::from_bytes(&string)
60 }
61}
62
63impl AsnType for NumericString {
64 const TAG: Tag = Tag::NUMERIC_STRING;
65}
66
67impl Encode for NumericString {
68 fn encode_with_tag_and_constraints<E: Encoder>(
69 &self,
70 encoder: &mut E,
71 tag: Tag,
72 constraints: Constraints,
73 ) -> Result<(), E::Error> {
74 encoder
75 .encode_numeric_string(tag, constraints, self)
76 .map(drop)
77 }
78}
79
80impl Decode for NumericString {
81 fn decode_with_tag_and_constraints<D: Decoder>(
82 decoder: &mut D,
83 tag: Tag,
84 constraints: Constraints,
85 ) -> Result<Self, D::Error> {
86 decoder.decode_numeric_string(tag, constraints)
87 }
88}
89
90impl StaticPermittedAlphabet for NumericString {
91 const CHARACTER_SET: &'static [u32] = &bytes_to_chars([
92 b' ', b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9',
93 ]);
94
95 fn chars(&self) -> Box<dyn Iterator<Item = u32> + '_> {
96 Box::from(self.0.iter().map(|byte| *byte as u32))
97 }
98
99 fn push_char(&mut self, ch: u32) {
100 debug_assert!(
101 Self::CHARACTER_SET.contains(&ch),
102 "{} not in character set",
103 ch
104 );
105 self.0.push(ch as u8);
106 }
107
108 fn index_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
109 INDEX_MAP.get_or_init(Self::build_index_map)
110 }
111
112 fn character_map() -> &'static alloc::collections::BTreeMap<u32, u32> {
113 CHARACTER_MAP.get_or_init(Self::build_character_map)
114 }
115}