1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2018 astonbitecode
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::{fmt, result};
use std::convert::Infallible;
use std::env::VarError;
use std::error::Error;
use std::ffi::NulError;
use std::io;
use std::sync::{PoisonError, TryLockError};
use std::sync::mpsc::RecvError;

use fs_extra;
use serde_json;

use futures::channel::oneshot::Canceled;

pub type Result<T> = result::Result<T, J4RsError>;

pub(crate) fn opt_to_res<T>(opt: Option<T>) -> Result<T> {
    opt.ok_or(J4RsError::RustError(format!(
        "Option was found None while converting to result"
    )))
}

#[allow(unused)]
pub(crate) fn res_to_opt<T>(res: Result<T>) -> Option<T> {
    if res.is_err() {
        None
    } else {
        Some(res.unwrap())
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum J4RsError {
    GeneralError(String),
    JavaError(String),
    JniError(String),
    RustError(String),
    ParseError(String),
    Timeout,
}

impl fmt::Display for J4RsError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &J4RsError::GeneralError(ref message) => write!(f, "{}", message),
            &J4RsError::JavaError(ref message) => write!(f, "{}", message),
            &J4RsError::JniError(ref message) => write!(f, "{}", message),
            &J4RsError::RustError(ref message) => write!(f, "{}", message),
            &J4RsError::ParseError(ref message) => write!(f, "{}", message),
            &J4RsError::Timeout => write!(f, "Timeout"),
        }
    }
}

impl Error for J4RsError {
    fn description(&self) -> &str {
        match *self {
            J4RsError::GeneralError(_) => "A general error occured",
            J4RsError::JavaError(_) => "An error coming from Java occured",
            J4RsError::JniError(_) => "A JNI error occured",
            J4RsError::RustError(_) => "An error coming from Rust occured",
            J4RsError::ParseError(_) => "A parsing error occured",
            J4RsError::Timeout => "Timeout",
        }
    }
}

impl From<NulError> for J4RsError {
    fn from(err: NulError) -> J4RsError {
        J4RsError::JniError(format!("{:?}", err))
    }
}

impl From<io::Error> for J4RsError {
    fn from(err: io::Error) -> J4RsError {
        J4RsError::GeneralError(format!("{:?}", err))
    }
}

impl From<serde_json::Error> for J4RsError {
    fn from(err: serde_json::Error) -> J4RsError {
        J4RsError::ParseError(format!("{:?}", err))
    }
}

impl From<fs_extra::error::Error> for J4RsError {
    fn from(err: fs_extra::error::Error) -> J4RsError {
        J4RsError::GeneralError(format!("{:?}", err))
    }
}

impl<T> From<TryLockError<T>> for J4RsError {
    fn from(err: TryLockError<T>) -> J4RsError {
        J4RsError::GeneralError(format!("{:?}", err))
    }
}

impl<T> From<PoisonError<T>> for J4RsError {
    fn from(err: PoisonError<T>) -> J4RsError {
        J4RsError::GeneralError(format!("{:?}", err))
    }
}

impl From<Infallible> for J4RsError {
    fn from(err: Infallible) -> J4RsError {
        J4RsError::RustError(format!("{:?}", err))
    }
}

impl From<RecvError> for J4RsError {
    fn from(err: RecvError) -> J4RsError {
        J4RsError::RustError(format!("{:?}", err))
    }
}

impl From<VarError> for J4RsError {
    fn from(err: VarError) -> J4RsError {
        J4RsError::RustError(format!("{:?}", err))
    }
}

impl From<Canceled> for J4RsError {
    fn from(err: Canceled) -> J4RsError {
        J4RsError::RustError(format!("{:?}", err))
    }
}