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
use core::{
convert::Infallible,
fmt,
num::{ParseIntError, TryFromIntError},
};
use std::ffi::CStr;
pub(crate) type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
StringReprContainedNul,
ReadStringTruncated,
RemainderOutsideBounds,
ConversionOutsideRange,
IntParseFailed,
NoErrorPossible,
UnknownRoundingMode,
IMath {
code: creachadair_imath_sys::mp_result,
msg: Option<&'static str>,
},
NotCanonicalInteger,
}
impl std::error::Error for Error {}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::StringReprContainedNul => {
write!(f, "String representation contained a 'nul' character.")
}
Error::ReadStringTruncated => write!(
f,
"During conversion, the value representation was truncated."
),
Error::RemainderOutsideBounds => write!(
f,
"The result of a remainder operation was outside the expected bounds."
),
Error::ConversionOutsideRange => write!(
f,
"Attempted to convert to a primitive integer while outside its valid range."
),
Error::IntParseFailed => write!(f, "Integer parsing failed."),
Error::UnknownRoundingMode => write!(f, "Unknown value for an imath rounding mode."),
Error::NoErrorPossible => write!(
f,
"This error is not supposed to be possible. Please file an issue."
),
Error::IMath {
code,
msg: Some(msg),
} => write!(
f,
"imath error ({:?} \"{}\"): {}",
code,
get_creachadair_imath_sys_error_msg(*code),
msg
),
Error::IMath { code, msg: None } => write!(
f,
"imath error ({:?} \"{}\")",
code,
get_creachadair_imath_sys_error_msg(*code)
),
Error::NotCanonicalInteger => write!(
f,
"The rational value is not a canonical integer, in the form `n/1`."
),
}
}
}
impl From<TryFromIntError> for Error {
fn from(_src: TryFromIntError) -> Self {
Error::ConversionOutsideRange
}
}
impl From<Infallible> for Error {
fn from(_src: Infallible) -> Self {
Error::NoErrorPossible
}
}
impl From<ParseIntError> for Error {
fn from(_src: ParseIntError) -> Self {
Error::IntParseFailed
}
}
fn get_creachadair_imath_sys_error_msg(code: creachadair_imath_sys::mp_result) -> String {
let err_char_ptr = unsafe { creachadair_imath_sys::mp_error_string(code) };
unsafe { CStr::from_ptr(err_char_ptr) }
.to_string_lossy()
.into_owned()
}
macro_rules! imath_check_panic {
($arg:expr) => {
if $arg != unsafe { creachadair_imath_sys::MP_OK } {
panic!(
"{}",
Error::IMath {
code: $arg,
msg: None
}
);
}
};
($arg:expr, $msg:tt) => {
if $arg != unsafe { creachadair_imath_sys::MP_OK } {
panic!(
"{}",
Error::IMath {
code: $arg,
msg: Some($msg)
}
);
}
};
}