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
//! Error types for arithmetic, conversion, and parse operations
//! on [`JsonInt`] and [`JsonUInt`].

use crate::num::{JsonInt, JsonUInt};
use std::{
    fmt::{self, Display},
    num::IntErrorKind,
};
use thiserror::Error;

/// Errors raised when trying to convert between JSON integer types
/// or between a JSON int and a regular Rust int, or when performing
/// arithmetic on JSON ints that would over-/underflow.
#[derive(Debug, Error)]
pub struct JsonIntOverflowError {
    kind: JsonIntOverflowKind,
}

/// Errors raised when trying to parse JSON integer types from strings.
#[derive(Debug, PartialEq, Eq, Error, Clone)]
pub struct JsonIntParseError {
    kind: JsonIntParseErrorKind,
}

impl JsonIntOverflowError {
    pub(crate) fn int_pos_overflow(src: i64) -> Self {
        Self {
            kind: JsonIntOverflowKind::IntPos(src),
        }
    }

    pub(crate) fn int_pos_overflow_u(src: u64) -> Self {
        Self {
            kind: JsonIntOverflowKind::IntPosU(src),
        }
    }

    pub(crate) fn int_neg_overflow(src: i64) -> Self {
        Self {
            kind: JsonIntOverflowKind::IntNeg(src),
        }
    }

    pub(crate) fn uint_pos_overflow(src: u64) -> Self {
        Self {
            kind: JsonIntOverflowKind::UIntPos(src),
        }
    }

    pub(crate) fn negative_uint(src: i64) -> Self {
        Self {
            kind: JsonIntOverflowKind::UIntNeg(src),
        }
    }

    pub(crate) fn zero_non_zero_uint() -> Self {
        Self {
            kind: JsonIntOverflowKind::NonZeroUIntZero,
        }
    }
}

impl JsonIntParseError {
    pub(crate) fn int_parse_error(src: &str, err: &IntErrorKind) -> Self {
        Self {
            kind: match err {
                IntErrorKind::PosOverflow => JsonIntParseErrorKind::IntPosOverflow(src.to_string()),
                IntErrorKind::NegOverflow => JsonIntParseErrorKind::IntNegOverflow(src.to_string()),
                IntErrorKind::Zero => unreachable!(), // Zero is always a valid JsonInt value.
                _ => JsonIntParseErrorKind::InvalidFormat(src.to_string()),
            },
        }
    }

    pub(crate) fn parse_conversion_err(src: &str, err: &JsonIntOverflowError) -> Self {
        Self {
            kind: match err.kind {
                JsonIntOverflowKind::IntPosU(_) | JsonIntOverflowKind::IntPos(_) => {
                    JsonIntParseErrorKind::IntPosOverflow(src.to_string())
                }
                JsonIntOverflowKind::IntNeg(_) => JsonIntParseErrorKind::IntNegOverflow(src.to_string()),
                JsonIntOverflowKind::UIntPos(_) => JsonIntParseErrorKind::UIntPosOverflow(src.to_string()),
                JsonIntOverflowKind::UIntNeg(_) => JsonIntParseErrorKind::UIntNegOverflow(src.to_string()),
                JsonIntOverflowKind::NonZeroUIntZero => JsonIntParseErrorKind::NonZeroUIntZero(src.to_string()),
            },
        }
    }

    pub(crate) fn uint_parse_error(src: &str, err: &IntErrorKind) -> Self {
        Self {
            kind: match err {
                IntErrorKind::PosOverflow => JsonIntParseErrorKind::UIntPosOverflow(src.to_string()),
                IntErrorKind::NegOverflow => JsonIntParseErrorKind::UIntNegOverflow(src.to_string()),
                IntErrorKind::Zero => unreachable!(), // Zero is always a valid JsonUInt value.
                _ => JsonIntParseErrorKind::InvalidFormat(src.to_string()),
            },
        }
    }

    pub(crate) fn non_zero_uint_parse_error(src: &str, err: &IntErrorKind) -> Self {
        Self {
            kind: match err {
                IntErrorKind::PosOverflow => JsonIntParseErrorKind::UIntPosOverflow(src.to_string()),
                IntErrorKind::NegOverflow => JsonIntParseErrorKind::UIntNegOverflow(src.to_string()),
                IntErrorKind::Zero => JsonIntParseErrorKind::NonZeroUIntZero(src.to_string()),
                _ => JsonIntParseErrorKind::InvalidFormat(src.to_string()),
            },
        }
    }
}

#[derive(Debug)]
enum JsonIntOverflowKind {
    IntPos(i64),
    IntPosU(u64),
    IntNeg(i64),
    UIntPos(u64),
    UIntNeg(i64),
    NonZeroUIntZero,
}

#[derive(Debug, PartialEq, Eq, Clone)]
enum JsonIntParseErrorKind {
    IntPosOverflow(String),
    IntNegOverflow(String),
    UIntPosOverflow(String),
    UIntNegOverflow(String),
    NonZeroUIntZero(String),
    InvalidFormat(String),
}

impl Display for JsonIntOverflowError {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.kind.fmt(f)
    }
}

impl Display for JsonIntParseError {
    #[inline(always)]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.kind.fmt(f)
    }
}

impl Display for JsonIntOverflowKind {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::IntPos(src) => write!(
                f,
                "value {src} is above the range of JsonInt values [{}..{}]",
                JsonInt::MIN,
                JsonInt::MAX
            ),
            Self::IntPosU(src) => write!(
                f,
                "value {src} is above the range of JsonInt values [{}..{}]",
                JsonInt::MIN,
                JsonInt::MAX
            ),
            Self::IntNeg(src) => write!(
                f,
                "value {src} is below the range of JsonInt values [{}..{}]",
                JsonInt::MIN,
                JsonInt::MAX
            ),
            Self::UIntPos(src) => write!(
                f,
                "value {src} is above the range of JsonUInt values [0..{}]",
                JsonUInt::MAX
            ),
            Self::UIntNeg(src) => {
                write!(f, "attempt to convert a negative value {src} into a JsonUInt",)
            }
            Self::NonZeroUIntZero => {
                write!(f, "attempt to convert a zero value into a JsonNonZeroUInt",)
            }
        }
    }
}

impl Display for JsonIntParseErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::IntPosOverflow(src) => write!(
                f,
                "string '{src}' represents a value above the range of JsonInt values [{}..{}]",
                JsonInt::MIN,
                JsonInt::MAX
            ),
            Self::IntNegOverflow(src) => write!(
                f,
                "string '{src}' represents a value below the range of JsonInt values [{}..{}]",
                JsonInt::MIN,
                JsonInt::MAX
            ),
            Self::UIntPosOverflow(src) => write!(
                f,
                "string '{src}' represents a value above the range of JsonUInt values [0..{}]",
                JsonUInt::MAX
            ),
            Self::UIntNegOverflow(src) => {
                write!(
                    f,
                    "string '{src}' represents a value below the range of JsonUInt values [0..{}]",
                    JsonUInt::MAX
                )
            }
            Self::NonZeroUIntZero(src) => write!(
                f,
                "string '{src}' represents a zero value, which is not a valid JsonNonZeroUInt"
            ),
            Self::InvalidFormat(src) => write!(f, "string '{src}' is not a valid representation of a JSON integer"),
        }
    }
}