1use std::convert::Infallible;
16use std::env::VarError;
17use std::error::Error;
18use std::ffi::NulError;
19use std::io;
20use std::sync::mpsc::RecvError;
21use std::sync::{PoisonError, TryLockError};
22use std::{fmt, result};
23
24use cesu8::Cesu8DecodingError;
25use fs_extra;
26use serde_json;
27
28use futures::channel::oneshot::Canceled;
29
30pub type Result<T> = result::Result<T, J4RsError>;
31
32pub(crate) fn opt_to_res<T>(opt: Option<T>) -> Result<T> {
33 opt.ok_or(J4RsError::RustError("Option was found None while converting to result".to_string()))
34}
35
36#[allow(unused)]
37pub(crate) fn res_to_opt<T>(res: Result<T>) -> Option<T> {
38 if res.is_err() {
39 None
40 } else {
41 Some(res.unwrap())
42 }
43}
44
45#[derive(Debug, PartialEq, Eq, Clone)]
46pub enum J4RsError {
47 GeneralError(String),
48 JavaError(String),
49 JniError(String),
50 RustError(String),
51 ParseError(String),
52 Timeout,
53}
54
55impl fmt::Display for J4RsError {
56 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57 match self {
58 J4RsError::GeneralError(message) => write!(f, "{}", message),
59 J4RsError::JavaError(message) => write!(f, "{}", message),
60 J4RsError::JniError(message) => write!(f, "{}", message),
61 J4RsError::RustError(message) => write!(f, "{}", message),
62 J4RsError::ParseError(message) => write!(f, "{}", message),
63 &J4RsError::Timeout => write!(f, "Timeout"),
64 }
65 }
66}
67
68impl Error for J4RsError {
69 fn description(&self) -> &str {
70 match *self {
71 J4RsError::GeneralError(_) => "A general error occured",
72 J4RsError::JavaError(_) => "An error coming from Java occured",
73 J4RsError::JniError(_) => "A JNI error occured",
74 J4RsError::RustError(_) => "An error coming from Rust occured",
75 J4RsError::ParseError(_) => "A parsing error occured",
76 J4RsError::Timeout => "Timeout",
77 }
78 }
79}
80
81impl From<NulError> for J4RsError {
82 fn from(err: NulError) -> J4RsError {
83 J4RsError::JniError(format!("{:?}", err))
84 }
85}
86
87impl From<io::Error> for J4RsError {
88 fn from(err: io::Error) -> J4RsError {
89 J4RsError::GeneralError(format!("{:?}", err))
90 }
91}
92
93impl From<serde_json::Error> for J4RsError {
94 fn from(err: serde_json::Error) -> J4RsError {
95 J4RsError::ParseError(format!("{:?}", err))
96 }
97}
98
99impl From<fs_extra::error::Error> for J4RsError {
100 fn from(err: fs_extra::error::Error) -> J4RsError {
101 J4RsError::GeneralError(format!("{:?}", err))
102 }
103}
104
105impl<T> From<TryLockError<T>> for J4RsError {
106 fn from(err: TryLockError<T>) -> J4RsError {
107 J4RsError::GeneralError(format!("{:?}", err))
108 }
109}
110
111impl<T> From<PoisonError<T>> for J4RsError {
112 fn from(err: PoisonError<T>) -> J4RsError {
113 J4RsError::GeneralError(format!("{:?}", err))
114 }
115}
116
117impl From<Infallible> for J4RsError {
118 fn from(err: Infallible) -> J4RsError {
119 J4RsError::RustError(format!("{:?}", err))
120 }
121}
122
123impl From<RecvError> for J4RsError {
124 fn from(err: RecvError) -> J4RsError {
125 J4RsError::RustError(format!("{:?}", err))
126 }
127}
128
129impl From<VarError> for J4RsError {
130 fn from(err: VarError) -> J4RsError {
131 J4RsError::RustError(format!("{:?}", err))
132 }
133}
134
135impl From<Canceled> for J4RsError {
136 fn from(err: Canceled) -> J4RsError {
137 J4RsError::RustError(format!("{:?}", err))
138 }
139}
140
141impl From<Cesu8DecodingError> for J4RsError {
142 fn from(err: Cesu8DecodingError) -> J4RsError {
143 J4RsError::ParseError(format!("{:?}", err))
144 }
145}