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
//! C-compatible error type.

/// Error code, indicating success or failure.
///
/// Success, or no error, is 0, while error messages are designating
/// by an error code of less than 0. This is to be compatible with C
/// conventions.
#[repr(i8)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum ErrorCode {
    /// No error, success.
    Success = 0,
    /// Integral overflow occurred during numeric parsing.
    ///
    /// Numeric overflow takes precedence over the presence of an invalid
    /// digit.
    Overflow = -1,
    /// Invalid digit found before string termination.
    InvalidDigit = -2,
}

/// C-compatible error for FFI.
#[repr(C)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct Error {
    /// Error code designating the type of error occurred.
    pub code: ErrorCode,
    /// Optional position within the buffer for the error.
    pub index: usize,
}

/// Check if the error code is successful.
#[inline(always)]
pub extern "C" fn is_success(error: Error) -> bool {
    error.code == ErrorCode::Success
}

/// Check if the error code designates integer overflow.
#[inline(always)]
pub extern "C" fn is_overflow(error: Error) -> bool {
    error.code == ErrorCode::Overflow
}

/// Check if the error code designates an invalid digit was encountered.
#[inline(always)]
pub extern "C" fn is_invalid_digit(error: Error) -> bool {
    error.code == ErrorCode::InvalidDigit
}

/// Helper function to create a success message.
#[inline(always)]
pub(crate) fn success() -> Error {
    Error { code: ErrorCode::Success, index: 0 }
}

/// Helper function to create an overflow error.
#[inline(always)]
pub(crate) fn overflow_error() -> Error {
    Error { code: ErrorCode::Overflow, index: 0 }
}

/// Helper function to create an invalid digit error.
#[inline(always)]
pub(crate) fn invalid_digit_error(index: usize) -> Error {
    Error { code: ErrorCode::InvalidDigit, index: index }
}