1use std::{error, fmt};
2
3#[derive(Debug)]
4pub enum Error {
5 VlqLeftover,
7 VlqNoValues,
9 VlqOverflow,
11 BadJson(serde_json::Error),
13 BadSegmentSize(u32),
15 BadSourceReference(u32),
17 BadNameReference(u32),
19}
20impl 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
40impl error::Error for Error {
41 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
42 if let Self::BadJson(err) = self { Some(err) } else { None }
43 }
44}
45
46pub type Result<T> = std::result::Result<T, Error>;
48
49impl From<serde_json::Error> for Error {
50 fn from(err: serde_json::Error) -> Error {
51 Error::BadJson(err)
52 }
53}