Skip to content

Commit 5503bff

Browse files
committed
chore: Use newtype wrapper for converting css_inline::InlineError to PyErr
1 parent e4b8567 commit 5503bff

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

python/src/lib.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ const INLINE_ERROR_DOCSTRING: &str = "An error that can occur during CSS inlinin
77

88
create_exception!(css_inline, InlineError, exceptions::ValueError);
99

10-
fn to_pyerr(error: rust_inline::InlineError) -> PyErr {
11-
match error {
12-
rust_inline::InlineError::IO(error) => InlineError::py_err(format!("{}", error)),
13-
rust_inline::InlineError::Network(error) => InlineError::py_err(format!("{}", error)),
14-
rust_inline::InlineError::ParseError(message) => InlineError::py_err(message),
10+
struct InlineErrorWrapper(rust_inline::InlineError);
11+
12+
impl From<InlineErrorWrapper> for PyErr {
13+
fn from(error: InlineErrorWrapper) -> Self {
14+
match error.0 {
15+
rust_inline::InlineError::IO(error) => InlineError::py_err(format!("{}", error)),
16+
rust_inline::InlineError::Network(error) => InlineError::py_err(format!("{}", error)),
17+
rust_inline::InlineError::ParseError(message) => InlineError::py_err(message),
18+
}
1519
}
1620
}
1721

@@ -61,7 +65,7 @@ impl CSSInliner {
6165
/// Inline CSS in the given HTML document
6266
#[text_signature = "(html)"]
6367
fn inline(&self, html: &str) -> PyResult<String> {
64-
Ok(self.inner.inline(html).map_err(to_pyerr)?)
68+
Ok(self.inner.inline(html).map_err(InlineErrorWrapper)?)
6569
}
6670

6771
/// inline_many(htmls)
@@ -90,7 +94,7 @@ fn inline(
9094
load_remote_stylesheets: load_remote_stylesheets.unwrap_or(true),
9195
};
9296
let inliner = rust_inline::CSSInliner::new(options);
93-
Ok(inliner.inline(html).map_err(to_pyerr)?)
97+
Ok(inliner.inline(html).map_err(InlineErrorWrapper)?)
9498
}
9599

96100
/// inline_many(htmls, remove_style_tags=False, base_url=None, load_remote_stylesheets=True)
@@ -120,7 +124,7 @@ fn inline_many_impl(inliner: &rust_inline::CSSInliner, htmls: &PyList) -> PyResu
120124
.par_iter()
121125
.map(|html| inliner.inline(html))
122126
.collect();
123-
Ok(inlined.map_err(to_pyerr)?)
127+
Ok(inlined.map_err(InlineErrorWrapper)?)
124128
}
125129

126130
#[allow(dead_code)]

0 commit comments

Comments
 (0)