1use std::fmt;
2use std::sync::mpsc;
3
4mod bnr_error;
5mod usb_error;
6
7pub use bnr_error::*;
8pub use usb_error::*;
9
10use time as datetime;
11
12pub type Result<T> = std::result::Result<T, Error>;
14
15#[repr(C)]
17#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
18pub enum Error {
19 Generic(i64),
20 Serde(String),
21 Parsing(String),
22 Enum(String),
23 Usb(String),
24 Io(String),
25 Json(String),
26 Xfs(String),
27 DateTime(String),
28 Bnr(BnrError),
29 BnrUsb(UsbError),
30}
31
32impl From<serde_xml::Error> for Error {
33 fn from(err: serde_xml::Error) -> Self {
34 Self::Serde(format!("{err}"))
35 }
36}
37
38impl From<std::array::TryFromSliceError> for Error {
39 fn from(err: std::array::TryFromSliceError) -> Self {
40 Self::Parsing(format!("{err}"))
41 }
42}
43
44impl From<std::io::Error> for Error {
45 fn from(err: std::io::Error) -> Self {
46 Self::Io(format!("{err}"))
47 }
48}
49
50impl From<std::string::FromUtf8Error> for Error {
51 fn from(err: std::string::FromUtf8Error) -> Self {
52 Self::Parsing(format!("{err}"))
53 }
54}
55
56impl From<std::str::Utf8Error> for Error {
57 fn from(err: std::str::Utf8Error) -> Self {
58 Self::Parsing(format!("{err}"))
59 }
60}
61
62impl<T> From<std::sync::PoisonError<T>> for Error {
63 fn from(err: std::sync::PoisonError<T>) -> Self {
64 Self::Io(format!("{err}"))
65 }
66}
67
68impl From<datetime::Error> for Error {
69 fn from(err: datetime::Error) -> Self {
70 Self::DateTime(format!("{err}"))
71 }
72}
73
74impl From<datetime::error::Format> for Error {
75 fn from(err: datetime::error::Format) -> Self {
76 Self::DateTime(format!("{err}"))
77 }
78}
79
80impl From<datetime::error::Parse> for Error {
81 fn from(err: datetime::error::Parse) -> Self {
82 Self::DateTime(format!("{err}"))
83 }
84}
85
86impl From<serde_json::Error> for Error {
87 fn from(err: serde_json::Error) -> Self {
88 Self::Json(format!("{err}"))
89 }
90}
91
92impl<E> From<mpsc::SendError<E>> for Error {
93 fn from(err: mpsc::SendError<E>) -> Self {
94 Self::Io(format!("mpsc send error: {err}"))
95 }
96}
97
98impl From<std::time::SystemTimeError> for Error {
99 fn from(err: std::time::SystemTimeError) -> Self {
100 Self::DateTime(format!("time error: {err}"))
101 }
102}
103
104impl fmt::Display for Error {
105 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106 match self {
107 Self::Generic(err) => write!(f, "Generic error: {err}"),
108 Self::Serde(err) => write!(f, "Serialization error: {err}"),
109 Self::Parsing(err) => write!(f, "Parsing error: {err}"),
110 Self::Enum(err) => write!(f, "Enum error: {err}"),
111 Self::Usb(err) => write!(f, "USB error: {err}"),
112 Self::Io(err) => write!(f, "I/O error: {err}"),
113 Self::Xfs(err) => write!(f, "XFS error: {err}"),
114 Self::DateTime(err) => write!(f, "DateTime error: {err}"),
115 Self::Bnr(err) => write!(f, "BNR error: {err}"),
116 Self::BnrUsb(err) => write!(f, "BNR USB error: {err}"),
117 Self::Json(err) => write!(f, "JSON error: {err}"),
118 }
119 }
120}
121
122impl From<nusb::transfer::TransferError> for Error {
123 fn from(err: nusb::transfer::TransferError) -> Self {
124 Self::Usb(format!("{err}"))
125 }
126}