Skip to content

Commit 2ba3aa9

Browse files
committed
Remove quick_xml::encoding::Decoder and Reader::decoder()
As quick-xml will pre-decode everything, it is unnecessary.
1 parent 1103fef commit 2ba3aa9

11 files changed

+35
-105
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ async-tokio = ["tokio"]
8484
## let mut buf = Vec::new();
8585
## let mut unsupported = false;
8686
## loop {
87-
## if !reader.decoder().encoding().is_ascii_compatible() {
87+
## if !reader.encoding().is_ascii_compatible() {
8888
## unsupported = true;
8989
## break;
9090
## }

Changelog.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919

2020
### Misc Changes
2121

22-
[#441]: https://github.com/tafia/quick-xml/issues/441
22+
- [#441]: `Reader::decoder()` removed as it is no longer necessary (`Reader` already
23+
decodes everything for you). `Reader::encoding()` is provided to make the current
24+
encoding accessible as it was before.
25+
26+
[#441]: https://github.com/tafia/quick-xml/pull/441
2327

2428
## 0.24.0 -- 2022-08-28
2529

src/encoding.rs

-68
Original file line numberDiff line numberDiff line change
@@ -75,74 +75,6 @@ impl<R: io::Read> io::BufRead for Utf8BytesReader<R> {
7575
}
7676
}
7777

78-
/// Decoder of byte slices into strings.
79-
///
80-
/// If feature `encoding` is enabled, this encoding taken from the `"encoding"`
81-
/// XML declaration or assumes UTF-8, if XML has no <?xml ?> declaration, encoding
82-
/// key is not defined or contains unknown encoding.
83-
///
84-
/// The library supports any UTF-8 compatible encodings that crate `encoding_rs`
85-
/// is supported. [*UTF-16 and ISO-2022-JP are not supported at the present*][utf16].
86-
///
87-
/// If feature `encoding` is disabled, the decoder is always UTF-8 decoder:
88-
/// any XML declarations are ignored.
89-
///
90-
/// [utf16]: https://github.com/tafia/quick-xml/issues/158
91-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
92-
pub struct Decoder {
93-
#[cfg(feature = "encoding")]
94-
pub(crate) encoding: &'static Encoding,
95-
}
96-
97-
impl Decoder {
98-
pub(crate) fn utf8() -> Self {
99-
Decoder {
100-
#[cfg(feature = "encoding")]
101-
encoding: UTF_8,
102-
}
103-
}
104-
105-
#[cfg(all(test, feature = "encoding", feature = "serialize"))]
106-
pub(crate) fn utf16() -> Self {
107-
Decoder { encoding: UTF_16LE }
108-
}
109-
}
110-
111-
impl Decoder {
112-
/// Returns the `Reader`s encoding.
113-
///
114-
/// This encoding will be used by [`decode`].
115-
///
116-
/// [`decode`]: Self::decode
117-
#[cfg(feature = "encoding")]
118-
pub const fn encoding(&self) -> &'static Encoding {
119-
self.encoding
120-
}
121-
122-
/// ## Without `encoding` feature
123-
///
124-
/// Decodes an UTF-8 slice regardless of XML declaration and ignoring BOM
125-
/// if it is present in the `bytes`.
126-
///
127-
/// ## With `encoding` feature
128-
///
129-
/// Decodes specified bytes using encoding, declared in the XML, if it was
130-
/// declared there, or UTF-8 otherwise, and ignoring BOM if it is present
131-
/// in the `bytes`.
132-
///
133-
/// ----
134-
/// Returns an error in case of malformed sequences in the `bytes`.
135-
pub fn decode<'b>(&self, bytes: &'b [u8]) -> Result<Cow<'b, str>> {
136-
#[cfg(not(feature = "encoding"))]
137-
let decoded = Ok(Cow::Borrowed(std::str::from_utf8(bytes)?));
138-
139-
#[cfg(feature = "encoding")]
140-
let decoded = decode(bytes, self.encoding);
141-
142-
decoded
143-
}
144-
}
145-
14678
/// Decodes the provided bytes using the specified encoding.
14779
///
14880
/// Returns an error in case of malformed or non-representable sequences in the `bytes`.

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ pub mod utils;
6565
pub mod writer;
6666

6767
// reexports
68-
pub use crate::encoding::Decoder;
6968
#[cfg(feature = "serialize")]
7069
pub use crate::errors::serialize::DeError;
7170
pub use crate::errors::{Error, Result};

src/reader/buffered_reader.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,9 @@ mod test {
437437
Reader::from_reader(b"\xFF\xFE<?xml encoding='windows-1251'?>".as_ref());
438438
let mut buf = Vec::new();
439439

440-
assert_eq!(reader.decoder().encoding(), UTF_8);
440+
assert_eq!(reader.encoding(), UTF_8);
441441
reader.read_event_into(&mut buf).unwrap();
442-
assert_eq!(reader.decoder().encoding(), WINDOWS_1251);
442+
assert_eq!(reader.encoding(), WINDOWS_1251);
443443

444444
assert_eq!(reader.read_event_into(&mut buf).unwrap(), Event::Eof);
445445
}
@@ -452,12 +452,12 @@ mod test {
452452
);
453453
let mut buf = Vec::new();
454454

455-
assert_eq!(reader.decoder().encoding(), UTF_8);
455+
assert_eq!(reader.encoding(), UTF_8);
456456
reader.read_event_into(&mut buf).unwrap();
457-
assert_eq!(reader.decoder().encoding(), UTF_16LE);
457+
assert_eq!(reader.encoding(), UTF_16LE);
458458

459459
reader.read_event_into(&mut buf).unwrap();
460-
assert_eq!(reader.decoder().encoding(), UTF_16LE);
460+
assert_eq!(reader.encoding(), UTF_16LE);
461461

462462
assert_eq!(reader.read_event_into(&mut buf).unwrap(), Event::Eof);
463463
}

src/reader/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::ops::Range;
66
#[cfg(feature = "encoding")]
77
use encoding_rs::{Encoding, UTF_8};
88

9-
use crate::encoding::{Decoder, Utf8BytesReader};
9+
use crate::encoding::Utf8BytesReader;
1010
use crate::errors::{Error, Result};
1111
use crate::events::Event;
1212
use crate::reader::parser::Parser;
@@ -283,8 +283,7 @@ macro_rules! read_to_end {
283283
depth -= 1;
284284
}
285285
Ok(Event::Eof) => {
286-
let name = $self.decoder().decode($end.as_ref().as_bytes());
287-
return Err(Error::UnexpectedEof(format!("</{:?}>", name)));
286+
return Err(Error::UnexpectedEof(format!("</{:?}>", $end.as_ref())));
288287
}
289288
_ => (),
290289
}
@@ -529,16 +528,17 @@ impl<R> Reader<R> {
529528
}
530529
}
531530

532-
/// Get the decoder, used to decode bytes, read by this reader, to the strings.
531+
/// Get the encoding this reader is currently using to decode strings.
533532
///
534533
/// If `encoding` feature is enabled, the used encoding may change after
535534
/// parsing the XML declaration, otherwise encoding is fixed to UTF-8.
536535
///
537536
/// If `encoding` feature is enabled and no encoding is specified in declaration,
538537
/// defaults to UTF-8.
538+
#[cfg(feature = "encoding")]
539539
#[inline]
540-
pub const fn decoder(&self) -> Decoder {
541-
self.parser.decoder()
540+
pub const fn encoding(&self) -> &'static Encoding {
541+
self.parser.encoding.encoding()
542542
}
543543
}
544544

src/reader/ns_reader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ impl<'i> NsReader<&'i [u8]> {
780780
///
781781
/// ```ignore
782782
/// let span = reader.read_to_end(end)?;
783-
/// let text = reader.decoder().decode(&reader.inner_slice[span]);
783+
/// let text = std::str::from_utf8(&reader.inner_slice[span]);
784784
/// ```
785785
///
786786
/// # Examples

src/reader/parser.rs

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#[cfg(feature = "encoding")]
22
use encoding_rs::UTF_8;
33

4-
use crate::encoding::Decoder;
54
use crate::errors::{Error, Result};
65
use crate::events::{BytesCData, BytesDecl, BytesEnd, BytesStart, BytesText, Event};
76
#[cfg(feature = "encoding")]
@@ -55,6 +54,16 @@ pub(super) struct Parser {
5554

5655
#[cfg(feature = "encoding")]
5756
/// Reference to the encoding used to read an XML
57+
///
58+
/// If feature `encoding` is enabled, this encoding is taken from the `"encoding"`
59+
/// XML declaration or assumes UTF-8, if XML has no <?xml ?> declaration, encoding
60+
/// key is not defined or contains unknown encoding.
61+
///
62+
/// The library supports any UTF-8 compatible encodings that crate `encoding_rs`
63+
/// is supported. [*UTF-16 and ISO-2022-JP are not supported at the present*][utf16].
64+
///
65+
/// If feature `encoding` is disabled, the decoder is always UTF-8 decoder:
66+
/// any XML declarations are ignored.
5867
pub encoding: EncodingRef,
5968
}
6069

@@ -226,20 +235,6 @@ impl Parser {
226235
.split_off(self.opened_starts.pop().unwrap());
227236
Ok(Event::End(BytesEnd::new(name)))
228237
}
229-
230-
/// Get the decoder, used to decode bytes, read by this reader, to the strings.
231-
///
232-
/// If `encoding` feature is enabled, the used encoding may change after
233-
/// parsing the XML declaration, otherwise encoding is fixed to UTF-8.
234-
///
235-
/// If `encoding` feature is enabled and no encoding is specified in declaration,
236-
/// defaults to UTF-8.
237-
pub const fn decoder(&self) -> Decoder {
238-
Decoder {
239-
#[cfg(feature = "encoding")]
240-
encoding: self.encoding.encoding(),
241-
}
242-
}
243238
}
244239

245240
impl Default for Parser {

src/reader/slice_reader.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl<'a> Reader<&'a [u8]> {
158158
///
159159
/// ```ignore
160160
/// let span = reader.read_to_end(end)?;
161-
/// let text = reader.decoder().decode(&reader.inner_slice[span]);
161+
/// let text = std::str::from_utf8(&reader.inner_slice[span]);
162162
/// ```
163163
///
164164
/// # Examples
@@ -211,7 +211,7 @@ impl<'a> Reader<&'a [u8]> {
211211
let buffer = self.reader;
212212
let span = self.read_to_end(end)?;
213213

214-
self.decoder().decode(&buffer[0..span.len()])
214+
Ok(Cow::Borrowed(std::str::from_utf8(&buffer[0..span.len()])?))
215215
}
216216
}
217217

@@ -362,9 +362,9 @@ mod test {
362362
fn str_always_has_utf8() {
363363
let mut reader = Reader::from_str("<?xml encoding='UTF-16'?>");
364364

365-
assert_eq!(reader.decoder().encoding(), UTF_8);
365+
assert_eq!(reader.encoding(), UTF_8);
366366
reader.read_event().unwrap();
367-
assert_eq!(reader.decoder().encoding(), UTF_8);
367+
assert_eq!(reader.encoding(), UTF_8);
368368

369369
assert_eq!(reader.read_event().unwrap(), Event::Eof);
370370
}

tests/encodings.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ mod detect {
5959
let mut r = Reader::from_reader(
6060
include_bytes!(concat!("documents/encoding/", $file, ".xml")).as_ref(),
6161
);
62-
assert_eq!(r.decoder().encoding(), UTF_8);
62+
assert_eq!(r.encoding(), UTF_8);
6363

6464
let mut buf = Vec::new();
6565
loop {
6666
match dbg!(r.read_event_into(&mut buf).unwrap()) {
6767
Event::Eof => break,
6868
_ => {}
6969
}
70-
assert_eq!(r.decoder().encoding(), $enc);
70+
assert_eq!(r.encoding(), $enc);
7171
buf.clear();
7272
$($break)?
7373
}

tests/xmlrs_reader_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ fn test_bytes(input: &[u8], output: &[u8], trim: bool) {
394394
loop {
395395
let line = match reader.read_resolved_event_into(&mut Vec::new()) {
396396
Ok((_, Event::Decl(e))) => {
397-
// Declaration could change decoder
397+
// Declaration could change encoding
398398
let version = e.version().unwrap();
399399
let encoding = e.encoding().unwrap().unwrap();
400400
format!("StartDocument({}, {})", version, encoding)

0 commit comments

Comments
 (0)