1
1
//! Errors that may happen during inlining.
2
- use cssparser:: ParseError ;
3
- use std:: io;
2
+ use cssparser:: { BasicParseErrorKind , ParseError , ParseErrorKind } ;
3
+ use std:: fmt:: { Display , Formatter } ;
4
+ use std:: { fmt, io} ;
4
5
5
6
/// Inlining error
6
7
#[ derive( Debug ) ]
7
8
pub enum InlineError {
8
9
/// Input-output error. May happen during writing the resulting HTML.
9
10
IO ( io:: Error ) ,
10
11
/// Syntax errors or unsupported selectors.
11
- ParseError ,
12
+ ParseError ( String ) ,
12
13
}
13
14
14
15
impl From < io:: Error > for InlineError {
@@ -17,8 +18,29 @@ impl From<io::Error> for InlineError {
17
18
}
18
19
}
19
20
21
+ impl Display for InlineError {
22
+ fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
23
+ match self {
24
+ InlineError :: IO ( error) => write ! ( f, "{}" , error) ,
25
+ InlineError :: ParseError ( error) => write ! ( f, "{}" , error) ,
26
+ }
27
+ }
28
+ }
29
+
20
30
impl From < ( ParseError < ' _ , ( ) > , & str ) > for InlineError {
21
- fn from ( _: ( ParseError < ' _ , ( ) > , & str ) ) -> Self {
22
- InlineError :: ParseError
31
+ fn from ( error : ( ParseError < ' _ , ( ) > , & str ) ) -> Self {
32
+ let message = match error. 0 . kind {
33
+ ParseErrorKind :: Basic ( kind) => match kind {
34
+ BasicParseErrorKind :: UnexpectedToken ( token) => {
35
+ format ! ( "Unexpected token: {:?}" , token)
36
+ }
37
+ BasicParseErrorKind :: EndOfInput => "End of input" . to_string ( ) ,
38
+ BasicParseErrorKind :: AtRuleInvalid ( value) => format ! ( "Invalid @ rule: {}" , value) ,
39
+ BasicParseErrorKind :: AtRuleBodyInvalid => "Invalid @ rule body" . to_string ( ) ,
40
+ BasicParseErrorKind :: QualifiedRuleInvalid => "Invalid qualified rule" . to_string ( ) ,
41
+ } ,
42
+ ParseErrorKind :: Custom ( _) => "Unknown error" . to_string ( ) ,
43
+ } ;
44
+ InlineError :: ParseError ( message)
23
45
}
24
46
}
0 commit comments