Skip to content

Commit 51c7236

Browse files
committed
perf: Avoid string allocations during converting ParseError to InlineError
1 parent 5e6ceb4 commit 51c7236

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- Compatibility with the new `cssparser` crate version.
88

9+
### Performance
10+
11+
- Avoid string allocations during converting `ParseError` to `InlineError`.
12+
913
## [0.6.0] - 2020-11-02
1014

1115
### Changed

src/error.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,25 @@ impl Display for InlineError {
4444

4545
impl From<(ParseError<'_, ()>, &str)> for InlineError {
4646
fn from(error: (ParseError<'_, ()>, &str)) -> Self {
47-
let message = match error.0.kind {
47+
return match error.0.kind {
4848
ParseErrorKind::Basic(kind) => match kind {
4949
BasicParseErrorKind::UnexpectedToken(token) => {
50-
format!("Unexpected token: {:?}", token)
50+
InlineError::ParseError(Cow::Owned(format!("Unexpected token: {:?}", token)))
51+
}
52+
BasicParseErrorKind::EndOfInput => {
53+
InlineError::ParseError(Cow::Borrowed("End of input"))
54+
}
55+
BasicParseErrorKind::AtRuleInvalid(value) => {
56+
InlineError::ParseError(Cow::Owned(format!("Invalid @ rule: {}", value)))
57+
}
58+
BasicParseErrorKind::AtRuleBodyInvalid => {
59+
InlineError::ParseError(Cow::Borrowed("Invalid @ rule body"))
60+
}
61+
BasicParseErrorKind::QualifiedRuleInvalid => {
62+
InlineError::ParseError(Cow::Borrowed("Invalid qualified rule"))
5163
}
52-
BasicParseErrorKind::EndOfInput => "End of input".to_string(),
53-
BasicParseErrorKind::AtRuleInvalid(value) => format!("Invalid @ rule: {}", value),
54-
BasicParseErrorKind::AtRuleBodyInvalid => "Invalid @ rule body".to_string(),
55-
BasicParseErrorKind::QualifiedRuleInvalid => "Invalid qualified rule".to_string(),
5664
},
57-
ParseErrorKind::Custom(_) => "Unknown error".to_string(),
65+
ParseErrorKind::Custom(_) => InlineError::ParseError(Cow::Borrowed("Unknown error")),
5866
};
59-
InlineError::ParseError(Cow::from(message))
6067
}
6168
}

0 commit comments

Comments
 (0)