Skip to content

Commit

Permalink
Fix return type of XmlSource::read_bang_element - it shouldn't return…
Browse files Browse the repository at this point in the history
… None, because we inside a markup

An opened `<` character was already read when we call read_bang_element,
so we MUST to find `>` or return a syntax error
  • Loading branch information
Mingun committed Nov 15, 2023
1 parent 79489b5 commit 34e8d50
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 48 deletions.
6 changes: 3 additions & 3 deletions src/reader/buffered_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ macro_rules! impl_buffered_source {
&mut self,
buf: &'b mut Vec<u8>,
position: &mut usize,
) -> Result<Option<(BangType, &'b [u8])>> {
) -> Result<(BangType, &'b [u8])> {
// Peeked one bang ('!') before being called, so it's guaranteed to
// start with it.
let start = buf.len();
Expand Down Expand Up @@ -140,9 +140,9 @@ macro_rules! impl_buffered_source {
}

if read == 0 {
Ok(None)
Err(bang_type.to_err())
} else {
Ok(Some((bang_type, &buf[start..])))
Ok((bang_type, &buf[start..]))
}
}

Expand Down
81 changes: 38 additions & 43 deletions src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,7 @@ macro_rules! read_until_close {
.read_bang_element($buf, &mut $self.state.offset)
$(.$await)?
{
Ok(None) => Ok(Event::Eof),
Ok(Some((bang_type, bytes))) => $self.state.emit_bang(bang_type, bytes),
Ok((bang_type, bytes)) => $self.state.emit_bang(bang_type, bytes),
Err(e) => Err(e),
},
// `</` - closing tag
Expand Down Expand Up @@ -790,11 +789,7 @@ trait XmlSource<'r, B> {
/// - `position`: Will be increased by amount of bytes consumed
///
/// [events]: crate::events::Event
fn read_bang_element(
&mut self,
buf: B,
position: &mut usize,
) -> Result<Option<(BangType, &'r [u8])>>;
fn read_bang_element(&mut self, buf: B, position: &mut usize) -> Result<(BangType, &'r [u8])>;

/// Read input until XML element is closed by approaching a `>` symbol.
/// Returns a buffer that contains a data between `<` and `>` or
Expand Down Expand Up @@ -1154,13 +1149,13 @@ mod test {
let mut input = b"![CDATA[]]>other content".as_ref();
// ^= 11

let (ty, bytes) = $source(&mut input)
.read_bang_element(buf, &mut position)
$(.$await)?
.unwrap();
assert_eq!(
$source(&mut input)
.read_bang_element(buf, &mut position)
$(.$await)?
.unwrap()
.map(|(ty, data)| (ty, Bytes(data))),
Some((BangType::CData, Bytes(b"![CDATA[]]")))
(ty, Bytes(bytes)),
(BangType::CData, Bytes(b"![CDATA[]]"))
);
assert_eq!(position, 11);
}
Expand All @@ -1175,13 +1170,13 @@ mod test {
let mut input = b"![CDATA[cdata]] ]>content]]>other content]]>".as_ref();
// ^= 28

let (ty, bytes) = $source(&mut input)
.read_bang_element(buf, &mut position)
$(.$await)?
.unwrap();
assert_eq!(
$source(&mut input)
.read_bang_element(buf, &mut position)
$(.$await)?
.unwrap()
.map(|(ty, data)| (ty, Bytes(data))),
Some((BangType::CData, Bytes(b"![CDATA[cdata]] ]>content]]")))
(ty, Bytes(bytes)),
(BangType::CData, Bytes(b"![CDATA[cdata]] ]>content]]"))
);
assert_eq!(position, 28);
}
Expand Down Expand Up @@ -1300,13 +1295,13 @@ mod test {
let mut input = b"!---->other content".as_ref();
// ^= 6

let (ty, bytes) = $source(&mut input)
.read_bang_element(buf, &mut position)
$(.$await)?
.unwrap();
assert_eq!(
$source(&mut input)
.read_bang_element(buf, &mut position)
$(.$await)?
.unwrap()
.map(|(ty, data)| (ty, Bytes(data))),
Some((BangType::Comment, Bytes(b"!----")))
(ty, Bytes(bytes)),
(BangType::Comment, Bytes(b"!----"))
);
assert_eq!(position, 6);
}
Expand All @@ -1318,13 +1313,13 @@ mod test {
let mut input = b"!--->comment<--->other content".as_ref();
// ^= 17

let (ty, bytes) = $source(&mut input)
.read_bang_element(buf, &mut position)
$(.$await)?
.unwrap();
assert_eq!(
$source(&mut input)
.read_bang_element(buf, &mut position)
$(.$await)?
.unwrap()
.map(|(ty, data)| (ty, Bytes(data))),
Some((BangType::Comment, Bytes(b"!--->comment<---")))
(ty, Bytes(bytes)),
(BangType::Comment, Bytes(b"!--->comment<---"))
);
assert_eq!(position, 17);
}
Expand Down Expand Up @@ -1379,13 +1374,13 @@ mod test {
let mut input = b"!DOCTYPE>other content".as_ref();
// ^= 9

let (ty, bytes) = $source(&mut input)
.read_bang_element(buf, &mut position)
$(.$await)?
.unwrap();
assert_eq!(
$source(&mut input)
.read_bang_element(buf, &mut position)
$(.$await)?
.unwrap()
.map(|(ty, data)| (ty, Bytes(data))),
Some((BangType::DocType, Bytes(b"!DOCTYPE")))
(ty, Bytes(bytes)),
(BangType::DocType, Bytes(b"!DOCTYPE"))
);
assert_eq!(position, 9);
}
Expand Down Expand Up @@ -1453,13 +1448,13 @@ mod test {
let mut input = b"!doctype>other content".as_ref();
// ^= 9

let (ty, bytes) = $source(&mut input)
.read_bang_element(buf, &mut position)
$(.$await)?
.unwrap();
assert_eq!(
$source(&mut input)
.read_bang_element(buf, &mut position)
$(.$await)?
.unwrap()
.map(|(ty, data)| (ty, Bytes(data))),
Some((BangType::DocType, Bytes(b"!doctype")))
(ty, Bytes(bytes)),
(BangType::DocType, Bytes(b"!doctype"))
);
assert_eq!(position, 9);
}
Expand Down
4 changes: 2 additions & 2 deletions src/reader/slice_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
&mut self,
_buf: (),
position: &mut usize,
) -> Result<Option<(BangType, &'a [u8])>> {
) -> Result<(BangType, &'a [u8])> {
// Peeked one bang ('!') before being called, so it's guaranteed to
// start with it.
debug_assert_eq!(self[0], b'!');
Expand All @@ -291,7 +291,7 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
if let Some((bytes, i)) = bang_type.parse(&[], self) {
*position += i;
*self = &self[i..];
return Ok(Some((bang_type, bytes)));
return Ok((bang_type, bytes));
}

// Note: Do not update position, so the error points to
Expand Down

0 comments on commit 34e8d50

Please sign in to comment.