Skip to content

Commit c5068e7

Browse files
committed
Convert namespace-related structs to str
1 parent 66ccbee commit c5068e7

25 files changed

+470
-598
lines changed

benches/microbenches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ fn attributes(c: &mut Criterion) {
224224
let mut count = criterion::black_box(0);
225225
loop {
226226
match r.read_event() {
227-
Ok(Event::Empty(e)) if e.name() == QName(b"player") => {
227+
Ok(Event::Empty(e)) if e.name() == QName("player") => {
228228
for name in ["num", "status", "avg"] {
229229
if let Some(_attr) = e.try_get_attribute(name).unwrap() {
230230
count += 1

examples/custom_entities.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4141
}
4242
}
4343
Ok(Event::Start(ref e)) => match e.name().as_ref() {
44-
b"test" => {
44+
"test" => {
4545
let attributes = e
4646
.attributes()
4747
.map(|a| {

examples/nested_readers.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn main() -> Result<(), quick_xml::Error> {
2222
loop {
2323
match reader.read_event_into(&mut buf)? {
2424
Event::Start(element) => match element.name().as_ref() {
25-
b"w:tbl" => {
25+
"w:tbl" => {
2626
count += 1;
2727
let mut stats = TableStat {
2828
index: count,
@@ -35,20 +35,17 @@ fn main() -> Result<(), quick_xml::Error> {
3535
skip_buf.clear();
3636
match reader.read_event_into(&mut skip_buf)? {
3737
Event::Start(element) => match element.name().as_ref() {
38-
b"w:tr" => {
38+
"w:tr" => {
3939
stats.rows.push(vec![]);
4040
row_index = stats.rows.len() - 1;
4141
}
42-
b"w:tc" => {
43-
stats.rows[row_index].push(
44-
String::from_utf8(element.name().as_ref().to_vec())
45-
.unwrap(),
46-
);
42+
"w:tc" => {
43+
stats.rows[row_index].push(element.name().as_ref().to_owned());
4744
}
4845
_ => {}
4946
},
5047
Event::End(element) => {
51-
if element.name().as_ref() == b"w:tbl" {
48+
if element.name().as_ref() == "w:tbl" {
5249
found_tables.push(stats);
5350
break;
5451
}

examples/read_buffered.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ fn main() -> Result<(), quick_xml::Error> {
1717
loop {
1818
match reader.read_event_into(&mut buf) {
1919
Ok(Event::Start(ref e)) => {
20-
let name = e.name();
21-
let name = reader.decoder().decode(name.as_ref())?;
22-
println!("read start event {:?}", name.as_ref());
20+
println!("read start event {:?}", e.name().as_ref());
2321
count += 1;
2422
}
2523
Ok(Event::Eof) => break, // exits the loop when reaching end of file

examples/read_texts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn main() {
1010

1111
loop {
1212
match reader.read_event() {
13-
Ok(Event::Start(e)) if e.name().as_ref() == b"tag2" => {
13+
Ok(Event::Start(e)) if e.name().as_ref() == "tag2" => {
1414
// read_text_into for buffered readers not implemented
1515
let txt = reader
1616
.read_text(e.name())

src/de/escape.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Serde `Deserializer` module
22
33
use crate::de::deserialize_bool;
4-
use crate::encoding::Decoder;
54
use crate::errors::serialize::DeError;
65
use crate::escape::unescape;
76
use serde::de::{DeserializeSeed, EnumAccess, VariantAccess, Visitor};
@@ -17,17 +16,15 @@ use std::borrow::Cow;
1716
/// anyway
1817
#[derive(Clone, Debug)]
1918
pub struct EscapedDeserializer<'a> {
20-
decoder: Decoder,
2119
/// Possible escaped value of text/CDATA or attribute value
22-
escaped_value: Cow<'a, [u8]>,
20+
escaped_value: Cow<'a, str>,
2321
/// If `true`, value requires unescaping before using
2422
escaped: bool,
2523
}
2624

2725
impl<'a> EscapedDeserializer<'a> {
28-
pub const fn new(escaped_value: Cow<'a, [u8]>, decoder: Decoder, escaped: bool) -> Self {
26+
pub const fn new(escaped_value: Cow<'a, str>, escaped: bool) -> Self {
2927
EscapedDeserializer {
30-
decoder,
3128
escaped_value,
3229
escaped,
3330
}
@@ -40,7 +37,7 @@ macro_rules! deserialize_num {
4037
where
4138
V: Visitor<'de>,
4239
{
43-
let value = self.decoder.decode(self.escaped_value.as_ref())?.parse()?;
40+
let value = self.escaped_value.as_ref().parse()?;
4441

4542
visitor.$visit(value)
4643
}
@@ -61,14 +58,13 @@ impl<'de, 'a> serde::Deserializer<'de> for EscapedDeserializer<'a> {
6158
where
6259
V: Visitor<'de>,
6360
{
64-
let decoded = self.decoder.decode(&self.escaped_value)?;
6561
if self.escaped {
66-
match unescape(&decoded)? {
62+
match unescape(&self.escaped_value)? {
6763
Cow::Borrowed(s) => visitor.visit_str(s),
6864
Cow::Owned(s) => visitor.visit_string(s),
6965
}
7066
} else {
71-
match decoded {
67+
match self.escaped_value {
7268
Cow::Borrowed(s) => visitor.visit_str(s),
7369
Cow::Owned(s) => visitor.visit_string(s),
7470
}
@@ -104,7 +100,7 @@ impl<'de, 'a> serde::Deserializer<'de> for EscapedDeserializer<'a> {
104100
where
105101
V: Visitor<'de>,
106102
{
107-
deserialize_bool(self.escaped_value.as_ref(), self.decoder, visitor)
103+
deserialize_bool(self.escaped_value.as_ref(), visitor)
108104
}
109105

110106
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>

src/de/map.rs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,13 @@ where
231231

232232
// FIXME: There error positions counted from the start of tag name - need global position
233233
let slice = &self.start.buf;
234-
let decoder = self.de.reader.decoder();
235234

236235
if let Some(a) = self.iter.next(slice).transpose()? {
237236
// try getting map from attributes (key= "value")
238237
let (key, value) = a.into();
239238
self.source = ValueSource::Attribute(value.unwrap_or_default());
240239
seed.deserialize(EscapedDeserializer::new(
241-
Cow::Borrowed(&slice[key]),
242-
decoder,
240+
Cow::Borrowed(std::str::from_utf8(&slice[key]).expect("fixme dalley")),
243241
false,
244242
))
245243
.map(Some)
@@ -269,33 +267,32 @@ where
269267
// }
270268
// TODO: This should be handled by #[serde(flatten)]
271269
// See https://github.com/serde-rs/serde/issues/1905
272-
DeEvent::Start(e) if self.has_value_field && not_in(self.fields, e, decoder)? => {
270+
DeEvent::Start(e) if self.has_value_field && not_in(self.fields, e)? => {
273271
self.source = ValueSource::Content;
274272
seed.deserialize(INNER_VALUE.into_deserializer()).map(Some)
275273
}
276274
DeEvent::Start(e) => {
277275
self.source = ValueSource::Nested;
278-
let key = if let Some(p) = self
279-
.unflatten_fields
280-
.iter()
281-
.position(|f| e.name().as_ref() == &f[UNFLATTEN_PREFIX.len()..])
282-
{
283-
// Used to deserialize elements, like:
284-
// <root>
285-
// <xxx>test</xxx>
286-
// </root>
287-
//
288-
// into
289-
//
290-
// struct Root {
291-
// #[serde(rename = "$unflatten=xxx")]
292-
// xxx: String,
293-
// }
294-
seed.deserialize(self.unflatten_fields.remove(p).into_deserializer())
295-
} else {
296-
let name = Cow::Borrowed(e.local_name().into_inner());
297-
seed.deserialize(EscapedDeserializer::new(name, decoder, false))
298-
};
276+
let key =
277+
if let Some(p) = self.unflatten_fields.iter().position(|f| {
278+
e.name().as_ref().as_bytes() == &f[UNFLATTEN_PREFIX.len()..]
279+
}) {
280+
// Used to deserialize elements, like:
281+
// <root>
282+
// <xxx>test</xxx>
283+
// </root>
284+
//
285+
// into
286+
//
287+
// struct Root {
288+
// #[serde(rename = "$unflatten=xxx")]
289+
// xxx: String,
290+
// }
291+
seed.deserialize(self.unflatten_fields.remove(p).into_deserializer())
292+
} else {
293+
let name = Cow::Borrowed(e.local_name().into_inner());
294+
seed.deserialize(EscapedDeserializer::new(name, false))
295+
};
299296
key.map(Some)
300297
}
301298
// Stop iteration after reaching a closing tag
@@ -616,18 +613,17 @@ where
616613
where
617614
T: DeserializeSeed<'de>,
618615
{
619-
let decoder = self.map.de.reader.decoder();
620616
loop {
621617
break match self.map.de.peek()? {
622618
// If we see a tag that we not interested, skip it
623619
#[cfg(feature = "overlapped-lists")]
624-
DeEvent::Start(e) if !self.filter.is_suitable(e, decoder)? => {
620+
DeEvent::Start(e) if !self.filter.is_suitable(e)? => {
625621
self.map.de.skip()?;
626622
continue;
627623
}
628624
// Stop iteration when list elements ends
629625
#[cfg(not(feature = "overlapped-lists"))]
630-
DeEvent::Start(e) if !self.filter.is_suitable(e, decoder)? => Ok(None),
626+
DeEvent::Start(e) if !self.filter.is_suitable(e)? => Ok(None),
631627

632628
// Stop iteration after reaching a closing tag
633629
DeEvent::End(e) if e.name() == self.map.start.name() => Ok(None),

src/de/mod.rs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -220,30 +220,12 @@ where
220220
}
221221
}
222222

223-
fn deserialize_bool<'de, V>(value: &[u8], decoder: Decoder, visitor: V) -> Result<V::Value, DeError>
223+
fn deserialize_bool<'de, V>(value: &str, visitor: V) -> Result<V::Value, DeError>
224224
where
225225
V: Visitor<'de>,
226226
{
227-
#[cfg(feature = "encoding")]
228-
{
229-
let value = decoder.decode(value)?;
230-
// No need to unescape because valid boolean representations cannot be escaped
231-
str2bool(value.as_ref(), visitor)
232-
}
233-
234-
#[cfg(not(feature = "encoding"))]
235-
{
236-
// No need to unescape because valid boolean representations cannot be escaped
237-
match value {
238-
b"true" | b"1" | b"True" | b"TRUE" | b"t" | b"Yes" | b"YES" | b"yes" | b"y" => {
239-
visitor.visit_bool(true)
240-
}
241-
b"false" | b"0" | b"False" | b"FALSE" | b"f" | b"No" | b"NO" | b"no" | b"n" => {
242-
visitor.visit_bool(false)
243-
}
244-
e => Err(DeError::InvalidBoolean(decoder.decode(e)?.into())),
245-
}
246-
}
227+
// No need to unescape because valid boolean representations cannot be escaped
228+
str2bool(value.as_ref(), visitor)
247229
}
248230

249231
impl<'de, R> Deserializer<'de, R>
@@ -633,10 +615,9 @@ where
633615
{
634616
// Try to go to the next `<tag ...>...</tag>` or `<tag .../>`
635617
if let Some(e) = self.next_start()? {
636-
let name = e.name().as_ref().to_vec();
637-
let map = map::MapAccess::new(self, e, fields)?;
618+
let map = map::MapAccess::new(self, e.clone(), fields)?;
638619
let value = visitor.visit_map(map)?;
639-
self.read_to_end(QName(&name))?;
620+
self.read_to_end(e.name())?;
640621
Ok(value)
641622
} else {
642623
Err(DeError::ExpectedStart)
@@ -1079,7 +1060,7 @@ mod tests {
10791060
// </skip>
10801061
// </root>
10811062
assert_eq!(de.next().unwrap(), Start(BytesStart::new("target")));
1082-
de.read_to_end(QName(b"target")).unwrap();
1063+
de.read_to_end(QName("target")).unwrap();
10831064
assert_eq!(de.read, vec![]);
10841065
assert_eq!(
10851066
de.write,
@@ -1116,7 +1097,7 @@ mod tests {
11161097
assert_eq!(de.write, vec![]);
11171098

11181099
assert_eq!(de.next().unwrap(), Start(BytesStart::new("skip")));
1119-
de.read_to_end(QName(b"skip")).unwrap();
1100+
de.read_to_end(QName("skip")).unwrap();
11201101

11211102
assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
11221103
assert_eq!(de.next().unwrap(), Eof);
@@ -1374,7 +1355,7 @@ mod tests {
13741355
de.next().unwrap(),
13751356
Start(BytesStart::from_content(r#"tag a="1""#, 3))
13761357
);
1377-
assert_eq!(de.read_to_end(QName(b"tag")).unwrap(), ());
1358+
assert_eq!(de.read_to_end(QName("tag")).unwrap(), ());
13781359

13791360
assert_eq!(
13801361
de.next().unwrap(),
@@ -1384,7 +1365,7 @@ mod tests {
13841365
assert_eq!(de.next().unwrap(), End(BytesEnd::new("tag")));
13851366

13861367
assert_eq!(de.next().unwrap(), Start(BytesStart::new("self-closed")));
1387-
assert_eq!(de.read_to_end(QName(b"self-closed")).unwrap(), ());
1368+
assert_eq!(de.read_to_end(QName("self-closed")).unwrap(), ());
13881369

13891370
assert_eq!(de.next().unwrap(), End(BytesEnd::new("root")));
13901371
assert_eq!(de.next().unwrap(), Eof);
@@ -1485,7 +1466,7 @@ mod tests {
14851466
reader.next().unwrap(),
14861467
DeEvent::Start(BytesStart::from_content("item ", 4))
14871468
);
1488-
reader.read_to_end(QName(b"item")).unwrap();
1469+
reader.read_to_end(QName("item")).unwrap();
14891470
assert_eq!(reader.next().unwrap(), DeEvent::Eof);
14901471
}
14911472

src/de/seq.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
use crate::de::{DeError, DeEvent, Deserializer, XmlRead};
2-
use crate::encoding::Decoder;
32
use crate::events::BytesStart;
43
use serde::de::{DeserializeSeed, SeqAccess};
54

6-
/// Check if tag `start` is included in the `fields` list. `decoder` is used to
7-
/// get a string representation of a tag.
5+
/// Check if tag `start` is included in the `fields` list.
86
///
97
/// Returns `true`, if `start` is not in the `fields` list and `false` otherwise.
10-
pub fn not_in(
11-
fields: &'static [&'static str],
12-
start: &BytesStart,
13-
decoder: Decoder,
14-
) -> Result<bool, DeError> {
15-
let tag = decoder.decode(start.name().into_inner())?;
16-
17-
Ok(fields.iter().all(|&field| field != tag.as_ref()))
8+
pub fn not_in(fields: &'static [&'static str], start: &BytesStart) -> Result<bool, DeError> {
9+
Ok(fields.iter().all(|&field| field != start.name().as_ref()))
1810
}
1911

2012
/// A filter that determines, what tags should form a sequence.
@@ -59,10 +51,10 @@ pub enum TagFilter<'de> {
5951
}
6052

6153
impl<'de> TagFilter<'de> {
62-
pub fn is_suitable(&self, start: &BytesStart, decoder: Decoder) -> Result<bool, DeError> {
54+
pub fn is_suitable(&self, start: &BytesStart) -> Result<bool, DeError> {
6355
match self {
6456
Self::Include(n) => Ok(n.name() == start.name()),
65-
Self::Exclude(fields) => not_in(fields, start, decoder),
57+
Self::Exclude(fields) => not_in(fields, start),
6658
}
6759
}
6860
}
@@ -132,18 +124,17 @@ where
132124
where
133125
T: DeserializeSeed<'de>,
134126
{
135-
let decoder = self.de.reader.decoder();
136127
loop {
137128
break match self.de.peek()? {
138129
// If we see a tag that we not interested, skip it
139130
#[cfg(feature = "overlapped-lists")]
140-
DeEvent::Start(e) if !self.filter.is_suitable(e, decoder)? => {
131+
DeEvent::Start(e) if !self.filter.is_suitable(e)? => {
141132
self.de.skip()?;
142133
continue;
143134
}
144135
// Stop iteration when list elements ends
145136
#[cfg(not(feature = "overlapped-lists"))]
146-
DeEvent::Start(e) if !self.filter.is_suitable(e, decoder)? => Ok(None),
137+
DeEvent::Start(e) if !self.filter.is_suitable(e)? => Ok(None),
147138
DeEvent::End(_) => Ok(None),
148139
DeEvent::Eof => Ok(None),
149140

@@ -158,13 +149,7 @@ where
158149
fn test_not_in() {
159150
let tag = BytesStart::new("tag");
160151

161-
assert_eq!(not_in(&[], &tag, Decoder::utf8()).unwrap(), true);
162-
assert_eq!(
163-
not_in(&["no", "such", "tags"], &tag, Decoder::utf8()).unwrap(),
164-
true
165-
);
166-
assert_eq!(
167-
not_in(&["some", "tag", "included"], &tag, Decoder::utf8()).unwrap(),
168-
false
169-
);
152+
assert_eq!(not_in(&[], &tag).unwrap(), true);
153+
assert_eq!(not_in(&["no", "such", "tags"], &tag).unwrap(), true);
154+
assert_eq!(not_in(&["some", "tag", "included"], &tag).unwrap(), false);
170155
}

0 commit comments

Comments
 (0)