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>;

/// Error used in `reckoner`, usually originating from `creachadair-imath-sys`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
    /// When converting from a string representation, the given string contained
    /// a zero-byte that was not at the end.
    StringReprContainedNul,
    /// An error occurred when converting a string to a value, and the
    /// output was truncated.
    ReadStringTruncated,
    /// The result of a remainder operation was outside the expected bounds.
    RemainderOutsideBounds,
    /// Could not convert a value to a primitive integer type because it was
    /// outside the range.
    ConversionOutsideRange,
    /// Integer parse failed.
    IntParseFailed,
    /// It impossible for this error to occur.
    NoErrorPossible,
    /// Unknown value for an imath rounding mode.
    UnknownRoundingMode,
    /// Internal error from `creachadair_imath_sys`.
    IMath {
        /// Internal `creachadair_imath_sys` error code.
        code: creachadair_imath_sys::mp_result,
        /// Custom message to display.
        msg: Option<&'static str>,
    },
    /// The rational value is not a canonical integer, in the form `n/1`.
    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 {
    // This is safe because the function will always return a cstring with static
    // lifetime, even if the error code is not a recognized value.
    let err_char_ptr = unsafe { creachadair_imath_sys::mp_error_string(code) };

    // This function is safe bc I checked the static string that `mp_error_string`
    // return and they conform to the condition of ending with nul byte. Other
    // conditions around lifetimes, and the borrowed cstring being edited are not an
    // issue because it is converted to a `String` immediately.
    unsafe { CStr::from_ptr(err_char_ptr) }
        .to_string_lossy()
        .into_owned()
}

macro_rules! imath_check_panic {
    ($arg:expr) => {
        // Accessing this is safe bc the MP_OK value is only ever used as an error
        // condition.
        if $arg != unsafe { creachadair_imath_sys::MP_OK } {
            panic!(
                "{}",
                Error::IMath {
                    code: $arg,
                    msg: None
                }
            );
        }
    };

    ($arg:expr, $msg:tt) => {
        // Accessing this is safe bc the MP_OK value is only ever used as an error
        // condition.
        if $arg != unsafe { creachadair_imath_sys::MP_OK } {
            panic!(
                "{}",
                Error::IMath {
                    code: $arg,
                    msg: Some($msg)
                }
            );
        }
    };
}