Skip to content

Commit 596edd6

Browse files
committed
Replace Error::UnexpectedToken by Error::IllFormed(DoubleHyphenInComment)
1 parent a4febad commit 596edd6

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed

Changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- `Error::UnexpectedBang` replaced by `SyntaxError`
2424
- `Error::UnexpectedEof` replaced by `SyntaxError` in some cases
2525
- `Error::UnexpectedEof` replaced by `IllFormedError` in some cases
26+
- `Error::UnexpectedToken` replaced by `IllFormedError::DoubleHyphenInComment`
2627

2728
[#675]: https://github.com/tafia/quick-xml/pull/675
2829

examples/read_nodes.rs

+25-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Note: for this specific data set using serde feature would simplify
33
// this simple data is purely to make it easier to understand the code
44

5+
use quick_xml::events::attributes::AttrError;
56
use quick_xml::events::{BytesStart, Event};
67
use quick_xml::name::QName;
78
use quick_xml::reader::Reader;
@@ -29,6 +30,26 @@ const XML: &str = r#"
2930
</Localization>
3031
"#;
3132

33+
#[derive(Debug)]
34+
enum AppError {
35+
/// XML parsing error
36+
Xml(quick_xml::Error),
37+
/// The `Translation/Text` node is missed
38+
NoText(String),
39+
}
40+
41+
impl From<quick_xml::Error> for AppError {
42+
fn from(error: quick_xml::Error) -> Self {
43+
Self::Xml(error)
44+
}
45+
}
46+
47+
impl From<AttrError> for AppError {
48+
fn from(error: AttrError) -> Self {
49+
Self::Xml(quick_xml::Error::InvalidAttr(error))
50+
}
51+
}
52+
3253
#[derive(Debug)]
3354
struct Translation {
3455
tag: String,
@@ -40,7 +61,7 @@ impl Translation {
4061
fn new_from_element(
4162
reader: &mut Reader<&[u8]>,
4263
element: BytesStart,
43-
) -> Result<Translation, quick_xml::Error> {
64+
) -> Result<Translation, AppError> {
4465
let mut tag = Cow::Borrowed("");
4566
let mut lang = Cow::Borrowed("");
4667

@@ -68,16 +89,16 @@ impl Translation {
6889
} else {
6990
dbg!("Expected Event::Start for Text, got: {:?}", &event);
7091
let name_string = reader.decoder().decode(name.as_ref())?;
71-
Err(quick_xml::Error::UnexpectedToken(name_string.into()))
92+
Err(AppError::NoText(name_string.into()))
7293
}
7394
} else {
7495
let event_string = format!("{:?}", event);
75-
Err(quick_xml::Error::UnexpectedToken(event_string))
96+
Err(AppError::NoText(event_string))
7697
}
7798
}
7899
}
79100

80-
fn main() -> Result<(), quick_xml::Error> {
101+
fn main() -> Result<(), AppError> {
81102
// In a real-world use case, Settings would likely be a struct
82103
// HashMap here is just to make the sample code short
83104
let mut settings: HashMap<String, String>;

src/errors.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ pub enum IllFormedError {
8484
/// Name of actually closed tag
8585
found: String,
8686
},
87+
/// A comment contains forbidden double-hyphen (`--`) sequence inside.
88+
///
89+
/// According to the [specification], for compatibility, comments MUST NOT contain
90+
/// double-hyphen (`--`) sequence, in particular, they cannot end by `--->`.
91+
///
92+
/// The quick-xml by default does not check that, because this restriction is
93+
/// mostly artificial, but you can enable it in the [configuration].
94+
///
95+
/// [specification]: https://www.w3.org/TR/xml11/#sec-comments
96+
/// [configuration]: crate::reader::Reader::check_comments
97+
DoubleHyphenInComment,
8798
}
8899

89100
impl fmt::Display for IllFormedError {
@@ -102,6 +113,9 @@ impl fmt::Display for IllFormedError {
102113
"expected `</{}>`, but `</{}>` was found",
103114
expected, found,
104115
),
116+
Self::DoubleHyphenInComment => {
117+
write!(f, "forbidden string `--` was found in a comment")
118+
}
105119
}
106120
}
107121
}
@@ -126,8 +140,6 @@ pub enum Error {
126140
///
127141
/// [`encoding`]: index.html#encoding
128142
NonDecodable(Option<Utf8Error>),
129-
/// Unexpected token
130-
UnexpectedToken(String),
131143
/// Text not found, expected `Event::Text`
132144
TextNotFound,
133145
/// `Event::BytesDecl` must start with *version* attribute. Contains the attribute
@@ -236,7 +248,6 @@ impl fmt::Display for Error {
236248
Error::IllFormed(e) => write!(f, "ill-formed document: {}", e),
237249
Error::NonDecodable(None) => write!(f, "Malformed input, decoding impossible"),
238250
Error::NonDecodable(Some(e)) => write!(f, "Malformed UTF-8 input: {}", e),
239-
Error::UnexpectedToken(e) => write!(f, "Unexpected token '{}'", e),
240251
Error::TextNotFound => write!(f, "Cannot read text, expecting Event::Text"),
241252
Error::XmlDeclWithoutVersion(e) => write!(
242253
f,

src/reader/state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl ReaderState {
9797
.position(|p| buf[3 + p + 1] == b'-')
9898
{
9999
self.offset += len - p;
100-
return Err(Error::UnexpectedToken("--".to_string()));
100+
return Err(Error::IllFormed(IllFormedError::DoubleHyphenInComment));
101101
}
102102
}
103103
Ok(Event::Comment(BytesText::wrap(

tests/xmlrs_reader_tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,15 @@ fn dashes_in_comments() {
201201
test(
202202
r#"<!-- comment -- --><hello/>"#,
203203
r#"
204-
|Error: Unexpected token '--'
204+
|Error: ill-formed document: forbidden string `--` was found in a comment
205205
"#,
206206
true,
207207
);
208208

209209
test(
210210
r#"<!-- comment ---><hello/>"#,
211211
r#"
212-
|Error: Unexpected token '--'
212+
|Error: ill-formed document: forbidden string `--` was found in a comment
213213
"#,
214214
true,
215215
);

0 commit comments

Comments
 (0)