1use std::fmt;
2use std::num;
3use std::io;
4use std::str::Utf8Error;
5use std::string::FromUtf8Error;
6use rmp::decode::{DecodeStringError, ValueReadError, MarkerReadError};
7use rmp::encode::ValueWriteError;
8
9#[derive(Debug)]
10pub enum BsonErr {
11 ParseError(String),
12 ParseIntError(Box<num::ParseIntError>),
13 DecodeIntUnknownByte,
14 IOErr(Box<io::Error>),
15 UTF8Error(Box<Utf8Error>),
16 FromUTF8Error(Box<FromUtf8Error>),
17 TypeNotComparable(String, String),
18 RmpWriteError(Box<ValueWriteError>),
19 RmpReadError(Box<ValueReadError>),
20 RmpMarkerReadError(Box<MarkerReadError>),
21 DecodeStringErr(String),
22}
23
24pub mod parse_error_reason {
25
26 pub static OBJECT_ID_LEN: &str = "length of ObjectId should be 12";
27 pub static OBJECT_ID_HEX_DECODE_ERROR: &str = "decode error failed for ObjectID";
28 pub static UNEXPECTED_DOCUMENT_FLAG: &str = "unexpected flag for document";
29 pub static UNEXPECTED_PAGE_HEADER: &str = "unexpected page header";
30 pub static UNEXPECTED_PAGE_TYPE: &str = "unexpected page type";
31 pub static UNEXPECTED_HEADER_FOR_BTREE_PAGE: &str = "unexpected header for btree page";
32 pub static KEY_TY_SHOULD_NOT_BE_ZERO: &str = "type name of KEY should not be zero";
33
34}
35
36impl fmt::Display for BsonErr {
37
38 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39 match self {
40 BsonErr::ParseError(reason) => write!(f, "ParseError: {}", reason),
41 BsonErr::ParseIntError(parse_int_err) => parse_int_err.fmt(f),
42 BsonErr::DecodeIntUnknownByte => write!(f, "DecodeIntUnknownByte"),
43 BsonErr::IOErr(io_err) => std::fmt::Display::fmt(&io_err, f),
44 BsonErr::UTF8Error(utf8_err) => std::fmt::Display::fmt(&utf8_err, f),
45 BsonErr::TypeNotComparable(expected, actual) =>
46 write!(f, "TypeNotComparable(expected: {}, actual: {})", expected, actual),
47 BsonErr::FromUTF8Error(err) => write!(f, "{}", err),
48 BsonErr::RmpWriteError(err) => write!(f, "{}", err),
49 BsonErr::RmpReadError(err) => write!(f, "{}", err),
50 BsonErr::DecodeStringErr(err) => write!(f, "{}", err),
51 BsonErr::RmpMarkerReadError(_err) => write!(f, "RmpMarkerReadError"),
52 }
53 }
54
55}
56
57impl From<io::Error> for BsonErr {
58
59 fn from(error: io::Error) -> Self {
60 BsonErr::IOErr(Box::new(error))
61 }
62
63}
64
65impl From<Utf8Error> for BsonErr {
66
67 fn from(error: Utf8Error) -> Self {
68 BsonErr::UTF8Error(Box::new(error))
69 }
70
71}
72
73impl From<num::ParseIntError> for BsonErr {
74
75 fn from(error: num::ParseIntError) -> Self {
76 BsonErr::ParseIntError(Box::new(error))
77 }
78
79}
80
81impl From<FromUtf8Error> for BsonErr {
82
83 fn from(error: FromUtf8Error) -> Self {
84 BsonErr::FromUTF8Error(Box::new(error))
85 }
86
87}
88
89impl From<ValueWriteError> for BsonErr {
90
91 fn from(error: ValueWriteError) -> Self {
92 BsonErr::RmpWriteError(Box::new(error))
93 }
94
95}
96
97impl From<ValueReadError> for BsonErr {
98
99 fn from(error: ValueReadError) -> Self {
100 BsonErr::RmpReadError(Box::new(error))
101 }
102
103}
104
105impl<'a> From<DecodeStringError<'a>> for BsonErr {
106 fn from(err: DecodeStringError<'a>) -> Self {
107 BsonErr::DecodeStringErr(err.to_string())
108 }
109}
110
111impl From<MarkerReadError> for BsonErr {
112 fn from(err: MarkerReadError) -> Self {
113 BsonErr::RmpMarkerReadError(Box::new(err))
114 }
115}
116
117impl std::error::Error for BsonErr {
118
119}