Skip to content

Commit 34e8d50

Browse files
committed
Fix return type of XmlSource::read_bang_element - it shouldn't return 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
1 parent 79489b5 commit 34e8d50

File tree

3 files changed

+43
-48
lines changed

3 files changed

+43
-48
lines changed

src/reader/buffered_reader.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ macro_rules! impl_buffered_source {
9797
&mut self,
9898
buf: &'b mut Vec<u8>,
9999
position: &mut usize,
100-
) -> Result<Option<(BangType, &'b [u8])>> {
100+
) -> Result<(BangType, &'b [u8])> {
101101
// Peeked one bang ('!') before being called, so it's guaranteed to
102102
// start with it.
103103
let start = buf.len();
@@ -140,9 +140,9 @@ macro_rules! impl_buffered_source {
140140
}
141141

142142
if read == 0 {
143-
Ok(None)
143+
Err(bang_type.to_err())
144144
} else {
145-
Ok(Some((bang_type, &buf[start..])))
145+
Ok((bang_type, &buf[start..]))
146146
}
147147
}
148148

src/reader/mod.rs

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,7 @@ macro_rules! read_until_close {
328328
.read_bang_element($buf, &mut $self.state.offset)
329329
$(.$await)?
330330
{
331-
Ok(None) => Ok(Event::Eof),
332-
Ok(Some((bang_type, bytes))) => $self.state.emit_bang(bang_type, bytes),
331+
Ok((bang_type, bytes)) => $self.state.emit_bang(bang_type, bytes),
333332
Err(e) => Err(e),
334333
},
335334
// `</` - closing tag
@@ -790,11 +789,7 @@ trait XmlSource<'r, B> {
790789
/// - `position`: Will be increased by amount of bytes consumed
791790
///
792791
/// [events]: crate::events::Event
793-
fn read_bang_element(
794-
&mut self,
795-
buf: B,
796-
position: &mut usize,
797-
) -> Result<Option<(BangType, &'r [u8])>>;
792+
fn read_bang_element(&mut self, buf: B, position: &mut usize) -> Result<(BangType, &'r [u8])>;
798793

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

1152+
let (ty, bytes) = $source(&mut input)
1153+
.read_bang_element(buf, &mut position)
1154+
$(.$await)?
1155+
.unwrap();
11571156
assert_eq!(
1158-
$source(&mut input)
1159-
.read_bang_element(buf, &mut position)
1160-
$(.$await)?
1161-
.unwrap()
1162-
.map(|(ty, data)| (ty, Bytes(data))),
1163-
Some((BangType::CData, Bytes(b"![CDATA[]]")))
1157+
(ty, Bytes(bytes)),
1158+
(BangType::CData, Bytes(b"![CDATA[]]"))
11641159
);
11651160
assert_eq!(position, 11);
11661161
}
@@ -1175,13 +1170,13 @@ mod test {
11751170
let mut input = b"![CDATA[cdata]] ]>content]]>other content]]>".as_ref();
11761171
// ^= 28
11771172

1173+
let (ty, bytes) = $source(&mut input)
1174+
.read_bang_element(buf, &mut position)
1175+
$(.$await)?
1176+
.unwrap();
11781177
assert_eq!(
1179-
$source(&mut input)
1180-
.read_bang_element(buf, &mut position)
1181-
$(.$await)?
1182-
.unwrap()
1183-
.map(|(ty, data)| (ty, Bytes(data))),
1184-
Some((BangType::CData, Bytes(b"![CDATA[cdata]] ]>content]]")))
1178+
(ty, Bytes(bytes)),
1179+
(BangType::CData, Bytes(b"![CDATA[cdata]] ]>content]]"))
11851180
);
11861181
assert_eq!(position, 28);
11871182
}
@@ -1300,13 +1295,13 @@ mod test {
13001295
let mut input = b"!---->other content".as_ref();
13011296
// ^= 6
13021297

1298+
let (ty, bytes) = $source(&mut input)
1299+
.read_bang_element(buf, &mut position)
1300+
$(.$await)?
1301+
.unwrap();
13031302
assert_eq!(
1304-
$source(&mut input)
1305-
.read_bang_element(buf, &mut position)
1306-
$(.$await)?
1307-
.unwrap()
1308-
.map(|(ty, data)| (ty, Bytes(data))),
1309-
Some((BangType::Comment, Bytes(b"!----")))
1303+
(ty, Bytes(bytes)),
1304+
(BangType::Comment, Bytes(b"!----"))
13101305
);
13111306
assert_eq!(position, 6);
13121307
}
@@ -1318,13 +1313,13 @@ mod test {
13181313
let mut input = b"!--->comment<--->other content".as_ref();
13191314
// ^= 17
13201315

1316+
let (ty, bytes) = $source(&mut input)
1317+
.read_bang_element(buf, &mut position)
1318+
$(.$await)?
1319+
.unwrap();
13211320
assert_eq!(
1322-
$source(&mut input)
1323-
.read_bang_element(buf, &mut position)
1324-
$(.$await)?
1325-
.unwrap()
1326-
.map(|(ty, data)| (ty, Bytes(data))),
1327-
Some((BangType::Comment, Bytes(b"!--->comment<---")))
1321+
(ty, Bytes(bytes)),
1322+
(BangType::Comment, Bytes(b"!--->comment<---"))
13281323
);
13291324
assert_eq!(position, 17);
13301325
}
@@ -1379,13 +1374,13 @@ mod test {
13791374
let mut input = b"!DOCTYPE>other content".as_ref();
13801375
// ^= 9
13811376

1377+
let (ty, bytes) = $source(&mut input)
1378+
.read_bang_element(buf, &mut position)
1379+
$(.$await)?
1380+
.unwrap();
13821381
assert_eq!(
1383-
$source(&mut input)
1384-
.read_bang_element(buf, &mut position)
1385-
$(.$await)?
1386-
.unwrap()
1387-
.map(|(ty, data)| (ty, Bytes(data))),
1388-
Some((BangType::DocType, Bytes(b"!DOCTYPE")))
1382+
(ty, Bytes(bytes)),
1383+
(BangType::DocType, Bytes(b"!DOCTYPE"))
13891384
);
13901385
assert_eq!(position, 9);
13911386
}
@@ -1453,13 +1448,13 @@ mod test {
14531448
let mut input = b"!doctype>other content".as_ref();
14541449
// ^= 9
14551450

1451+
let (ty, bytes) = $source(&mut input)
1452+
.read_bang_element(buf, &mut position)
1453+
$(.$await)?
1454+
.unwrap();
14561455
assert_eq!(
1457-
$source(&mut input)
1458-
.read_bang_element(buf, &mut position)
1459-
$(.$await)?
1460-
.unwrap()
1461-
.map(|(ty, data)| (ty, Bytes(data))),
1462-
Some((BangType::DocType, Bytes(b"!doctype")))
1456+
(ty, Bytes(bytes)),
1457+
(BangType::DocType, Bytes(b"!doctype"))
14631458
);
14641459
assert_eq!(position, 9);
14651460
}

src/reader/slice_reader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
281281
&mut self,
282282
_buf: (),
283283
position: &mut usize,
284-
) -> Result<Option<(BangType, &'a [u8])>> {
284+
) -> Result<(BangType, &'a [u8])> {
285285
// Peeked one bang ('!') before being called, so it's guaranteed to
286286
// start with it.
287287
debug_assert_eq!(self[0], b'!');
@@ -291,7 +291,7 @@ impl<'a> XmlSource<'a, ()> for &'a [u8] {
291291
if let Some((bytes, i)) = bang_type.parse(&[], self) {
292292
*position += i;
293293
*self = &self[i..];
294-
return Ok(Some((bang_type, bytes)));
294+
return Ok((bang_type, bytes));
295295
}
296296

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

0 commit comments

Comments
 (0)