1
+ use std:: { error, fmt} ;
2
+
1
3
#[ derive( Debug ) ]
2
4
pub enum Error {
3
5
/// a VLQ string was malformed and data was left over
@@ -15,6 +17,35 @@ pub enum Error {
15
17
/// a reference to a non existing name was encountered
16
18
BadNameReference ( u32 ) ,
17
19
}
20
+ impl fmt:: Display for Error {
21
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
22
+ match self {
23
+ Error :: VlqLeftover => write ! ( f, "VLQ string was malformed and data was left over" ) ,
24
+ Error :: VlqNoValues => write ! ( f, "VLQ string was empty and no values could be decoded" ) ,
25
+ Error :: VlqOverflow => write ! ( f, "The input encoded a number that didn't fit into i64" ) ,
26
+ Error :: BadJson ( err) => write ! ( f, "JSON parsing error: {err}" ) ,
27
+ Error :: BadSegmentSize ( size) => {
28
+ write ! ( f, "Mapping segment had an unsupported size of {size}" )
29
+ }
30
+ Error :: BadSourceReference ( idx) => {
31
+ write ! ( f, "Reference to non-existing source at position {idx}" )
32
+ }
33
+ Error :: BadNameReference ( idx) => {
34
+ write ! ( f, "Reference to non-existing name at position {idx}" )
35
+ }
36
+ }
37
+ }
38
+ }
39
+
40
+ impl error:: Error for Error {
41
+ fn source ( & self ) -> Option < & ( dyn error:: Error + ' static ) > {
42
+ if let Self :: BadJson ( err) = self {
43
+ Some ( err)
44
+ } else {
45
+ None
46
+ }
47
+ }
48
+ }
18
49
19
50
/// The result of decoding.
20
51
pub type Result < T > = std:: result:: Result < T , Error > ;
0 commit comments