1use std::fmt::Display;
2use serde::de::Error as DeError;
3use serde::ser::Error as SerError;
4
5error_chain! {
6 types {
7 Error,
8 ErrorKind,
9 ResultExt,
10 Result;
11 }
12
13 foreign_links {
14 Io(::std::io::Error);
15 FromUtf8Error(::std::string::FromUtf8Error);
16 ParseIntError(::std::num::ParseIntError);
17 ParseFloatError(::std::num::ParseFloatError);
18 ParseBoolError(::std::str::ParseBoolError);
19 Syntax(::xml::reader::Error);
20 }
21
22 errors {
23 UnexpectedToken(token: String, found: String) {
24 description("unexpected token")
25 display("Expected token {}, found {}", token, found)
26 }
27 Custom(field: String) {
28 description("other error")
29 display("custom: '{}'", field)
30 }
31 UnsupportedOperation(operation: String) {
32 description("unsupported operation")
33 display("unsupported operation: '{}'", operation)
34 }
35 NonPrimitiveKey {
36 description("Map key has non-primitive value")
37 display("Map key has non-primitive value")
38 }
39 }
40}
41
42macro_rules! expect {
43 ($actual: expr, $($expected: pat)|+ => $if_ok: expr) => {
44 match $actual {
45 $($expected)|+ => $if_ok,
46 actual => Err($crate::ErrorKind::UnexpectedToken(
47 stringify!($($expected)|+).to_string(), format!("{:?}",actual)
48 ).into()) as Result<_>
49 }
50 }
51}
52
53#[cfg(debug_assertions)]
54macro_rules! debug_expect {
55 ($actual: expr, $($expected: pat)|+ => $if_ok: expr) => {
56 match $actual {
57 $($expected)|+ => $if_ok,
58 actual => panic!(
59 "Internal error: Expected token {}, found {:?}",
60 stringify!($($expected)|+),
61 actual
62 )
63 }
64 }
65}
66
67#[cfg(not(debug_assertions))]
68macro_rules! debug_expect {
69 ($actual: expr, $($expected: pat)|+ => $if_ok: expr) => {
70 match $actual {
71 $($expected)|+ => $if_ok,
72 _ => unreachable!()
73 }
74 }
75}
76
77impl DeError for Error {
78 fn custom<T: Display>(msg: T) -> Self {
79 ErrorKind::Custom(msg.to_string()).into()
80 }
81}
82
83impl SerError for Error {
84 fn custom<T: Display>(msg: T) -> Self {
85 ErrorKind::Custom(msg.to_string()).into()
86 }
87}