Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An Error Type for Selector::parse #95

Merged
merged 17 commits into from Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/error.rs
Expand Up @@ -84,10 +84,10 @@ impl<'a> Display for SelectorErrorKind<'a> {
Self::UnexpectedToken(token) => {
format!("Token {:?} was not expected", utils::render_token(token))
}
Self::EndOfLine => format!("Unexpected EOL"),
Self::EndOfLine => "Unexpected EOL".to_string(),
Self::InvalidAtRule(rule) => format!("Invalid @-rule {:?}", rule),
Self::InvalidAtRuleBody => format!("The body of an @-rule was invalid"),
Self::QualRuleInvalid => format!("The qualified name was invalid"),
Self::InvalidAtRuleBody => "The body of an @-rule was invalid".to_string(),
Self::QualRuleInvalid => "The qualified name was invalid".to_string(),
Self::ExpectedColonOnPseudoElement(token) => format!(
"Expected a ':' token for pseudoelement, got {:?} instead",
utils::render_token(token)
Expand Down
11 changes: 5 additions & 6 deletions src/error/utils.rs
@@ -1,8 +1,7 @@
use cssparser::Token;

pub(crate) fn render_token<'a>(token: &Token<'a>) -> String {
pub(crate) fn render_token(token: &Token<'_>) -> String {
// THIS TOOK FOREVER TO IMPLEMENT
// TODO: Make this easier to read, I guess

match token {
// TODO: Give these guys some better names
Expand All @@ -19,20 +18,20 @@ pub(crate) fn render_token<'a>(token: &Token<'a>) -> String {
has_sign: signed,
unit_value: num,
int_value: _,
} => render_number(*signed, *num, &token),
} => render_number(*signed, *num, token),
Token::Dimension {
has_sign: signed,
value: num,
int_value: _,
unit,
} => format!("{}{}", render_int(*signed, *num), unit),
Token::WhiteSpace(_) => String::from(" "),
Token::Comment(comment) => format!("/* {} */", comment.clone()),
Token::Comment(comment) => format!("/* {} */", &(*comment).clone()),
Kiwifuit marked this conversation as resolved.
Show resolved Hide resolved
Token::Function(name) => format!("{}()", name.clone()),
Token::BadString(string) => format!("<Bad String {:?}>", string.clone()),
Token::BadUrl(url) => format!("<Bad URL {:?}>", url.clone()),
// Single-character token
sc_token => render_single_char_token(&sc_token),
sc_token => render_single_char_token(sc_token),
}
}

Expand Down Expand Up @@ -65,7 +64,7 @@ fn render_number(signed: bool, num: f32, token: &Token) -> String {
let num = render_int(signed, num);

match token {
Token::Number { .. } => format!("{}", num),
Token::Number { .. } => num.to_string(),
Kiwifuit marked this conversation as resolved.
Show resolved Hide resolved
Token::Percentage { .. } => format!("{}%", num),
_ => panic!("render_number is not supposed to be called on a non-numerical token"),
}
Expand Down
2 changes: 1 addition & 1 deletion src/selector.rs
Expand Up @@ -30,7 +30,7 @@ impl Selector {

parser::SelectorList::parse(&Parser, &mut parser)
.map(|list| Selector { selectors: list.0 })
.map_err(|e| SelectorErrorKind::from(e))
.map_err(SelectorErrorKind::from)
}

/// Returns true if the element matches this selector.
Expand Down