Skip to content

Commit

Permalink
Move Parser, ElementParser and PiParser to the new module `pars…
Browse files Browse the repository at this point in the history
…er`.
  • Loading branch information
Mingun committed Jul 6, 2024
1 parent 0d99a16 commit d387ed7
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 34 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

### Misc Changes

- [#780]: `reader::Parser`, `reader::ElementParser` and `reader::PiParser` moved to the new module `parser`.

[#780]: https://github.com/tafia/quick-xml/pull/780
[#781]: https://github.com/tafia/quick-xml/pull/781


Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub mod errors;
pub mod escape;
pub mod events;
pub mod name;
pub mod parser;
pub mod reader;
#[cfg(feature = "serialize")]
pub mod se;
Expand Down
4 changes: 2 additions & 2 deletions src/reader/element.rs → src/parser/element.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Contains a parser for an XML element.
use crate::errors::SyntaxError;
use crate::reader::Parser;
use crate::parser::Parser;

/// A parser that search a `>` symbol in the slice outside of quoted regions.
///
Expand All @@ -25,7 +25,7 @@ use crate::reader::Parser;
///
/// ```
/// # use pretty_assertions::assert_eq;
/// use quick_xml::reader::{ElementParser, Parser};
/// use quick_xml::parser::{ElementParser, Parser};
///
/// let mut parser = ElementParser::default();
///
Expand Down
29 changes: 29 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Contains low-level parsers of different XML pieces.
use crate::errors::SyntaxError;

mod element;
mod pi;

pub use element::ElementParser;
pub use pi::PiParser;

/// Used to decouple reading of data from data source and parsing XML structure from it.
/// This is a state preserved between getting chunks of bytes from the reader.
///
/// This trait is implemented for every parser that processes piece of XML grammar.
pub trait Parser {
/// Process new data and try to determine end of the parsed thing.
///
/// Returns position of the end of thing in `bytes` in case of successful search
/// and `None` otherwise.
///
/// # Parameters
/// - `bytes`: a slice to find the end of a thing.
/// Should contain text in ASCII-compatible encoding
fn feed(&mut self, bytes: &[u8]) -> Option<usize>;

/// Returns parse error produced by this parser in case of reaching end of
/// input without finding the end of a parsed thing.
fn eof_error() -> SyntaxError;
}
4 changes: 2 additions & 2 deletions src/reader/pi.rs → src/parser/pi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Contains a parser for an XML processing instruction.
use crate::errors::SyntaxError;
use crate::reader::Parser;
use crate::parser::Parser;

/// A parser that search a `?>` sequence in the slice.
///
Expand All @@ -19,7 +19,7 @@ use crate::reader::Parser;
///
/// ```
/// # use pretty_assertions::assert_eq;
/// use quick_xml::reader::{Parser, PiParser};
/// use quick_xml::parser::{Parser, PiParser};
///
/// let mut parser = PiParser::default();
///
Expand Down
5 changes: 2 additions & 3 deletions src/reader/async_tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use tokio::io::{self, AsyncBufRead, AsyncBufReadExt};
use crate::errors::{Error, Result, SyntaxError};
use crate::events::Event;
use crate::name::{QName, ResolveResult};
use crate::parser::{ElementParser, Parser, PiParser};
use crate::reader::buffered_reader::impl_buffered_source;
use crate::reader::{
BangType, ElementParser, NsReader, ParseState, Parser, PiParser, ReadTextResult, Reader, Span,
};
use crate::reader::{BangType, NsReader, ParseState, ReadTextResult, Reader, Span};
use crate::utils::is_whitespace;

/// A struct for read XML asynchronously from an [`AsyncBufRead`].
Expand Down
3 changes: 2 additions & 1 deletion src/reader/buffered_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use std::path::Path;
use crate::errors::{Error, Result};
use crate::events::Event;
use crate::name::QName;
use crate::reader::{BangType, Parser, ReadTextResult, Reader, Span, XmlSource};
use crate::parser::Parser;
use crate::reader::{BangType, ReadTextResult, Reader, Span, XmlSource};
use crate::utils::is_whitespace;

macro_rules! impl_buffered_source {
Expand Down
27 changes: 2 additions & 25 deletions src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::ops::Range;
use crate::encoding::Decoder;
use crate::errors::{Error, Result, SyntaxError};
use crate::events::Event;
use crate::parser::{ElementParser, Parser, PiParser};
use crate::reader::state::ReaderState;

/// A struct that holds a parser configuration.
Expand Down Expand Up @@ -449,15 +450,11 @@ macro_rules! read_to_end {
#[cfg(feature = "async-tokio")]
mod async_tokio;
mod buffered_reader;
mod element;
mod ns_reader;
mod pi;
mod slice_reader;
mod state;

pub use element::ElementParser;
pub use ns_reader::NsReader;
pub use pi::PiParser;

/// Range of input in bytes, that corresponds to some piece of XML
pub type Span = Range<u64>;
Expand Down Expand Up @@ -790,26 +787,6 @@ enum ReadTextResult<'r, B> {
Err(io::Error),
}

/// Used to decouple reading of data from data source and parsing XML structure from it.
/// This is a state preserved between getting chunks of bytes from the reader.
///
/// This trait is implemented for every parser that processes piece of XML grammar.
pub trait Parser {
/// Process new data and try to determine end of the parsed thing.
///
/// Returns position of the end of thing in `bytes` in case of successful search
/// and `None` otherwise.
///
/// # Parameters
/// - `bytes`: a slice to find the end of a thing.
/// Should contain text in ASCII-compatible encoding
fn feed(&mut self, bytes: &[u8]) -> Option<usize>;

/// Returns parse error produced by this parser in case of reaching end of
/// input without finding the end of a parsed thing.
fn eof_error() -> SyntaxError;
}

/// Represents an input for a reader that can return borrowed data.
///
/// There are two implementors of this trait: generic one that read data from
Expand Down Expand Up @@ -1541,7 +1518,7 @@ mod test {
mod read_element {
use super::*;
use crate::errors::{Error, SyntaxError};
use crate::reader::ElementParser;
use crate::parser::ElementParser;
use crate::utils::Bytes;
use pretty_assertions::assert_eq;

Expand Down
3 changes: 2 additions & 1 deletion src/reader/slice_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use encoding_rs::{Encoding, UTF_8};
use crate::errors::{Error, Result};
use crate::events::Event;
use crate::name::QName;
use crate::reader::{BangType, Parser, ReadTextResult, Reader, Span, XmlSource};
use crate::parser::Parser;
use crate::reader::{BangType, ReadTextResult, Reader, Span, XmlSource};
use crate::utils::is_whitespace;

/// This is an implementation for reading from a `&[u8]` as underlying byte stream.
Expand Down

0 comments on commit d387ed7

Please sign in to comment.