Skip to content

Commit

Permalink
Implement XmlSource::read_pi like XmlSource::read_element
Browse files Browse the repository at this point in the history
Return error from read_pi function instead of returning flag and later converting it to the error
  • Loading branch information
Mingun authored and dralley committed Jun 9, 2024
1 parent 79b2fda commit 6c58bef
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
17 changes: 10 additions & 7 deletions src/reader/buffered_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,12 @@ macro_rules! impl_buffered_source {
&mut self,
buf: &'b mut Vec<u8>,
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,
Expand All @@ -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);
Expand All @@ -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>)? (
Expand Down
9 changes: 4 additions & 5 deletions src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
///
Expand Down
12 changes: 5 additions & 7 deletions src/reader/slice_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,19 @@ 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) {
// +1 for `>` which we do not include
*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(
Expand Down

0 comments on commit 6c58bef

Please sign in to comment.