Skip to content

Commit 53ac848

Browse files
committed
Handle missing Token::Delim variant when rendering errors
Also fold `render_single_char_token` into `render_token` so that in the future, the compiler can check the exhaustiveness of the match and warn us of missing tokens.
1 parent e0d4ea7 commit 53ac848

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

scraper/src/error/utils.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use cssparser::Token;
22

33
pub(crate) fn render_token(token: &Token<'_>) -> String {
4-
// THIS TOOK FOREVER TO IMPLEMENT
5-
64
match token {
7-
// TODO: Give these guys some better names
8-
Token::Ident(ident) => format!("{}", ident.clone()),
9-
Token::AtKeyword(value) => format!("@{}", value.clone()),
10-
Token::Hash(name) | Token::IDHash(name) => format!("#{}", name.clone()),
11-
Token::QuotedString(value) => format!("\"{}\"", value.clone()),
5+
Token::Ident(ident) => ident.to_string(),
6+
Token::AtKeyword(value) => format!("@{}", value),
7+
Token::Hash(name) | Token::IDHash(name) => format!("#{}", name),
8+
Token::QuotedString(value) => format!("\"{}\"", value),
9+
Token::UnquotedUrl(value) => value.to_string(),
1210
Token::Number {
1311
has_sign: signed,
1412
value: num,
@@ -27,39 +25,30 @@ pub(crate) fn render_token(token: &Token<'_>) -> String {
2725
} => format!("{}{}", render_int(*signed, *num), unit),
2826
Token::WhiteSpace(_) => String::from(" "),
2927
Token::Comment(comment) => format!("/* {} */", comment),
30-
Token::Function(name) => format!("{}()", name.clone()),
31-
Token::BadString(string) => format!("<Bad String {:?}>", string.clone()),
32-
Token::BadUrl(url) => format!("<Bad URL {:?}>", url.clone()),
28+
Token::Function(name) => format!("{}()", name),
29+
Token::BadString(string) => format!("<Bad String {:?}>", string),
30+
Token::BadUrl(url) => format!("<Bad URL {:?}>", url),
3331
// Single-character token
34-
sc_token => render_single_char_token(sc_token),
32+
Token::Colon => ":".into(),
33+
Token::Semicolon => ";".into(),
34+
Token::Comma => ",".into(),
35+
Token::IncludeMatch => "~=".into(),
36+
Token::DashMatch => "|=".into(),
37+
Token::PrefixMatch => "^=".into(),
38+
Token::SuffixMatch => "$=".into(),
39+
Token::SubstringMatch => "*=".into(),
40+
Token::CDO => "<!--".into(),
41+
Token::CDC => "-->".into(),
42+
Token::ParenthesisBlock => "<(".into(),
43+
Token::SquareBracketBlock => "<[".into(),
44+
Token::CurlyBracketBlock => "<{".into(),
45+
Token::CloseParenthesis => "<)".into(),
46+
Token::CloseSquareBracket => "<]".into(),
47+
Token::CloseCurlyBracket => "<}".into(),
48+
Token::Delim(delim) => (*delim).into(),
3549
}
3650
}
3751

38-
fn render_single_char_token(token: &Token) -> String {
39-
String::from(match token {
40-
Token::Colon => ":",
41-
Token::Semicolon => ";",
42-
Token::Comma => ",",
43-
Token::IncludeMatch => "~=",
44-
Token::DashMatch => "|=",
45-
Token::PrefixMatch => "^=",
46-
Token::SuffixMatch => "$=",
47-
Token::SubstringMatch => "*=",
48-
Token::CDO => "<!--",
49-
Token::CDC => "-->",
50-
Token::ParenthesisBlock => "<(",
51-
Token::SquareBracketBlock => "<[",
52-
Token::CurlyBracketBlock => "<{",
53-
Token::CloseParenthesis => "<)",
54-
Token::CloseSquareBracket => "<]",
55-
Token::CloseCurlyBracket => "<}",
56-
other => panic!(
57-
"Token {:?} is not supposed to match as a single-character token!",
58-
other
59-
),
60-
})
61-
}
62-
6352
fn render_number(signed: bool, num: f32, token: &Token) -> String {
6453
let num = render_int(signed, num);
6554

@@ -89,3 +78,14 @@ fn render_int_signed(num: f32) -> String {
8978
fn render_int_unsigned(num: f32) -> String {
9079
format!("{}", num)
9180
}
81+
82+
#[cfg(test)]
83+
mod tests {
84+
use crate::Selector;
85+
86+
#[test]
87+
fn regression_test_issue212() {
88+
let err = Selector::parse("div138293@!#@!!@#").unwrap_err();
89+
assert_eq!(err.to_string(), "Token \"@\" was not expected");
90+
}
91+
}

0 commit comments

Comments
 (0)