1use num::bigint::BigInt;
4use std::convert::From;
5use std::io;
6
7#[derive(Debug)]
9pub enum Error {
10 Io(io::Error),
12 Message(String),
14 InvalidUnicodeScalar(u32),
17 NumberOutOfRange(BigInt),
20 CannotDeserializeAny,
23 MissingCloseDelimiter,
25 MissingItem,
27 Expected(ExpectedKind, Received),
29 #[doc(hidden)] StreamingSerializationUnsupported,
31}
32
33#[derive(Debug)]
35pub enum Received {
36 #[doc(hidden)] ReceivedSomethingElse,
38 ReceivedRecordWithLabel(String),
40 ReceivedOtherValue(String),
42}
43
44#[derive(Debug, PartialEq)]
46pub enum ExpectedKind {
47 Boolean,
48 Float,
49 Double,
50
51 SignedIntegerI128,
52 SignedIntegerU128,
53 SignedInteger,
54 String,
55 ByteString,
56 Symbol,
57
58 Record(Option<usize>),
60 SimpleRecord(String, Option<usize>),
62 Sequence,
63 Set,
64 Dictionary,
65
66 Embedded,
67
68 SequenceOrSet, Option,
71 UnicodeScalar,
72}
73
74impl From<io::Error> for Error {
75 fn from(e: io::Error) -> Self {
76 Error::Io(e)
77 }
78}
79
80impl From<Error> for io::Error {
81 fn from(e: Error) -> Self {
82 match e {
83 Error::Io(ioe) => ioe,
84 Error::Message(str) => io::Error::new(io::ErrorKind::Other, str),
85 _ => io::Error::new(io::ErrorKind::Other, e.to_string()),
86 }
87 }
88}
89
90impl serde::ser::Error for Error {
91 fn custom<T: std::fmt::Display>(msg: T) -> Self {
92 Self::Message(msg.to_string())
93 }
94}
95
96impl serde::de::Error for Error {
97 fn custom<T: std::fmt::Display>(msg: T) -> Self {
98 Self::Message(msg.to_string())
99 }
100}
101
102impl std::error::Error for Error {}
103
104impl std::fmt::Display for Error {
105 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106 write!(f, "{:?}", self)
107 }
108}
109
110pub fn is_io_error(e: &Error) -> bool {
114 matches!(e, Error::Io(_))
115}
116
117pub fn eof() -> Error {
119 Error::Io(io_eof())
120}
121
122pub fn is_eof_error(e: &Error) -> bool {
124 if let Error::Io(ioe) = e {
125 is_eof_io_error(ioe)
126 } else {
127 false
128 }
129}
130
131pub fn syntax_error(s: &str) -> Error {
133 Error::Io(io_syntax_error(s))
134}
135
136pub fn is_syntax_error(e: &Error) -> bool {
138 if let Error::Io(ioe) = e {
139 is_syntax_io_error(ioe)
140 } else {
141 false
142 }
143}
144
145pub fn io_eof() -> io::Error {
149 io::Error::new(io::ErrorKind::UnexpectedEof, "EOF")
150}
151
152pub fn is_eof_io_error(e: &io::Error) -> bool {
154 matches!(e.kind(), io::ErrorKind::UnexpectedEof)
155}
156
157pub fn io_syntax_error(s: &str) -> io::Error {
159 io::Error::new(io::ErrorKind::InvalidData, s)
160}
161
162pub fn is_syntax_io_error(e: &io::Error) -> bool {
164 matches!(e.kind(), io::ErrorKind::InvalidData)
165}