Skip to content

Commit 7737625

Browse files
committed
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
1 parent 52d6ab3 commit 7737625

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

src/reader/buffered_reader.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,12 @@ macro_rules! impl_buffered_source {
9595
&mut self,
9696
buf: &'b mut Vec<u8>,
9797
position: &mut usize,
98-
) -> Result<(&'b [u8], bool)> {
98+
) -> Result<&'b [u8]> {
9999
let mut parser = super::PiParser::default();
100100

101101
let mut read = 0;
102-
let mut done = false;
103102
let start = buf.len();
104-
while !done {
103+
loop {
105104
let used = {
106105
let available = match self $(.$reader)? .fill_buf() $(.$await)? {
107106
Ok(n) if n.is_empty() => break,
@@ -116,9 +115,13 @@ macro_rules! impl_buffered_source {
116115
match parser.feed(available) {
117116
Some(i) => {
118117
buf.extend_from_slice(&available[..i]);
119-
done = true;
118+
120119
// +1 for `>` which we do not include
121-
i + 1
120+
self $(.$reader)? .consume(i + 1);
121+
read += i + 1;
122+
123+
*position += read;
124+
return Ok(&buf[start..]);
122125
}
123126
None => {
124127
buf.extend_from_slice(available);
@@ -129,9 +132,9 @@ macro_rules! impl_buffered_source {
129132
self $(.$reader)? .consume(used);
130133
read += used;
131134
}
132-
*position += read;
133135

134-
Ok((&buf[start..], done))
136+
*position += read;
137+
Err(Error::Syntax(SyntaxError::UnclosedPIOrXmlDecl))
135138
}
136139

137140
$($async)? fn read_bang_element $(<$lf>)? (

src/reader/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,13 @@ macro_rules! read_until_close {
364364
.read_pi($buf, &mut $self.state.offset)
365365
$(.$await)?
366366
{
367-
Ok((bytes, true)) => $self.state.emit_question_mark(bytes),
368-
Ok((_, false)) => {
367+
Ok(bytes) => $self.state.emit_question_mark(bytes),
368+
Err(e) => {
369369
// We want to report error at `<`, but offset was increased,
370370
// so return it back (-1 for `<`)
371371
$self.state.last_error_offset = start - 1;
372-
Err(Error::Syntax(SyntaxError::UnclosedPIOrXmlDecl))
372+
Err(e)
373373
}
374-
Err(e) => Err(e),
375374
},
376375
// `<...` - opening or self-closed tag
377376
Ok(Some(_)) => match $reader
@@ -833,7 +832,7 @@ trait XmlSource<'r, B> {
833832
/// - `position`: Will be increased by amount of bytes consumed
834833
///
835834
/// [events]: crate::events::Event
836-
fn read_pi(&mut self, buf: B, position: &mut usize) -> Result<(&'r [u8], bool)>;
835+
fn read_pi(&mut self, buf: B, position: &mut usize) -> Result<&'r [u8]>;
837836

838837
/// Read input until comment or CDATA is finished.
839838
///

src/reader/slice_reader.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,21 +275,19 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
275275
}
276276
}
277277

278-
fn read_pi(&mut self, _buf: (), position: &mut usize) -> Result<(&'a [u8], bool)> {
278+
fn read_pi(&mut self, _buf: (), position: &mut usize) -> Result<&'a [u8]> {
279279
let mut parser = PiParser::default();
280280

281281
if let Some(i) = parser.feed(self) {
282282
// +1 for `>` which we do not include
283283
*position += i + 1;
284284
let bytes = &self[..i];
285285
*self = &self[i + 1..];
286-
Ok((bytes, true))
287-
} else {
288-
*position += self.len();
289-
let bytes = &self[..];
290-
*self = &[];
291-
Ok((bytes, false))
286+
return Ok(bytes);
292287
}
288+
289+
*position += self.len();
290+
Err(Error::Syntax(SyntaxError::UnclosedPIOrXmlDecl))
293291
}
294292

295293
fn read_bang_element(

0 commit comments

Comments
 (0)