From 6c58bef3ab12edd0dbbfe30a41a86b4b04530d2d Mon Sep 17 00:00:00 2001 From: Mingun Date: Thu, 6 Jun 2024 22:22:22 +0500 Subject: [PATCH] Implement XmlSource::read_pi like XmlSource::read_element Return error from read_pi function instead of returning flag and later converting it to the error --- src/reader/buffered_reader.rs | 17 ++++++++++------- src/reader/mod.rs | 9 ++++----- src/reader/slice_reader.rs | 12 +++++------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/reader/buffered_reader.rs b/src/reader/buffered_reader.rs index cbc6bb07..8aaa5baa 100644 --- a/src/reader/buffered_reader.rs +++ b/src/reader/buffered_reader.rs @@ -95,13 +95,12 @@ macro_rules! impl_buffered_source { &mut self, buf: &'b mut Vec, position: &mut usize, - ) -> Result<(&'b [u8], bool)> { + ) -> Result<&'b [u8]> { let mut parser = super::PiParser::default(); let mut read = 0; - let mut done = false; let start = buf.len(); - while !done { + loop { let used = { let available = match self $(.$reader)? .fill_buf() $(.$await)? { Ok(n) if n.is_empty() => break, @@ -116,9 +115,13 @@ macro_rules! impl_buffered_source { match parser.feed(available) { Some(i) => { buf.extend_from_slice(&available[..i]); - done = true; + // +1 for `>` which we do not include - i + 1 + self $(.$reader)? .consume(i + 1); + read += i + 1; + + *position += read; + return Ok(&buf[start..]); } None => { buf.extend_from_slice(available); @@ -129,9 +132,9 @@ macro_rules! impl_buffered_source { self $(.$reader)? .consume(used); read += used; } - *position += read; - Ok((&buf[start..], done)) + *position += read; + Err(Error::Syntax(SyntaxError::UnclosedPIOrXmlDecl)) } $($async)? fn read_bang_element $(<$lf>)? ( diff --git a/src/reader/mod.rs b/src/reader/mod.rs index 6de103af..d5cfa237 100644 --- a/src/reader/mod.rs +++ b/src/reader/mod.rs @@ -364,14 +364,13 @@ macro_rules! read_until_close { .read_pi($buf, &mut $self.state.offset) $(.$await)? { - Ok((bytes, true)) => $self.state.emit_question_mark(bytes), - Ok((_, false)) => { + Ok(bytes) => $self.state.emit_question_mark(bytes), + Err(e) => { // We want to report error at `<`, but offset was increased, // so return it back (-1 for `<`) $self.state.last_error_offset = start - 1; - Err(Error::Syntax(SyntaxError::UnclosedPIOrXmlDecl)) + Err(e) } - Err(e) => Err(e), }, // `<...` - opening or self-closed tag Ok(Some(_)) => match $reader @@ -833,7 +832,7 @@ trait XmlSource<'r, B> { /// - `position`: Will be increased by amount of bytes consumed /// /// [events]: crate::events::Event - fn read_pi(&mut self, buf: B, position: &mut usize) -> Result<(&'r [u8], bool)>; + fn read_pi(&mut self, buf: B, position: &mut usize) -> Result<&'r [u8]>; /// Read input until comment or CDATA is finished. /// diff --git a/src/reader/slice_reader.rs b/src/reader/slice_reader.rs index 78b5cb08..ad2925b3 100644 --- a/src/reader/slice_reader.rs +++ b/src/reader/slice_reader.rs @@ -275,7 +275,7 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] { } } - fn read_pi(&mut self, _buf: (), position: &mut usize) -> Result<(&'a [u8], bool)> { + fn read_pi(&mut self, _buf: (), position: &mut usize) -> Result<&'a [u8]> { let mut parser = PiParser::default(); if let Some(i) = parser.feed(self) { @@ -283,13 +283,11 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] { *position += i + 1; let bytes = &self[..i]; *self = &self[i + 1..]; - Ok((bytes, true)) - } else { - *position += self.len(); - let bytes = &self[..]; - *self = &[]; - Ok((bytes, false)) + return Ok(bytes); } + + *position += self.len(); + Err(Error::Syntax(SyntaxError::UnclosedPIOrXmlDecl)) } fn read_bang_element(