Skip to content

Commit e4daf00

Browse files
committed
chore: Add Result type alias
1 parent 5503bff commit e4daf00

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

src/lib.rs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -114,36 +114,19 @@
114114
variant_size_differences
115115
)]
116116
use kuchiki::traits::TendrilSink;
117-
use kuchiki::{parse_html, NodeRef, Selectors};
117+
use kuchiki::{parse_html, NodeRef};
118118

119119
pub mod error;
120120
mod parser;
121121

122122
pub use error::InlineError;
123+
use parser::Rule;
123124
use std::borrow::Cow;
124125
use std::collections::HashMap;
125126
use std::fs::File;
126127
use std::io::{Read, Write};
127128
pub use url::{ParseError, Url};
128129

129-
#[derive(Debug)]
130-
struct Rule<'i> {
131-
selectors: Selectors,
132-
declarations: Vec<parser::Declaration<'i>>,
133-
}
134-
135-
impl<'i> Rule<'i> {
136-
pub fn new(
137-
selectors: &str,
138-
declarations: Vec<parser::Declaration<'i>>,
139-
) -> Result<Rule<'i>, ()> {
140-
Ok(Rule {
141-
selectors: Selectors::compile(selectors)?,
142-
declarations,
143-
})
144-
}
145-
}
146-
147130
/// Configuration options for CSS inlining process.
148131
#[derive(Debug)]
149132
pub struct InlineOptions {
@@ -178,6 +161,8 @@ impl Default for InlineOptions {
178161
}
179162
}
180163

164+
type Result<T> = std::result::Result<T, InlineError>;
165+
181166
/// Customizable CSS inliner.
182167
#[derive(Debug)]
183168
pub struct CSSInliner {
@@ -203,7 +188,7 @@ impl CSSInliner {
203188
/// Inline CSS styles from <style> tags to matching elements in the HTML tree and return a
204189
/// string.
205190
#[inline]
206-
pub fn inline(&self, html: &str) -> Result<String, InlineError> {
191+
pub fn inline(&self, html: &str) -> Result<String> {
207192
let mut out = vec![];
208193
self.inline_to(html, &mut out)?;
209194
Ok(String::from_utf8_lossy(&out).to_string())
@@ -212,7 +197,7 @@ impl CSSInliner {
212197
/// Inline CSS & write the result to a generic writer. Use it if you want to write
213198
/// the inlined document to a file.
214199
#[inline]
215-
pub fn inline_to<W: Write>(&self, html: &str, target: &mut W) -> Result<(), InlineError> {
200+
pub fn inline_to<W: Write>(&self, html: &str, target: &mut W) -> Result<()> {
216201
let document = parse_html().one(html);
217202
for style_tag in document
218203
.select("style")
@@ -263,7 +248,7 @@ impl CSSInliner {
263248
Cow::Borrowed(href)
264249
}
265250

266-
fn load_external(&self, url: &str) -> Result<String, InlineError> {
251+
fn load_external(&self, url: &str) -> Result<String> {
267252
if url.starts_with("http") | url.starts_with("https") {
268253
let response = attohttpc::get(url).send()?;
269254
Ok(response.text()?)
@@ -276,7 +261,7 @@ impl CSSInliner {
276261
}
277262
}
278263

279-
fn process_css(document: &NodeRef, css: &str) -> Result<(), InlineError> {
264+
fn process_css(document: &NodeRef, css: &str) -> Result<()> {
280265
let mut parse_input = cssparser::ParserInput::new(css);
281266
let mut parser = parser::CSSParser::new(&mut parse_input);
282267
for parsed in parser.parse() {
@@ -319,20 +304,17 @@ impl Default for CSSInliner {
319304

320305
/// Shortcut for inlining CSS with default parameters.
321306
#[inline]
322-
pub fn inline(html: &str) -> Result<String, InlineError> {
307+
pub fn inline(html: &str) -> Result<String> {
323308
CSSInliner::default().inline(html)
324309
}
325310

326311
/// Shortcut for inlining CSS with default parameters and writing the output to a generic writer.
327312
#[inline]
328-
pub fn inline_to<W: Write>(html: &str, target: &mut W) -> Result<(), InlineError> {
313+
pub fn inline_to<W: Write>(html: &str, target: &mut W) -> Result<()> {
329314
CSSInliner::default().inline_to(html, target)
330315
}
331316

332-
fn merge_styles(
333-
existing_style: &str,
334-
new_styles: &[parser::Declaration],
335-
) -> Result<String, InlineError> {
317+
fn merge_styles(existing_style: &str, new_styles: &[parser::Declaration]) -> Result<String> {
336318
// Parse existing declarations in "style" attribute
337319
let mut input = cssparser::ParserInput::new(existing_style);
338320
let mut parser = cssparser::Parser::new(&mut input);

src/parser.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
use kuchiki::Selectors;
2+
13
pub struct CSSRuleListParser;
24
pub(crate) struct CSSDeclarationListParser;
35

46
pub type Declaration<'i> = (cssparser::CowRcStr<'i>, &'i str);
57
pub type QualifiedRule<'i> = (&'i str, Vec<Declaration<'i>>);
68

9+
#[derive(Debug)]
10+
pub(crate) struct Rule<'i> {
11+
pub(crate) selectors: Selectors,
12+
pub(crate) declarations: Vec<Declaration<'i>>,
13+
}
14+
15+
impl<'i> Rule<'i> {
16+
pub fn new(selectors: &str, declarations: Vec<Declaration<'i>>) -> Result<Rule<'i>, ()> {
17+
Ok(Rule {
18+
selectors: Selectors::compile(selectors)?,
19+
declarations,
20+
})
21+
}
22+
}
23+
724
fn exhaust<'i>(input: &mut cssparser::Parser<'i, '_>) -> &'i str {
825
let start = input.position();
926
while input.next().is_ok() {}

0 commit comments

Comments
 (0)