@@ -4,14 +4,16 @@ use crate::escape::EscapeError;
44use crate :: events:: attributes:: AttrError ;
55use crate :: utils:: write_byte_string;
66use std:: str:: Utf8Error ;
7+ use std:: string:: FromUtf8Error ;
78
89/// The error type used by this crate.
910#[ derive( Debug ) ]
1011pub enum Error {
1112 /// IO error
1213 Io ( :: std:: io:: Error ) ,
13- /// Utf8 error
14- Utf8 ( Utf8Error ) ,
14+ /// Input decoding error. If `encoding` feature is disabled, contains `None`,
15+ /// otherwise contains the UTF-8 decoding error
16+ NonDecodable ( Option < Utf8Error > ) ,
1517 /// Unexpected End of File
1618 UnexpectedEof ( String ) ,
1719 /// End event mismatch
@@ -46,10 +48,18 @@ impl From<::std::io::Error> for Error {
4648}
4749
4850impl From < Utf8Error > for Error {
49- /// Creates a new `Error::Utf8 ` from the given error
51+ /// Creates a new `Error::NonDecodable ` from the given error
5052 #[ inline]
5153 fn from ( error : Utf8Error ) -> Error {
52- Error :: Utf8 ( error)
54+ Error :: NonDecodable ( Some ( error) )
55+ }
56+ }
57+
58+ impl From < FromUtf8Error > for Error {
59+ /// Creates a new `Error::Utf8` from the given error
60+ #[ inline]
61+ fn from ( error : FromUtf8Error ) -> Error {
62+ error. utf8_error ( ) . into ( )
5363 }
5464}
5565
@@ -77,7 +87,8 @@ impl std::fmt::Display for Error {
7787 fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
7888 match self {
7989 Error :: Io ( e) => write ! ( f, "I/O error: {}" , e) ,
80- Error :: Utf8 ( e) => write ! ( f, "UTF8 error: {}" , e) ,
90+ Error :: NonDecodable ( None ) => write ! ( f, "Malformed input, decoding impossible" ) ,
91+ Error :: NonDecodable ( Some ( e) ) => write ! ( f, "Malformed UTF-8 input: {}" , e) ,
8192 Error :: UnexpectedEof ( e) => write ! ( f, "Unexpected EOF during reading {}" , e) ,
8293 Error :: EndEventMismatch { expected, found } => {
8394 write ! ( f, "Expecting </{}> found </{}>" , expected, found)
@@ -109,7 +120,7 @@ impl std::error::Error for Error {
109120 fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
110121 match self {
111122 Error :: Io ( e) => Some ( e) ,
112- Error :: Utf8 ( e ) => Some ( e) ,
123+ Error :: NonDecodable ( Some ( e ) ) => Some ( e) ,
113124 Error :: InvalidAttr ( e) => Some ( e) ,
114125 Error :: EscapeError ( e) => Some ( e) ,
115126 _ => None ,
@@ -227,6 +238,7 @@ pub mod serialize {
227238 }
228239
229240 impl From < Error > for DeError {
241+ #[ inline]
230242 fn from ( e : Error ) -> Self {
231243 Self :: InvalidXml ( e)
232244 }
@@ -239,15 +251,17 @@ pub mod serialize {
239251 }
240252 }
241253
242- impl From < ParseIntError > for DeError {
243- fn from ( e : ParseIntError ) -> Self {
244- Self :: InvalidInt ( e)
254+ impl From < Utf8Error > for DeError {
255+ #[ inline]
256+ fn from ( e : Utf8Error ) -> Self {
257+ Self :: InvalidXml ( e. into ( ) )
245258 }
246259 }
247260
248- impl From < ParseFloatError > for DeError {
249- fn from ( e : ParseFloatError ) -> Self {
250- Self :: InvalidFloat ( e)
261+ impl From < FromUtf8Error > for DeError {
262+ #[ inline]
263+ fn from ( e : FromUtf8Error ) -> Self {
264+ Self :: InvalidXml ( e. into ( ) )
251265 }
252266 }
253267
@@ -257,4 +271,18 @@ pub mod serialize {
257271 Self :: InvalidXml ( e. into ( ) )
258272 }
259273 }
274+
275+ impl From < ParseIntError > for DeError {
276+ #[ inline]
277+ fn from ( e : ParseIntError ) -> Self {
278+ Self :: InvalidInt ( e)
279+ }
280+ }
281+
282+ impl From < ParseFloatError > for DeError {
283+ #[ inline]
284+ fn from ( e : ParseFloatError ) -> Self {
285+ Self :: InvalidFloat ( e)
286+ }
287+ }
260288}
0 commit comments