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
use super::*;

use crate::error::strings::InvalidGeneralString;
use alloc::{borrow::ToOwned, string::String, vec::Vec};

/// A "general" string containing the `C0` Controls plane, `SPACE`,
/// Basic Latin, `DELETE`, and Latin-1 Supplement characters.
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct GeneralString(Vec<u8>);

impl GeneralString {
    fn is_valid(bytes: &[u8]) -> Result<(), InvalidGeneralString> {
        for byte in bytes {
            let is_in_set = matches!(
                byte,
                | 0x00..=0x1F // C0 Controls (C set)
                | 0x20        // SPACE
                | 0x21..=0x7E // Basic Latin (G set)
                | 0x7F        // DELETE
                | 0xA1..=0xFF // Latin-1 Supplement (G set)
            );

            if !is_in_set {
                return Err(InvalidGeneralString { character: *byte });
            }
        }
        Ok(())
    }

    pub fn from_bytes(bytes: &[u8]) -> Result<Self, InvalidGeneralString> {
        Self::is_valid(bytes)?;
        Ok(Self(bytes.to_owned()))
    }
}

impl TryFrom<Vec<u8>> for GeneralString {
    type Error = InvalidGeneralString;

    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        Self::is_valid(&value)?;
        Ok(Self(value))
    }
}

impl TryFrom<String> for GeneralString {
    type Error = InvalidGeneralString;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::is_valid(value.as_bytes())?;
        Ok(Self(value.into_bytes()))
    }
}

impl core::ops::Deref for GeneralString {
    type Target = Vec<u8>;

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

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

impl AsnType for GeneralString {
    const TAG: Tag = Tag::GENERAL_STRING;
}

impl Decode for GeneralString {
    fn decode_with_tag_and_constraints<D: Decoder>(
        decoder: &mut D,
        tag: Tag,
        constraints: Constraints,
    ) -> Result<Self, D::Error> {
        decoder.decode_general_string(tag, constraints)
    }
}

impl Encode for GeneralString {
    fn encode_with_tag_and_constraints<E: Encoder>(
        &self,
        encoder: &mut E,
        tag: Tag,
        constraints: Constraints,
    ) -> Result<(), E::Error> {
        encoder
            .encode_general_string(tag, constraints, self)
            .map(drop)
    }
}