From d387ed7416af516034b1952375ac01ee7a0a513a Mon Sep 17 00:00:00 2001 From: Mingun Date: Sun, 23 Jun 2024 12:01:21 +0500 Subject: [PATCH] Move `Parser`, `ElementParser` and `PiParser` to the new module `parser`. --- Changelog.md | 3 +++ src/lib.rs | 1 + src/{reader => parser}/element.rs | 4 ++-- src/parser/mod.rs | 29 +++++++++++++++++++++++++++++ src/{reader => parser}/pi.rs | 4 ++-- src/reader/async_tokio.rs | 5 ++--- src/reader/buffered_reader.rs | 3 ++- src/reader/mod.rs | 27 ++------------------------- src/reader/slice_reader.rs | 3 ++- 9 files changed, 45 insertions(+), 34 deletions(-) rename src/{reader => parser}/element.rs (98%) create mode 100644 src/parser/mod.rs rename src/{reader => parser}/pi.rs (98%) diff --git a/Changelog.md b/Changelog.md index 1c364f60..c15a19fd 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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 diff --git a/src/lib.rs b/src/lib.rs index f51f6834..b0f37f50 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/reader/element.rs b/src/parser/element.rs similarity index 98% rename from src/reader/element.rs rename to src/parser/element.rs index 323a8311..e257c5ac 100644 --- a/src/reader/element.rs +++ b/src/parser/element.rs @@ -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. /// @@ -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(); /// diff --git a/src/parser/mod.rs b/src/parser/mod.rs new file mode 100644 index 00000000..90f97c9f --- /dev/null +++ b/src/parser/mod.rs @@ -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; + + /// 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; +} diff --git a/src/reader/pi.rs b/src/parser/pi.rs similarity index 98% rename from src/reader/pi.rs rename to src/parser/pi.rs index 24e33d79..ee553579 100644 --- a/src/reader/pi.rs +++ b/src/parser/pi.rs @@ -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. /// @@ -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(); /// diff --git a/src/reader/async_tokio.rs b/src/reader/async_tokio.rs index e1f28629..7b8fa688 100644 --- a/src/reader/async_tokio.rs +++ b/src/reader/async_tokio.rs @@ -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`]. diff --git a/src/reader/buffered_reader.rs b/src/reader/buffered_reader.rs index cd1e5f41..548eca92 100644 --- a/src/reader/buffered_reader.rs +++ b/src/reader/buffered_reader.rs @@ -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 { diff --git a/src/reader/mod.rs b/src/reader/mod.rs index 329a734f..104f4102 100644 --- a/src/reader/mod.rs +++ b/src/reader/mod.rs @@ -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. @@ -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; @@ -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; - - /// 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 @@ -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; diff --git a/src/reader/slice_reader.rs b/src/reader/slice_reader.rs index 88b9e2f7..1afc3e36 100644 --- a/src/reader/slice_reader.rs +++ b/src/reader/slice_reader.rs @@ -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.