|
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; |
@@ -428,7 +430,7 @@ enum ParseState { |
428 | 430 | /// BomDetected -- "encoding=..." --> XmlDetected |
429 | 431 | /// ``` |
430 | 432 | #[cfg(feature = "encoding")] |
431 | | -#[derive(Clone, Copy)] |
| 433 | +#[derive(Clone, Copy, Debug)] |
432 | 434 | enum EncodingRef { |
433 | 435 | /// Encoding was implicitly assumed to have a specified value. It can be refined |
434 | 436 | /// using BOM or by the XML declaration event (`<?xml encoding=... ?>`) |
@@ -528,73 +530,22 @@ pub struct Reader<R> { |
528 | 530 | } |
529 | 531 |
|
530 | 532 | /// Builder methods |
531 | | -impl<R> Reader<R> { |
| 533 | +impl<R: Read> Reader<Utf8BytesReader<R>> { |
532 | 534 | /// Creates a `Reader` that reads from a given reader. |
533 | 535 | pub fn from_reader(reader: R) -> Self { |
534 | 536 | Self { |
535 | | - reader, |
| 537 | + reader: Utf8BytesReader::new(reader), |
536 | 538 | parser: Parser::default(), |
537 | 539 | } |
538 | 540 | } |
539 | | - |
540 | | - configure_methods!(); |
541 | 541 | } |
542 | 542 |
|
543 | | -/// Getters |
| 543 | +/// Public implementation-independent functionality |
544 | 544 | impl<R> Reader<R> { |
545 | | - /// Consumes `Reader` returning the underlying reader |
546 | | - /// |
547 | | - /// Can be used to compute line and column of a parsing error position |
548 | | - /// |
549 | | - /// # Examples |
550 | | - /// |
551 | | - /// ``` |
552 | | - /// # use pretty_assertions::assert_eq; |
553 | | - /// use std::{str, io::Cursor}; |
554 | | - /// use quick_xml::events::Event; |
555 | | - /// use quick_xml::reader::Reader; |
556 | | - /// |
557 | | - /// let xml = r#"<tag1 att1 = "test"> |
558 | | - /// <tag2><!--Test comment-->Test</tag2> |
559 | | - /// <tag3>Test 2</tag3> |
560 | | - /// </tag1>"#; |
561 | | - /// let mut reader = Reader::from_reader(Cursor::new(xml.as_bytes())); |
562 | | - /// let mut buf = Vec::new(); |
563 | | - /// |
564 | | - /// fn into_line_and_column(reader: Reader<Cursor<&[u8]>>) -> (usize, usize) { |
565 | | - /// let end_pos = reader.buffer_position(); |
566 | | - /// let mut cursor = reader.into_inner(); |
567 | | - /// let s = String::from_utf8(cursor.into_inner()[0..end_pos].to_owned()) |
568 | | - /// .expect("can't make a string"); |
569 | | - /// let mut line = 1; |
570 | | - /// let mut column = 0; |
571 | | - /// for c in s.chars() { |
572 | | - /// if c == '\n' { |
573 | | - /// line += 1; |
574 | | - /// column = 0; |
575 | | - /// } else { |
576 | | - /// column += 1; |
577 | | - /// } |
578 | | - /// } |
579 | | - /// (line, column) |
580 | | - /// } |
581 | | - /// |
582 | | - /// loop { |
583 | | - /// match reader.read_event_into(&mut buf) { |
584 | | - /// Ok(Event::Start(ref e)) => match e.name().as_ref() { |
585 | | - /// b"tag1" | b"tag2" => (), |
586 | | - /// tag => { |
587 | | - /// assert_eq!(b"tag3", tag); |
588 | | - /// assert_eq!((3, 22), into_line_and_column(reader)); |
589 | | - /// break; |
590 | | - /// } |
591 | | - /// }, |
592 | | - /// Ok(Event::Eof) => unreachable!(), |
593 | | - /// _ => (), |
594 | | - /// } |
595 | | - /// buf.clear(); |
596 | | - /// } |
597 | | - /// ``` |
| 545 | + // Configuration setters |
| 546 | + configure_methods!(); |
| 547 | + |
| 548 | + /// Consumes `Reader` returning the underlying reader. |
598 | 549 | pub fn into_inner(self) -> R { |
599 | 550 | self.reader |
600 | 551 | } |
|
0 commit comments