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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//! Error handling in Rust called from R.

use std::convert::Infallible;

use crate::conversions::try_into_int::ConversionError;
use crate::robj::Types;
use crate::{throw_r_error, Robj};

/// Throw an R error if a result is an error.
#[doc(hidden)]
pub fn unwrap_or_throw<T>(r: std::result::Result<T, &'static str>) -> T {
    match r {
        Err(e) => {
            throw_r_error(e);
        }
        Ok(v) => v,
    }
}

#[doc(hidden)]
pub fn unwrap_or_throw_error<T>(r: std::result::Result<T, Error>) -> T {
    match r {
        Err(e) => {
            throw_r_error(e.to_string());
        }
        Ok(v) => v,
    }
}

#[derive(Debug, PartialEq)]
pub enum Error {
    Panic(Robj),
    NotFound(Robj),
    EvalError(Robj),
    ParseError(Robj),
    NamesLengthMismatch(Robj),

    ExpectedNull(Robj),
    ExpectedSymbol(Robj),
    ExpectedPairlist(Robj),
    ExpectedFunction(Robj),
    ExpectedEnvironment(Robj),
    ExpectedPromise(Robj),
    ExpectedLanguage(Robj),
    ExpectedSpecial(Robj),
    ExpectedBuiltin(Robj),
    ExpectedRstr(Robj),
    ExpectedLogical(Robj),
    ExpectedInteger(Robj),
    ExpectedReal(Robj),
    ExpectedComplex(Robj),
    ExpectedString(Robj),
    ExpectedDot(Robj),
    ExpectedAny(Robj),
    ExpectedList(Robj),
    ExpectedExpression(Robj),
    ExpectedBytecode(Robj),
    ExpectedExternalPtr(Robj),
    ExpectedWeakRef(Robj),
    ExpectedRaw(Robj),
    ExpectedS4(Robj),
    ExpectedPrimitive(Robj),

    ExpectedScalar(Robj),
    ExpectedVector(Robj),
    ExpectedMatrix(Robj),
    ExpectedMatrix3D(Robj),
    ExpectedNumeric(Robj),
    ExpectedAltrep(Robj),
    ExpectedDataframe(Robj),

    OutOfRange(Robj),
    MustNotBeNA(Robj),
    ExpectedWholeNumber(Robj, ConversionError),
    ExpectedNonZeroLength(Robj),
    OutOfLimits(Robj),
    TypeMismatch(Robj),
    NamespaceNotFound(Robj),
    NoGraphicsDevices(Robj),

    ExpectedExternalPtrType(Robj, String),
    ExpectedExternalNonNullPtr(Robj),
    ExpectedExternalPtrReference,
    Other(String),

    #[cfg(feature = "ndarray")]
    NDArrayShapeError(ndarray::ShapeError),

    #[cfg(feature = "either")]
    EitherError(Box<Error>, Box<Error>),
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::Panic(robj) => write!(f, "Panic detected {:?}.", robj),
            Error::NotFound(robj) => write!(f, "Not found. {:?}", robj),
            Error::EvalError(robj) => write!(f, "Evaluation error in {:?}.", robj),
            Error::ParseError(code) => write!(f, "Parse error in {:?}.", code),
            Error::NamesLengthMismatch(robj) => {
                write!(f, "Length of names does not match vector. {:?}", robj)
            }

            Error::ExpectedNull(robj) => write!(f, "Expected Null got {:?}", robj.rtype()),
            Error::ExpectedSymbol(robj) => write!(f, "Expected Symbol got {:?}", robj.rtype()),
            Error::ExpectedPairlist(robj) => write!(f, "Expected Pairlist got {:?}", robj.rtype()),
            Error::ExpectedFunction(robj) => write!(f, "Expected Function got {:?}", robj.rtype()),
            Error::ExpectedEnvironment(robj) => {
                write!(f, "Expected Environment got {:?}", robj.rtype())
            }
            Error::ExpectedPromise(robj) => write!(f, "Expected Promise got {:?}", robj.rtype()),
            Error::ExpectedLanguage(robj) => write!(f, "Expected Language got {:?}", robj.rtype()),
            Error::ExpectedSpecial(robj) => write!(f, "Expected Special got {:?}", robj.rtype()),
            Error::ExpectedBuiltin(robj) => write!(f, "Expected Builtin got {:?}", robj.rtype()),
            Error::ExpectedRstr(robj) => {
                write!(f, "Expected Rstr got {:?}", robj.rtype())
            }
            Error::ExpectedLogical(robj) => write!(f, "Expected Logicals got {:?}", robj.rtype()),
            Error::ExpectedInteger(robj) => write!(f, "Expected Integers got {:?}", robj.rtype()),
            Error::ExpectedReal(robj) => write!(f, "Expected Doubles got {:?}", robj.rtype()),
            Error::ExpectedComplex(robj) => write!(f, "Expected Complexes got {:?}", robj.rtype()),
            Error::ExpectedString(robj) => write!(f, "Expected Strings got {:?}", robj.rtype()),
            Error::ExpectedDot(robj) => write!(f, "Expected Dot got {:?}", robj.rtype()),
            Error::ExpectedAny(robj) => write!(f, "Expected Any got {:?}", robj.rtype()),
            Error::ExpectedList(robj) => write!(f, "Expected List got {:?}", robj.rtype()),
            Error::ExpectedExpression(robj) => {
                write!(f, "Expected Expression got {:?}", robj.rtype())
            }
            Error::ExpectedBytecode(robj) => write!(f, "Expected Bytecode got {:?}", robj.rtype()),
            Error::ExpectedExternalPtr(robj) => {
                write!(f, "Expected ExternalPtr got {:?}", robj.rtype())
            }
            Error::ExpectedWeakRef(robj) => write!(f, "Expected WeakRef got {:?}", robj.rtype()),
            Error::ExpectedRaw(robj) => write!(f, "Expected Raw got {:?}", robj.rtype()),
            Error::ExpectedS4(robj) => write!(f, "Expected S4 got {:?}", robj.rtype()),
            Error::ExpectedPrimitive(robj) => {
                write!(f, "Expected Primitive got {:?}", robj.rtype())
            }

            Error::ExpectedScalar(robj) => write!(f, "Expected Scalar, got {:?}", robj.rtype()),
            Error::ExpectedVector(robj) => write!(f, "Expected Vector, got {:?}", robj.rtype()),
            Error::ExpectedMatrix(robj) => write!(f, "Expected Matrix, got {:?}", robj.rtype()),
            Error::ExpectedMatrix3D(robj) => write!(f, "Expected Matrix3D, got {:?}", robj.rtype()),
            Error::ExpectedNumeric(robj) => write!(f, "Expected Numeric, got {:?}", robj.rtype()),
            Error::ExpectedAltrep(robj) => write!(f, "Expected Altrep, got {:?}", robj.rtype()),
            Error::ExpectedDataframe(robj) => {
                write!(f, "Expected Dataframe, got {:?}", robj.rtype())
            }

            Error::OutOfRange(_robj) => write!(f, "Out of range."),
            Error::MustNotBeNA(_robj) => write!(f, "Must not be NA."),
            Error::ExpectedNonZeroLength(_robj) => write!(f, "Expected non zero length"),
            Error::OutOfLimits(robj) => write!(f, "The value is too big: {:?}", robj),
            Error::TypeMismatch(_robj) => write!(f, "Type mismatch"),

            Error::NamespaceNotFound(robj) => write!(f, "Namespace {:?} not found", robj),
            Error::ExpectedExternalPtrType(_robj, type_name) => {
                write!(f, "Incorrect external pointer type {}", type_name)
            }
            Error::ExpectedExternalNonNullPtr(robj) => {
                write!(
                    f,
                    "expected non-null pointer in externalptr, instead {:?}",
                    robj
                )
            }
            Error::ExpectedExternalPtrReference => {
                write!(f, "It is only possible to return a reference to self.")
            }
            Error::NoGraphicsDevices(_robj) => write!(f, "No graphics devices active."),
            Error::Other(str) => write!(f, "{}", str),

            Error::ExpectedWholeNumber(robj, conversion_error) => {
                write!(
                    f,
                    "Failed to convert a float to a whole number: {}. Actual value received: {:?}",
                    conversion_error, robj
                )
            }

            #[cfg(feature = "ndarray")]
            Error::NDArrayShapeError(shape_error) => {
                write!(f, "NDArray failed with error: {}.", shape_error)
            }

            #[cfg(feature = "either")]
            Error::EitherError(left_err, right_err) => {
                write!(
                    f,
                    "Both cases of Either errored. Left: '{}'; Right: '{}'.",
                    left_err, right_err
                )
            }
        }
    }
}
pub type Result<T> = std::result::Result<T, Error>;

// impl std::fmt::Display for Error {
//     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
//         write!(f, "{:?}", self)
//     }
// }

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        None
    }
}

impl From<Box<dyn std::error::Error>> for Error {
    fn from(err: Box<dyn std::error::Error>) -> Error {
        Error::Other(format!("{}", err))
    }
}

impl From<&str> for Error {
    fn from(err: &str) -> Error {
        Error::Other(err.to_string())
    }
}

impl From<String> for Error {
    fn from(err: String) -> Error {
        Error::Other(err)
    }
}

// NoneError is unstable.
//
// impl From<std::option::NoneError> for Error {
//     fn from(err: std::option::NoneError) -> Error {
//         Error::None
//     }
// }

impl From<Infallible> for Error {
    fn from(_: Infallible) -> Self {
        Error::Other("".to_string())
    }
}