|
3 | 3 | #[cfg(feature = "encoding")]
|
4 | 4 | use encoding_rs::Encoding;
|
5 | 5 |
|
6 |
| -use crate::encoding::Decoder; |
| 6 | +use std::io::Read; |
| 7 | + |
| 8 | +use crate::encoding::{Decoder, DecodingReader}; |
7 | 9 | use crate::errors::{Error, Result};
|
8 | 10 | use crate::events::Event;
|
9 | 11 | use crate::reader::parser::Parser;
|
@@ -289,73 +291,19 @@ pub struct Reader<R> {
|
289 | 291 | }
|
290 | 292 |
|
291 | 293 | /// Builder methods
|
292 |
| -impl<R> Reader<R> { |
| 294 | +impl<R: Read> Reader<DecodingReader<R>> { |
293 | 295 | /// Creates a `Reader` that reads from a given reader.
|
294 | 296 | pub fn from_reader(reader: R) -> Self {
|
295 | 297 | Self {
|
296 |
| - reader, |
| 298 | + reader: DecodingReader::new(reader), |
297 | 299 | parser: Parser::default(),
|
298 | 300 | }
|
299 | 301 | }
|
300 |
| - |
301 |
| - configure_methods!(); |
302 | 302 | }
|
303 | 303 |
|
304 | 304 | /// Getters
|
305 | 305 | impl<R> Reader<R> {
|
306 |
| - /// Consumes `Reader` returning the underlying reader |
307 |
| - /// |
308 |
| - /// Can be used to compute line and column of a parsing error position |
309 |
| - /// |
310 |
| - /// # Examples |
311 |
| - /// |
312 |
| - /// ``` |
313 |
| - /// # use pretty_assertions::assert_eq; |
314 |
| - /// use std::{str, io::Cursor}; |
315 |
| - /// use quick_xml::Reader; |
316 |
| - /// use quick_xml::events::Event; |
317 |
| - /// |
318 |
| - /// let xml = r#"<tag1 att1 = "test"> |
319 |
| - /// <tag2><!--Test comment-->Test</tag2> |
320 |
| - /// <tag3>Test 2</tag3> |
321 |
| - /// </tag1>"#; |
322 |
| - /// let mut reader = Reader::from_reader(Cursor::new(xml.as_bytes())); |
323 |
| - /// let mut buf = Vec::new(); |
324 |
| - /// |
325 |
| - /// fn into_line_and_column(reader: Reader<Cursor<&[u8]>>) -> (usize, usize) { |
326 |
| - /// let end_pos = reader.buffer_position(); |
327 |
| - /// let mut cursor = reader.into_inner(); |
328 |
| - /// let s = String::from_utf8(cursor.into_inner()[0..end_pos].to_owned()) |
329 |
| - /// .expect("can't make a string"); |
330 |
| - /// let mut line = 1; |
331 |
| - /// let mut column = 0; |
332 |
| - /// for c in s.chars() { |
333 |
| - /// if c == '\n' { |
334 |
| - /// line += 1; |
335 |
| - /// column = 0; |
336 |
| - /// } else { |
337 |
| - /// column += 1; |
338 |
| - /// } |
339 |
| - /// } |
340 |
| - /// (line, column) |
341 |
| - /// } |
342 |
| - /// |
343 |
| - /// loop { |
344 |
| - /// match reader.read_event_into(&mut buf) { |
345 |
| - /// Ok(Event::Start(ref e)) => match e.name().as_ref() { |
346 |
| - /// b"tag1" | b"tag2" => (), |
347 |
| - /// tag => { |
348 |
| - /// assert_eq!(b"tag3", tag); |
349 |
| - /// assert_eq!((3, 22), into_line_and_column(reader)); |
350 |
| - /// break; |
351 |
| - /// } |
352 |
| - /// }, |
353 |
| - /// Ok(Event::Eof) => unreachable!(), |
354 |
| - /// _ => (), |
355 |
| - /// } |
356 |
| - /// buf.clear(); |
357 |
| - /// } |
358 |
| - /// ``` |
| 306 | + /// TODO |
359 | 307 | pub fn into_inner(self) -> R {
|
360 | 308 | self.reader
|
361 | 309 | }
|
@@ -394,6 +342,8 @@ impl<R> Reader<R> {
|
394 | 342 | pub fn decoder(&self) -> Decoder {
|
395 | 343 | self.parser.decoder()
|
396 | 344 | }
|
| 345 | + |
| 346 | + configure_methods!(); |
397 | 347 | }
|
398 | 348 |
|
399 | 349 | /// Private sync reading methods
|
|
0 commit comments