rasn/types/
strings.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
mod bit;
mod bmp;
mod constrained;
mod general;
mod ia5;
mod numeric;
mod octet;
mod printable;
mod teletex;
mod visible;

use crate::error::strings::PermittedAlphabetError;
use crate::prelude::*;
use nom::AsBytes;

pub use {
    alloc::string::String as Utf8String,
    bit::{BitStr, BitString, FixedBitString},
    bmp::BmpString,
    general::GeneralString,
    ia5::Ia5String,
    numeric::NumericString,
    octet::{FixedOctetString, OctetString},
    printable::PrintableString,
    teletex::TeletexString,
    visible::VisibleString,
};

pub(crate) use constrained::{
    should_be_indexed, DynConstrainedCharacterString, StaticPermittedAlphabet,
};

const fn bytes_to_chars<const N: usize>(input: [u8; N]) -> [u32; N] {
    let mut chars: [u32; N] = [0; N];

    let mut index = 0;
    while index < N {
        chars[index] = input[index] as u32;
        index += 1;
    }

    chars
}

macro_rules! impl_restricted_core_traits {
    ($(($target:ty, $width:ty)),* $(,)?) => {
    $(
    impl TryFrom<&'_ [u8]> for $target {
        type Error = PermittedAlphabetError;
        fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
            Ok(Self(Self::try_from_slice(value)?))
            }
        }

    impl TryFrom<&'_ str> for $target {
        type Error = PermittedAlphabetError;
        fn try_from(value: &str) -> Result<Self, Self::Error> {
            Ok(Self(Self::try_from_slice(value)?))
        }
    }

    impl TryFrom<alloc::vec::Vec<u8>> for $target {
        type Error = PermittedAlphabetError;
        fn try_from(value: alloc::vec::Vec<u8>) -> Result<Self, Self::Error> {
            Ok(Self(Self::try_from_slice(value.as_slice())?))
        }
    }

    impl TryFrom<alloc::string::String> for $target {
        type Error = PermittedAlphabetError;
        fn try_from(value: alloc::string::String) -> Result<Self, Self::Error> {
            Ok(Self(Self::try_from_slice(&value)?))
        }
    }

    impl TryFrom<bytes::Bytes> for $target {
        type Error = PermittedAlphabetError;

        fn try_from(value: bytes::Bytes) -> Result<Self, Self::Error> {
            Ok(Self(Self::try_from_slice(value.as_ref().as_bytes())?))
        }
    }

    impl TryFrom<BitString> for $target {
        type Error = PermittedAlphabetError;

        fn try_from(string: BitString) -> Result<Self, Self::Error> {
            Self::try_from_permitted_alphabet(string, None)
        }
    }

    impl core::ops::Deref for $target {
        type Target = alloc::vec::Vec<$width>;

        fn deref(&self) -> &Self::Target {
            &self.0
        }
    }

    impl core::ops::DerefMut for $target {
        fn deref_mut(&mut self) -> &mut Self::Target {
            &mut self.0
        }
    }

    )*
};
}
impl_restricted_core_traits!(
    (BmpString, u16),
    (GeneralString, u8),
    (Ia5String, u8),
    (NumericString, u8),
    (PrintableString, u8),
    (TeletexString, u32),
    (VisibleString, u8)
);