Skip to content

Commit 6d856d5

Browse files
ChiefMilesEdgeworthStranger6667
authored andcommitted
chore: Add usage of Cow for parse error text
1 parent bcfa3f1 commit 6d856d5

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/error.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Errors that may happen during inlining.
22
use cssparser::{BasicParseErrorKind, ParseError, ParseErrorKind};
33
use std::{
4+
borrow::Cow,
45
error::Error,
56
fmt,
67
fmt::{Display, Formatter},
@@ -15,7 +16,7 @@ pub enum InlineError {
1516
/// Network-related problem. E.g. resource is not available.
1617
Network(attohttpc::Error),
1718
/// Syntax errors or unsupported selectors.
18-
ParseError(String),
19+
ParseError(Cow<'static, str>),
1920
}
2021

2122
impl From<io::Error> for InlineError {
@@ -36,7 +37,7 @@ impl Display for InlineError {
3637
match self {
3738
InlineError::IO(error) => f.write_str(error.to_string().as_str()),
3839
InlineError::Network(error) => f.write_str(error.to_string().as_str()),
39-
InlineError::ParseError(error) => f.write_str(error.as_str()),
40+
InlineError::ParseError(error) => f.write_str(error),
4041
}
4142
}
4243
}
@@ -55,6 +56,6 @@ impl From<(ParseError<'_, ()>, &str)> for InlineError {
5556
},
5657
ParseErrorKind::Custom(_) => "Unknown error".to_string(),
5758
};
58-
InlineError::ParseError(message)
59+
InlineError::ParseError(Cow::from(message))
5960
}
6061
}

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl<'a> CSSInliner<'a> {
265265
if self.options.inline_style_tags {
266266
for style_tag in document
267267
.select("style")
268-
.map_err(|_| error::InlineError::ParseError("Unknown error".to_string()))?
268+
.map_err(|_| error::InlineError::ParseError(Cow::from("Unknown error")))?
269269
{
270270
if let Some(first_child) = style_tag.as_node().first_child() {
271271
if let Some(css_cell) = first_child.as_text() {
@@ -279,15 +279,15 @@ impl<'a> CSSInliner<'a> {
279279
} else if self.options.remove_style_tags {
280280
for style_tag in document
281281
.select("style")
282-
.map_err(|_| error::InlineError::ParseError("Unknown error".to_string()))?
282+
.map_err(|_| error::InlineError::ParseError(Cow::from("Unknown error")))?
283283
{
284284
style_tag.as_node().detach()
285285
}
286286
}
287287
if self.options.load_remote_stylesheets {
288288
for link_tag in document
289289
.select("link[rel~=stylesheet]")
290-
.map_err(|_| error::InlineError::ParseError("Unknown error".to_string()))?
290+
.map_err(|_| error::InlineError::ParseError(Cow::from("Unknown error")))?
291291
{
292292
if let Some(href) = link_tag.attributes.borrow().get("href") {
293293
let url = self.get_full_url(href);

0 commit comments

Comments
 (0)