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
177
178
179
180
181
pub mod private;
pub mod transform;
pub mod unicode;
pub use private::Private;
pub use transform::Transform;
pub use unicode::Unicode;
use std::iter::Peekable;
use crate::parser::ParserError;
#[derive(Debug, PartialEq, Eq, Clone, Hash, PartialOrd, Ord, Copy)]
pub enum ExtensionType {
Transform,
Unicode,
Private,
}
impl ExtensionType {
pub fn from_byte(key: u8) -> Result<Self, ParserError> {
let key = key.to_ascii_lowercase();
match key {
b'u' => Ok(Self::Unicode),
b't' => Ok(Self::Transform),
b'x' => Ok(Self::Private),
_ => Err(ParserError::InvalidExtension),
}
}
}
#[derive(Debug, Default, PartialEq, Eq, Clone, Hash, PartialOrd, Ord)]
pub struct Extensions {
pub unicode: Unicode,
pub transform: Transform,
pub private: Private,
}
impl Extensions {
#[inline]
pub const fn new() -> Self {
Self {
unicode: Unicode::new(),
transform: Transform::new(),
private: Private::new(),
}
}
pub fn is_empty(&self) -> bool {
self.unicode.is_empty() && self.transform.is_empty() && self.private.is_empty()
}
pub(crate) fn try_from_iter<'a>(
iter: &mut Peekable<impl Iterator<Item = &'a [u8]>>,
) -> Result<Self, ParserError> {
let mut unicode = None;
let mut transform = None;
let mut private = None;
let mut st = iter.next();
while let Some(subtag) = st {
match subtag.get(0).map(|b| ExtensionType::from_byte(*b)) {
Some(Ok(ExtensionType::Unicode)) => {
unicode = Some(Unicode::try_from_iter(iter)?);
}
Some(Ok(ExtensionType::Transform)) => {
transform = Some(Transform::try_from_iter(iter)?);
}
Some(Ok(ExtensionType::Private)) => {
private = Some(Private::try_from_iter(iter)?);
}
None => {}
_ => return Err(ParserError::InvalidExtension),
}
st = iter.next();
}
Ok(Self {
unicode: unicode.unwrap_or_default(),
transform: transform.unwrap_or_default(),
private: private.unwrap_or_default(),
})
}
}
impl std::fmt::Display for Extensions {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
writeable::Writeable::write_to(self, f)
}
}
impl writeable::Writeable for Extensions {
fn write_to<W: std::fmt::Write + ?Sized>(&self, sink: &mut W) -> std::fmt::Result {
writeable::Writeable::write_to(&self.transform, sink)?;
writeable::Writeable::write_to(&self.unicode, sink)?;
writeable::Writeable::write_to(&self.private, sink)?;
Ok(())
}
fn write_len(&self) -> writeable::LengthHint {
let mut result = writeable::LengthHint::Exact(0);
result += writeable::Writeable::write_len(&self.transform);
result += writeable::Writeable::write_len(&self.unicode);
result += writeable::Writeable::write_len(&self.private);
result
}
}