114
114
variant_size_differences
115
115
) ]
116
116
use kuchiki:: traits:: TendrilSink ;
117
- use kuchiki:: { parse_html, NodeRef , Selectors } ;
117
+ use kuchiki:: { parse_html, NodeRef } ;
118
118
119
119
pub mod error;
120
120
mod parser;
121
121
122
122
pub use error:: InlineError ;
123
+ use parser:: Rule ;
123
124
use std:: borrow:: Cow ;
124
125
use std:: collections:: HashMap ;
125
126
use std:: fs:: File ;
126
127
use std:: io:: { Read , Write } ;
127
128
pub use url:: { ParseError , Url } ;
128
129
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
-
147
130
/// Configuration options for CSS inlining process.
148
131
#[ derive( Debug ) ]
149
132
pub struct InlineOptions {
@@ -178,6 +161,8 @@ impl Default for InlineOptions {
178
161
}
179
162
}
180
163
164
+ type Result < T > = std:: result:: Result < T , InlineError > ;
165
+
181
166
/// Customizable CSS inliner.
182
167
#[ derive( Debug ) ]
183
168
pub struct CSSInliner {
@@ -203,7 +188,7 @@ impl CSSInliner {
203
188
/// Inline CSS styles from <style> tags to matching elements in the HTML tree and return a
204
189
/// string.
205
190
#[ inline]
206
- pub fn inline ( & self , html : & str ) -> Result < String , InlineError > {
191
+ pub fn inline ( & self , html : & str ) -> Result < String > {
207
192
let mut out = vec ! [ ] ;
208
193
self . inline_to ( html, & mut out) ?;
209
194
Ok ( String :: from_utf8_lossy ( & out) . to_string ( ) )
@@ -212,7 +197,7 @@ impl CSSInliner {
212
197
/// Inline CSS & write the result to a generic writer. Use it if you want to write
213
198
/// the inlined document to a file.
214
199
#[ 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 < ( ) > {
216
201
let document = parse_html ( ) . one ( html) ;
217
202
for style_tag in document
218
203
. select ( "style" )
@@ -263,7 +248,7 @@ impl CSSInliner {
263
248
Cow :: Borrowed ( href)
264
249
}
265
250
266
- fn load_external ( & self , url : & str ) -> Result < String , InlineError > {
251
+ fn load_external ( & self , url : & str ) -> Result < String > {
267
252
if url. starts_with ( "http" ) | url. starts_with ( "https" ) {
268
253
let response = attohttpc:: get ( url) . send ( ) ?;
269
254
Ok ( response. text ( ) ?)
@@ -276,7 +261,7 @@ impl CSSInliner {
276
261
}
277
262
}
278
263
279
- fn process_css ( document : & NodeRef , css : & str ) -> Result < ( ) , InlineError > {
264
+ fn process_css ( document : & NodeRef , css : & str ) -> Result < ( ) > {
280
265
let mut parse_input = cssparser:: ParserInput :: new ( css) ;
281
266
let mut parser = parser:: CSSParser :: new ( & mut parse_input) ;
282
267
for parsed in parser. parse ( ) {
@@ -319,20 +304,17 @@ impl Default for CSSInliner {
319
304
320
305
/// Shortcut for inlining CSS with default parameters.
321
306
#[ inline]
322
- pub fn inline ( html : & str ) -> Result < String , InlineError > {
307
+ pub fn inline ( html : & str ) -> Result < String > {
323
308
CSSInliner :: default ( ) . inline ( html)
324
309
}
325
310
326
311
/// Shortcut for inlining CSS with default parameters and writing the output to a generic writer.
327
312
#[ 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 < ( ) > {
329
314
CSSInliner :: default ( ) . inline_to ( html, target)
330
315
}
331
316
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 > {
336
318
// Parse existing declarations in "style" attribute
337
319
let mut input = cssparser:: ParserInput :: new ( existing_style) ;
338
320
let mut parser = cssparser:: Parser :: new ( & mut input) ;
0 commit comments