http1/ascii/
mod.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
mod char;
pub use char::*;

use core::str;
use std::{
    borrow::Cow,
    fmt::{Debug, Display},
    ops::Deref,
    str::FromStr,
};

#[derive(Debug)]
pub struct InvalidAsciiStr(String);

impl std::error::Error for InvalidAsciiStr {}

impl Display for InvalidAsciiStr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "invalid ascii string: {:?}", self.0)
    }
}

#[repr(transparent)]
#[derive(Clone, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct AsciiString(String);

impl AsciiString {
    pub fn new() -> Self {
        Default::default()
    }

    pub fn from_string(s: String) -> Result<Self, InvalidAsciiStr> {
        if s.is_ascii() {
            Ok(AsciiString(s))
        } else {
            Err(InvalidAsciiStr(s))
        }
    }

    pub fn from_ascii(s: AsciiStr<'_>) -> Self {
        AsciiString(s.into_string())
    }

    pub fn push(&mut self, ch: AsciiChar) {
        self.0.push(ch.to_char());
    }

    pub fn push_str(&mut self, s: AsciiStr<'_>) {
        self.0.push_str(&s);
    }

    pub fn insert(&mut self, idx: usize, ch: AsciiChar) {
        self.0.insert(idx, ch.to_char());
    }

    pub fn clear(&mut self) {
        self.0.clear();
    }

    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    pub fn into_string(self) -> String {
        self.0
    }

    pub fn into_bytes(self) -> Vec<u8> {
        self.0.into_bytes()
    }
}

impl FromStr for AsciiString {
    type Err = InvalidAsciiStr;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let s = AsciiStr::new(s)?;
        Ok(AsciiString::from_ascii(s))
    }
}

impl TryFrom<String> for AsciiString {
    type Error = InvalidAsciiStr;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        AsciiString::from_string(value)
    }
}

impl<'a> TryFrom<&'a str> for AsciiString {
    type Error = InvalidAsciiStr;

    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
        let s = value.try_into()?;
        Ok(AsciiString::from_ascii(s))
    }
}

impl<'a> TryFrom<Cow<'a, str>> for AsciiString {
    type Error = InvalidAsciiStr;

    fn try_from(value: Cow<'a, str>) -> Result<Self, Self::Error> {
        let s = AsciiStr::new(&value)?;
        Ok(AsciiString::from_ascii(s))
    }
}

impl Deref for AsciiString {
    type Target = str;

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

impl Debug for AsciiString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.0)
    }
}

impl Display for AsciiString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

#[repr(transparent)]
#[derive(Clone, Copy, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct AsciiStr<'a>(&'a str);

impl<'a> AsciiStr<'a> {
    pub fn new(s: &'a str) -> Result<Self, InvalidAsciiStr> {
        if s.is_ascii() {
            Ok(AsciiStr(s))
        } else {
            Err(InvalidAsciiStr(s.to_owned()))
        }
    }

    pub fn into_string(self) -> String {
        self.0.to_owned()
    }

    pub fn as_str(&self) -> &str {
        self.0
    }
}

impl<'a> Deref for AsciiStr<'a> {
    type Target = str;

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

impl<'a> Debug for AsciiStr<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.0)
    }
}

impl<'a> Display for AsciiStr<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<'a> TryFrom<&'a str> for AsciiStr<'a> {
    type Error = InvalidAsciiStr;

    fn try_from(value: &'a str) -> Result<Self, Self::Error> {
        AsciiStr::new(value)
    }
}