rasn/types/
strings.rs

1mod bit;
2mod bmp;
3mod constrained;
4mod general;
5mod graphic;
6mod ia5;
7mod numeric;
8mod octet;
9mod printable;
10mod teletex;
11mod visible;
12
13use crate::error::strings::PermittedAlphabetError;
14use crate::prelude::*;
15use nom::AsBytes;
16
17pub use {
18    alloc::string::String as Utf8String,
19    bit::{BitStr, BitString, FixedBitString},
20    bmp::BmpString,
21    general::GeneralString,
22    graphic::GraphicString,
23    ia5::Ia5String,
24    numeric::NumericString,
25    octet::{FixedOctetString, OctetString},
26    printable::PrintableString,
27    teletex::TeletexString,
28    visible::VisibleString,
29};
30
31pub(crate) use constrained::{
32    should_be_indexed, DynConstrainedCharacterString, StaticPermittedAlphabet,
33};
34
35const fn bytes_to_chars<const N: usize>(input: [u8; N]) -> [u32; N] {
36    let mut chars: [u32; N] = [0; N];
37
38    let mut index = 0;
39    while index < N {
40        chars[index] = input[index] as u32;
41        index += 1;
42    }
43
44    chars
45}
46
47macro_rules! impl_restricted_core_traits {
48    ($(($target:ty, $width:ty)),* $(,)?) => {
49    $(
50    impl TryFrom<&'_ [u8]> for $target {
51        type Error = PermittedAlphabetError;
52        fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
53            Ok(Self(Self::try_from_slice(value)?))
54            }
55        }
56
57    impl TryFrom<&'_ str> for $target {
58        type Error = PermittedAlphabetError;
59        fn try_from(value: &str) -> Result<Self, Self::Error> {
60            Ok(Self(Self::try_from_slice(value)?))
61        }
62    }
63
64    impl TryFrom<alloc::vec::Vec<u8>> for $target {
65        type Error = PermittedAlphabetError;
66        fn try_from(value: alloc::vec::Vec<u8>) -> Result<Self, Self::Error> {
67            Ok(Self(Self::try_from_slice(value.as_slice())?))
68        }
69    }
70
71    impl TryFrom<alloc::string::String> for $target {
72        type Error = PermittedAlphabetError;
73        fn try_from(value: alloc::string::String) -> Result<Self, Self::Error> {
74            Ok(Self(Self::try_from_slice(&value)?))
75        }
76    }
77
78    impl TryFrom<OctetString> for $target {
79        type Error = PermittedAlphabetError;
80
81        fn try_from(value: OctetString) -> Result<Self, Self::Error> {
82            Ok(Self(Self::try_from_slice(value.as_ref().as_bytes())?))
83        }
84    }
85
86    impl TryFrom<BitString> for $target {
87        type Error = PermittedAlphabetError;
88
89        fn try_from(string: BitString) -> Result<Self, Self::Error> {
90            Self::try_from_permitted_alphabet(string, None)
91        }
92    }
93
94    impl core::ops::Deref for $target {
95        type Target = alloc::vec::Vec<$width>;
96
97        fn deref(&self) -> &Self::Target {
98            &self.0
99        }
100    }
101
102    impl core::ops::DerefMut for $target {
103        fn deref_mut(&mut self) -> &mut Self::Target {
104            &mut self.0
105        }
106    }
107
108    )*
109};
110}
111impl_restricted_core_traits!(
112    (BmpString, u16),
113    (GeneralString, u8),
114    (GraphicString, u8),
115    (Ia5String, u8),
116    (NumericString, u8),
117    (PrintableString, u8),
118    (TeletexString, u32),
119    (VisibleString, u8)
120);