|
1 | 1 | //! Contains high-level interface for a pull-based XML parser. |
2 | 2 |
|
| 3 | +use std::io::Read; |
| 4 | +use std::ops::Range; |
| 5 | + |
3 | 6 | #[cfg(feature = "encoding")] |
4 | 7 | use encoding_rs::Encoding; |
5 | | -use std::ops::Range; |
6 | 8 |
|
7 | | -use crate::encoding::Decoder; |
| 9 | +use crate::encoding::{Decoder, Utf8BytesReader}; |
8 | 10 | use crate::errors::{Error, Result}; |
9 | 11 | use crate::events::Event; |
10 | 12 | use crate::reader::parser::Parser; |
@@ -359,7 +361,7 @@ enum ParseState { |
359 | 361 | /// BomDetected -- "encoding=..." --> XmlDetected |
360 | 362 | /// ``` |
361 | 363 | #[cfg(feature = "encoding")] |
362 | | -#[derive(Clone, Copy)] |
| 364 | +#[derive(Clone, Copy, Debug)] |
363 | 365 | enum EncodingRef { |
364 | 366 | /// Encoding was implicitly assumed to have a specified value. It can be refined |
365 | 367 | /// using BOM or by the XML declaration event (`<?xml encoding=... ?>`) |
@@ -459,73 +461,22 @@ pub struct Reader<R> { |
459 | 461 | } |
460 | 462 |
|
461 | 463 | /// Builder methods |
462 | | -impl<R> Reader<R> { |
| 464 | +impl<R: Read> Reader<Utf8BytesReader<R>> { |
463 | 465 | /// Creates a `Reader` that reads from a given reader. |
464 | 466 | pub fn from_reader(reader: R) -> Self { |
465 | 467 | Self { |
466 | | - reader, |
| 468 | + reader: Utf8BytesReader::new(reader), |
467 | 469 | parser: Parser::default(), |
468 | 470 | } |
469 | 471 | } |
470 | | - |
471 | | - configure_methods!(); |
472 | 472 | } |
473 | 473 |
|
474 | | -/// Getters |
| 474 | +/// Public implementation-independent functionality |
475 | 475 | impl<R> Reader<R> { |
476 | | - /// Consumes `Reader` returning the underlying reader |
477 | | - /// |
478 | | - /// Can be used to compute line and column of a parsing error position |
479 | | - /// |
480 | | - /// # Examples |
481 | | - /// |
482 | | - /// ``` |
483 | | - /// # use pretty_assertions::assert_eq; |
484 | | - /// use std::{str, io::Cursor}; |
485 | | - /// use quick_xml::events::Event; |
486 | | - /// use quick_xml::reader::Reader; |
487 | | - /// |
488 | | - /// let xml = r#"<tag1 att1 = "test"> |
489 | | - /// <tag2><!--Test comment-->Test</tag2> |
490 | | - /// <tag3>Test 2</tag3> |
491 | | - /// </tag1>"#; |
492 | | - /// let mut reader = Reader::from_reader(Cursor::new(xml.as_bytes())); |
493 | | - /// let mut buf = Vec::new(); |
494 | | - /// |
495 | | - /// fn into_line_and_column(reader: Reader<Cursor<&[u8]>>) -> (usize, usize) { |
496 | | - /// let end_pos = reader.buffer_position(); |
497 | | - /// let mut cursor = reader.into_inner(); |
498 | | - /// let s = String::from_utf8(cursor.into_inner()[0..end_pos].to_owned()) |
499 | | - /// .expect("can't make a string"); |
500 | | - /// let mut line = 1; |
501 | | - /// let mut column = 0; |
502 | | - /// for c in s.chars() { |
503 | | - /// if c == '\n' { |
504 | | - /// line += 1; |
505 | | - /// column = 0; |
506 | | - /// } else { |
507 | | - /// column += 1; |
508 | | - /// } |
509 | | - /// } |
510 | | - /// (line, column) |
511 | | - /// } |
512 | | - /// |
513 | | - /// loop { |
514 | | - /// match reader.read_event_into(&mut buf) { |
515 | | - /// Ok(Event::Start(ref e)) => match e.name().as_ref() { |
516 | | - /// b"tag1" | b"tag2" => (), |
517 | | - /// tag => { |
518 | | - /// assert_eq!(b"tag3", tag); |
519 | | - /// assert_eq!((3, 22), into_line_and_column(reader)); |
520 | | - /// break; |
521 | | - /// } |
522 | | - /// }, |
523 | | - /// Ok(Event::Eof) => unreachable!(), |
524 | | - /// _ => (), |
525 | | - /// } |
526 | | - /// buf.clear(); |
527 | | - /// } |
528 | | - /// ``` |
| 476 | + // Configuration setters |
| 477 | + configure_methods!(); |
| 478 | + |
| 479 | + /// Consumes `Reader` returning the underlying reader. |
529 | 480 | pub fn into_inner(self) -> R { |
530 | 481 | self.reader |
531 | 482 | } |
@@ -1612,7 +1563,7 @@ mod test { |
1612 | 1563 | /// character should be stripped for consistency |
1613 | 1564 | #[$test] |
1614 | 1565 | $($async)? fn bom_from_reader() { |
1615 | | - let mut reader = Reader::from_reader("\u{feff}\u{feff}".as_bytes()); |
| 1566 | + let mut reader = Reader::from_str("\u{feff}\u{feff}"); |
1616 | 1567 |
|
1617 | 1568 | assert_eq!( |
1618 | 1569 | reader.$read_event($buf) $(.$await)? .unwrap(), |
|
0 commit comments