11//! 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} ;
45
56/// Inlining error
67#[ derive( Debug ) ]
78pub enum InlineError {
89 /// Input-output error. May happen during writing the resulting HTML.
910 IO ( io:: Error ) ,
1011 /// Syntax errors or unsupported selectors.
11- ParseError ,
12+ ParseError ( String ) ,
1213}
1314
1415impl From < io:: Error > for InlineError {
@@ -17,8 +18,29 @@ impl From<io::Error> for InlineError {
1718 }
1819}
1920
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+
2030impl 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)
2345 }
2446}
0 commit comments