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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
//! Attribute.
use std::borrow::Cow;
use std::str::Utf8Error;
use quick_xml::events::attributes::{
AttrError,
Attribute as QuickXMLAttribute,
Attributes,
};
use quick_xml::name::QName;
use quick_xml::Error as QuickXMLError;
use crate::escape::EscapeError;
/// Represent a XML attribute.
#[derive(Debug, Clone, PartialEq)]
pub struct Attribute<'data>
{
inner: QuickXMLAttribute<'data>,
}
impl<'data> Attribute<'data>
{
/// Returns a new `Attribute`.
pub fn new(key: &'data impl AsRef<[u8]>, value: impl Into<Cow<'data, [u8]>>) -> Self
{
Self {
inner: QuickXMLAttribute {
key: QName(key.as_ref()),
value: value.into(),
},
}
}
/// Returns the key.
///
/// # Errors
/// Will return `Err` if the key is invalid UTF-8.
pub fn key(&self) -> Result<&str, Error>
{
std::str::from_utf8(self.key_bytes()).map_err(Error::KeyNotUTF8)
}
/// Returns the key as bytes.
#[must_use]
pub fn key_bytes(&self) -> &[u8]
{
self.inner.key.as_ref()
}
/// Returns the value.
///
/// # Errors
/// Will return `Err` if:
/// - The value is invalid UTF-8
/// - Unescaping the value fails
pub fn value(&self) -> Result<Cow<str>, Error>
{
self.inner.unescape_value().map_err(|err| match err {
QuickXMLError::NonDecodable(Some(utf8_error)) => {
Error::ValueNotUTF8(utf8_error)
}
QuickXMLError::EscapeError(escape_err) => {
Error::UnescapeValueFailed(EscapeError::from_quick_xml(escape_err))
}
_ => {
unreachable!();
}
})
}
/// Returns the value as bytes. They may or may not be escaped.
#[must_use]
pub fn value_bytes(&self) -> &[u8]
{
&self.inner.value
}
}
// Crate-local functions
impl<'a> Attribute<'a>
{
pub(crate) fn from_inner(inner: QuickXMLAttribute<'a>) -> Self
{
Self { inner }
}
}
/// Errors that can be raised when parsing [`Attribute`]s.
///
/// Recovery position in examples shows the position from which parsing of the
/// next attribute will be attempted.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error
{
/// Attribute key was not followed by `=`, position relative to the start of
/// the owning tag is provided.
///
/// Example of input that raises this error:
/// ```xml
/// <tag key another="attribute"/>
/// <!-- ^~~ error position, recovery position (8) -->
/// ```
#[error("Position {0}: attribute key must be directly followed by `=` or space")]
ExpectedEq(usize),
/// Attribute value was not found after `=`, position relative to the start
/// of the owning tag is provided.
///
/// Example of input that raises this error:
/// ```xml
/// <tag key = />
/// <!-- ^~~ error position, recovery position (10) -->
/// ```
///
/// This error can be returned only for the last attribute in the list,
/// because otherwise any content after `=` will be threated as a value.
/// The XML
/// ```xml
/// <tag key = another-key = "value"/>
/// <!-- ^ ^- recovery position (24) -->
/// <!-- '~~ error position (22) -->
/// ```
///
/// will be treated as `Attribute { key = b"key", value = b"another-key" }`
/// and or [`Attribute`] is returned, or [`Error::UnquotedValue`] is raised,
/// depending on the parsing mode.
#[error("Position {0}: `=` must be followed by an attribute value")]
ExpectedValue(usize),
/// Attribute value is not quoted, position relative to the start of the
/// owning tag is provided.
///
/// Example of input that raises this error:
/// ```xml
/// <tag key = value />
/// <!-- ^ ^~~ recovery position (15) -->
/// <!-- '~~ error position (10) -->
/// ```
#[error("Position {0}: attribute value must be enclosed in `\"` or `'`")]
UnquotedValue(usize),
/// Attribute value was not finished with a matching quote, position relative
/// to the start of owning tag and a quote is provided. That position is always
/// a last character in the tag content.
///
/// Example of input that raises this error:
/// ```xml
/// <tag key = "value />
/// <tag key = 'value />
/// <!-- ^~~ error position, recovery position (18) -->
/// ```
///
/// This error can be returned only for the last attribute in the list,
/// because all input was consumed during scanning for a quote.
#[error("Position {0}: missing closing quote `{1}` in attribute value")]
ExpectedQuote(usize, u8),
/// An attribute with the same name was already encountered. Two parameters
/// define (1) the error position relative to the start of the owning tag
/// for a new attribute and (2) the start position of a previously encountered
/// attribute with the same name.
///
/// Example of input that raises this error:
/// ```xml
/// <tag key = 'value' key="value2" attr3='value3' />
/// <!-- ^ ^ ^~~ recovery position (32) -->
/// <!-- | '~~ error position (19) -->
/// <!-- '~~ previous position (4) -->
/// ```
#[error("Position {0}: duplicated attribute, previous declaration at position {1}")]
Duplicated(usize, usize),
/// Attribute key is not valid UTF-8.
#[error("Attribute key is not valid UTF-8")]
KeyNotUTF8(#[source] Utf8Error),
/// Attribute value is not valid UTF-8.
#[error("Attribute value is not valid UTF-8")]
ValueNotUTF8(#[source] Utf8Error),
/// Failed to unescape value.
#[error("Failed to unescape value")]
UnescapeValueFailed(#[source] EscapeError),
}
impl From<AttrError> for Error
{
fn from(attr_err: AttrError) -> Self
{
match attr_err {
AttrError::ExpectedEq(pos) => Self::ExpectedEq(pos),
AttrError::ExpectedValue(pos) => Self::ExpectedValue(pos),
AttrError::UnquotedValue(pos) => Self::UnquotedValue(pos),
AttrError::ExpectedQuote(pos, quote) => Self::ExpectedQuote(pos, quote),
AttrError::Duplicated(pos, same_attr_pos) => {
Self::Duplicated(pos, same_attr_pos)
}
}
}
}
/// Iterates through [`Attribute`]s.
#[derive(Debug)]
pub struct Iter<'a>
{
attrs: Attributes<'a>,
}
impl<'a> Iter<'a>
{
pub(crate) fn new(attrs: Attributes<'a>) -> Self
{
Self { attrs }
}
}
impl<'a> Iterator for Iter<'a>
{
type Item = Result<Attribute<'a>, Error>;
fn next(&mut self) -> Option<Self::Item>
{
let attr = self.attrs.next()?;
Some(attr.map(Attribute::from_inner).map_err(Into::into))
}
}
|