Skip to content

Commit a881551

Browse files
committed
Rename Parser to ReaderState because it is better reflects its purpose
1 parent ffc3215 commit a881551

File tree

3 files changed

+41
-41
lines changed

3 files changed

+41
-41
lines changed

src/reader/mod.rs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::ops::Range;
77
use crate::encoding::Decoder;
88
use crate::errors::{Error, Result};
99
use crate::events::Event;
10-
use crate::reader::parser::Parser;
10+
use crate::reader::state::ReaderState;
1111

1212
use memchr;
1313

@@ -31,7 +31,7 @@ macro_rules! configure_methods {
3131
/// [`End`]: Event::End
3232
/// [`check_end_names`]: Self::check_end_names
3333
pub fn expand_empty_elements(&mut self, val: bool) -> &mut Self {
34-
self $(.$holder)? .parser.expand_empty_elements = val;
34+
self $(.$holder)? .state.expand_empty_elements = val;
3535
self
3636
}
3737

@@ -58,8 +58,8 @@ macro_rules! configure_methods {
5858
/// [`BytesText::inplace_trim_start`]: crate::events::BytesText::inplace_trim_start
5959
/// [`BytesText::inplace_trim_end`]: crate::events::BytesText::inplace_trim_end
6060
pub fn trim_text(&mut self, val: bool) -> &mut Self {
61-
self $(.$holder)? .parser.trim_text_start = val;
62-
self $(.$holder)? .parser.trim_text_end = val;
61+
self $(.$holder)? .state.trim_text_start = val;
62+
self $(.$holder)? .state.trim_text_end = val;
6363
self
6464
}
6565

@@ -83,7 +83,7 @@ macro_rules! configure_methods {
8383
/// [`BytesText::inplace_trim_start`]: crate::events::BytesText::inplace_trim_start
8484
/// [`BytesText::inplace_trim_end`]: crate::events::BytesText::inplace_trim_end
8585
pub fn trim_text_end(&mut self, val: bool) -> &mut Self {
86-
self $(.$holder)? .parser.trim_text_end = val;
86+
self $(.$holder)? .state.trim_text_end = val;
8787
self
8888
}
8989

@@ -99,7 +99,7 @@ macro_rules! configure_methods {
9999
///
100100
/// [`End`]: Event::End
101101
pub fn trim_markup_names_in_closing_tags(&mut self, val: bool) -> &mut Self {
102-
self $(.$holder)? .parser.trim_markup_names_in_closing_tags = val;
102+
self $(.$holder)? .state.trim_markup_names_in_closing_tags = val;
103103
self
104104
}
105105

@@ -137,7 +137,7 @@ macro_rules! configure_methods {
137137
/// [`End`]: Event::End
138138
/// [`expand_empty_elements`]: Self::expand_empty_elements
139139
pub fn check_end_names(&mut self, val: bool) -> &mut Self {
140-
self $(.$holder)? .parser.check_end_names = val;
140+
self $(.$holder)? .state.check_end_names = val;
141141
self
142142
}
143143

@@ -152,7 +152,7 @@ macro_rules! configure_methods {
152152
///
153153
/// [`Comment`]: Event::Comment
154154
pub fn check_comments(&mut self, val: bool) -> &mut Self {
155-
self $(.$holder)? .parser.check_comments = val;
155+
self $(.$holder)? .state.check_comments = val;
156156
self
157157
}
158158
};
@@ -167,16 +167,16 @@ macro_rules! read_event_impl {
167167
$(, $await:ident)?
168168
) => {{
169169
let event = loop {
170-
match $self.parser.state {
170+
match $self.state.state {
171171
ParseState::Init => { // Go to OpenedTag state
172172
// If encoding set explicitly, we not need to detect it. For example,
173173
// explicit UTF-8 set automatically if Reader was created using `from_str`.
174174
// But we still need to remove BOM for consistency with no encoding
175175
// feature enabled path
176176
#[cfg(feature = "encoding")]
177177
if let Some(encoding) = $reader.detect_encoding() $(.$await)? ? {
178-
if $self.parser.encoding.can_be_refined() {
179-
$self.parser.encoding = crate::reader::EncodingRef::BomDetected(encoding);
178+
if $self.state.encoding.can_be_refined() {
179+
$self.state.encoding = crate::reader::EncodingRef::BomDetected(encoding);
180180
}
181181
}
182182

@@ -200,12 +200,12 @@ macro_rules! read_event_impl {
200200
},
201201
// Go to ClosedTag state in next two arms
202202
ParseState::OpenedTag => break $self.$read_until_close($buf) $(.$await)?,
203-
ParseState::Empty => break $self.parser.close_expanded_empty(),
203+
ParseState::Empty => break $self.state.close_expanded_empty(),
204204
ParseState::Exit => break Ok(Event::Eof),
205205
};
206206
};
207207
match event {
208-
Err(_) | Ok(Event::Eof) => $self.parser.state = ParseState::Exit,
208+
Err(_) | Ok(Event::Eof) => $self.state.state = ParseState::Exit,
209209
_ => {}
210210
}
211211
event
@@ -228,24 +228,24 @@ macro_rules! read_until_open {
228228
$read_event:ident
229229
$(, $await:ident)?
230230
) => {{
231-
$self.parser.state = ParseState::OpenedTag;
231+
$self.state.state = ParseState::OpenedTag;
232232

233-
if $self.parser.trim_text_start {
234-
$reader.skip_whitespace(&mut $self.parser.offset) $(.$await)? ?;
233+
if $self.state.trim_text_start {
234+
$reader.skip_whitespace(&mut $self.state.offset) $(.$await)? ?;
235235
}
236236

237237
// If we already at the `<` symbol, do not try to return an empty Text event
238-
if $reader.skip_one(b'<', &mut $self.parser.offset) $(.$await)? ? {
238+
if $reader.skip_one(b'<', &mut $self.state.offset) $(.$await)? ? {
239239
// Pass $buf to the next next iteration of parsing loop
240240
return Ok(Err($buf));
241241
}
242242

243243
match $reader
244-
.read_bytes_until(b'<', $buf, &mut $self.parser.offset)
244+
.read_bytes_until(b'<', $buf, &mut $self.state.offset)
245245
$(.$await)?
246246
{
247247
// Return Text event with `bytes` content
248-
Ok(Some(bytes)) => $self.parser.emit_text(bytes).map(Ok),
248+
Ok(Some(bytes)) => $self.state.emit_text(bytes).map(Ok),
249249
Ok(None) => Ok(Ok(Event::Eof)),
250250
Err(e) => Err(e),
251251
}
@@ -278,43 +278,43 @@ macro_rules! read_until_close {
278278
$reader:expr
279279
$(, $await:ident)?
280280
) => {{
281-
$self.parser.state = ParseState::ClosedTag;
281+
$self.state.state = ParseState::ClosedTag;
282282

283283
match $reader.peek_one() $(.$await)? {
284284
// `<!` - comment, CDATA or DOCTYPE declaration
285285
Ok(Some(b'!')) => match $reader
286-
.read_bang_element($buf, &mut $self.parser.offset)
286+
.read_bang_element($buf, &mut $self.state.offset)
287287
$(.$await)?
288288
{
289289
Ok(None) => Ok(Event::Eof),
290-
Ok(Some((bang_type, bytes))) => $self.parser.emit_bang(bang_type, bytes),
290+
Ok(Some((bang_type, bytes))) => $self.state.emit_bang(bang_type, bytes),
291291
Err(e) => Err(e),
292292
},
293293
// `</` - closing tag
294294
Ok(Some(b'/')) => match $reader
295-
.read_bytes_until(b'>', $buf, &mut $self.parser.offset)
295+
.read_bytes_until(b'>', $buf, &mut $self.state.offset)
296296
$(.$await)?
297297
{
298298
Ok(None) => Ok(Event::Eof),
299-
Ok(Some(bytes)) => $self.parser.emit_end(bytes),
299+
Ok(Some(bytes)) => $self.state.emit_end(bytes),
300300
Err(e) => Err(e),
301301
},
302302
// `<?` - processing instruction
303303
Ok(Some(b'?')) => match $reader
304-
.read_bytes_until(b'>', $buf, &mut $self.parser.offset)
304+
.read_bytes_until(b'>', $buf, &mut $self.state.offset)
305305
$(.$await)?
306306
{
307307
Ok(None) => Ok(Event::Eof),
308-
Ok(Some(bytes)) => $self.parser.emit_question_mark(bytes),
308+
Ok(Some(bytes)) => $self.state.emit_question_mark(bytes),
309309
Err(e) => Err(e),
310310
},
311311
// `<...` - opening or self-closed tag
312312
Ok(Some(_)) => match $reader
313-
.read_element($buf, &mut $self.parser.offset)
313+
.read_element($buf, &mut $self.state.offset)
314314
$(.$await)?
315315
{
316316
Ok(None) => Ok(Event::Eof),
317-
Ok(Some(bytes)) => $self.parser.emit_start(bytes),
317+
Ok(Some(bytes)) => $self.state.emit_start(bytes),
318318
Err(e) => Err(e),
319319
},
320320
Ok(None) => Ok(Event::Eof),
@@ -361,8 +361,8 @@ macro_rules! read_to_end {
361361
mod async_tokio;
362362
mod buffered_reader;
363363
mod ns_reader;
364-
mod parser;
365364
mod slice_reader;
365+
mod state;
366366

367367
pub use ns_reader::NsReader;
368368

@@ -524,7 +524,7 @@ pub struct Reader<R> {
524524
/// Source of data for parse
525525
reader: R,
526526
/// Configuration and current parse state
527-
parser: Parser,
527+
state: ReaderState,
528528
}
529529

530530
/// Builder methods
@@ -533,7 +533,7 @@ impl<R> Reader<R> {
533533
pub fn from_reader(reader: R) -> Self {
534534
Self {
535535
reader,
536-
parser: Parser::default(),
536+
state: ReaderState::default(),
537537
}
538538
}
539539

@@ -615,10 +615,10 @@ impl<R> Reader<R> {
615615
pub fn buffer_position(&self) -> usize {
616616
// when internal state is OpenedTag, we have actually read until '<',
617617
// which we don't want to show
618-
if let ParseState::OpenedTag = self.parser.state {
619-
self.parser.offset - 1
618+
if let ParseState::OpenedTag = self.state.state {
619+
self.state.offset - 1
620620
} else {
621-
self.parser.offset
621+
self.state.offset
622622
}
623623
}
624624

@@ -633,7 +633,7 @@ impl<R> Reader<R> {
633633
/// [`encoding`]: ../index.html#encoding
634634
#[inline]
635635
pub fn decoder(&self) -> Decoder {
636-
self.parser.decoder()
636+
self.state.decoder()
637637
}
638638
}
639639

src/reader/slice_reader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl<'a> Reader<&'a [u8]> {
2727
#[cfg(feature = "encoding")]
2828
{
2929
let mut reader = Self::from_reader(s.as_bytes());
30-
reader.parser.encoding = EncodingRef::Explicit(UTF_8);
30+
reader.state.encoding = EncodingRef::Explicit(UTF_8);
3131
reader
3232
}
3333

src/reader/parser.rs renamed to src/reader/state.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use crate::reader::{is_whitespace, BangType, ParseState};
1010

1111
use memchr;
1212

13-
/// A struct that holds a current parse state and a parser configuration.
13+
/// A struct that holds a current reader state and a parser configuration.
1414
/// It is independent on a way of reading data: the reader feed data into it and
1515
/// get back produced [`Event`]s.
1616
#[derive(Clone)]
17-
pub(super) struct Parser {
18-
/// Number of bytes read from the source of data since the parser was created
17+
pub(super) struct ReaderState {
18+
/// Number of bytes read from the source of data since the reader was created
1919
pub offset: usize,
2020
/// Defines how to process next byte
2121
pub state: ParseState,
@@ -58,7 +58,7 @@ pub(super) struct Parser {
5858
pub encoding: EncodingRef,
5959
}
6060

61-
impl Parser {
61+
impl ReaderState {
6262
/// Trims whitespaces from `bytes`, if required, and returns a [`Text`] event.
6363
///
6464
/// # Parameters
@@ -265,7 +265,7 @@ impl Parser {
265265
}
266266
}
267267

268-
impl Default for Parser {
268+
impl Default for ReaderState {
269269
fn default() -> Self {
270270
Self {
271271
offset: 0,

0 commit comments

Comments
 (0)