ng_oxigraph/oxsdatatypes/
integer.rs1use crate::oxsdatatypes::{Boolean, Decimal, Double, Float};
2use serde::{Deserialize, Serialize};
3use std::fmt;
4use std::num::ParseIntError;
5use std::str::FromStr;
6
7#[derive(
11 Debug, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize,
12)]
13#[repr(transparent)]
14pub struct Integer {
15 value: i64,
16}
17
18impl Integer {
19 pub const MAX: Self = Self { value: i64::MAX };
20 pub const MIN: Self = Self { value: i64::MIN };
21
22 #[inline]
23 #[must_use]
24 pub fn from_be_bytes(bytes: [u8; 8]) -> Self {
25 Self {
26 value: i64::from_be_bytes(bytes),
27 }
28 }
29
30 #[inline]
31 #[must_use]
32 pub fn to_be_bytes(self) -> [u8; 8] {
33 self.value.to_be_bytes()
34 }
35
36 #[inline]
40 #[must_use]
41 pub fn checked_add(self, rhs: impl Into<Self>) -> Option<Self> {
42 Some(Self {
43 value: self.value.checked_add(rhs.into().value)?,
44 })
45 }
46
47 #[inline]
51 #[must_use]
52 pub fn checked_sub(self, rhs: impl Into<Self>) -> Option<Self> {
53 Some(Self {
54 value: self.value.checked_sub(rhs.into().value)?,
55 })
56 }
57
58 #[inline]
62 #[must_use]
63 pub fn checked_mul(self, rhs: impl Into<Self>) -> Option<Self> {
64 Some(Self {
65 value: self.value.checked_mul(rhs.into().value)?,
66 })
67 }
68
69 #[inline]
73 #[must_use]
74 pub fn checked_div(self, rhs: impl Into<Self>) -> Option<Self> {
75 Some(Self {
76 value: self.value.checked_div(rhs.into().value)?,
77 })
78 }
79
80 #[inline]
84 #[must_use]
85 pub fn checked_rem(self, rhs: impl Into<Self>) -> Option<Self> {
86 Some(Self {
87 value: self.value.checked_rem(rhs.into().value)?,
88 })
89 }
90
91 #[inline]
95 #[must_use]
96 pub fn checked_rem_euclid(self, rhs: impl Into<Self>) -> Option<Self> {
97 Some(Self {
98 value: self.value.checked_rem_euclid(rhs.into().value)?,
99 })
100 }
101
102 #[inline]
106 #[must_use]
107 pub fn checked_neg(self) -> Option<Self> {
108 Some(Self {
109 value: self.value.checked_neg()?,
110 })
111 }
112
113 #[inline]
117 #[must_use]
118 pub fn checked_abs(self) -> Option<Self> {
119 Some(Self {
120 value: self.value.checked_abs()?,
121 })
122 }
123
124 #[inline]
125 #[must_use]
126 pub const fn is_negative(self) -> bool {
127 self.value < 0
128 }
129
130 #[inline]
131 #[must_use]
132 pub const fn is_positive(self) -> bool {
133 self.value > 0
134 }
135
136 #[inline]
138 #[must_use]
139 pub fn is_identical_with(self, other: Self) -> bool {
140 self == other
141 }
142}
143
144impl From<bool> for Integer {
145 #[inline]
146 fn from(value: bool) -> Self {
147 Self {
148 value: value.into(),
149 }
150 }
151}
152
153impl From<i8> for Integer {
154 #[inline]
155 fn from(value: i8) -> Self {
156 Self {
157 value: value.into(),
158 }
159 }
160}
161
162impl From<i16> for Integer {
163 #[inline]
164 fn from(value: i16) -> Self {
165 Self {
166 value: value.into(),
167 }
168 }
169}
170
171impl From<i32> for Integer {
172 #[inline]
173 fn from(value: i32) -> Self {
174 Self {
175 value: value.into(),
176 }
177 }
178}
179
180impl From<i64> for Integer {
181 #[inline]
182 fn from(value: i64) -> Self {
183 Self { value }
184 }
185}
186
187impl From<u8> for Integer {
188 #[inline]
189 fn from(value: u8) -> Self {
190 Self {
191 value: value.into(),
192 }
193 }
194}
195
196impl From<u16> for Integer {
197 #[inline]
198 fn from(value: u16) -> Self {
199 Self {
200 value: value.into(),
201 }
202 }
203}
204
205impl From<u32> for Integer {
206 #[inline]
207 fn from(value: u32) -> Self {
208 Self {
209 value: value.into(),
210 }
211 }
212}
213
214impl From<Boolean> for Integer {
215 #[inline]
216 fn from(value: Boolean) -> Self {
217 bool::from(value).into()
218 }
219}
220
221impl From<Integer> for i64 {
222 #[inline]
223 fn from(value: Integer) -> Self {
224 value.value
225 }
226}
227
228impl FromStr for Integer {
229 type Err = ParseIntError;
230
231 #[inline]
232 fn from_str(input: &str) -> Result<Self, Self::Err> {
233 Ok(i64::from_str(input)?.into())
234 }
235}
236
237impl fmt::Display for Integer {
238 #[inline]
239 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
240 self.value.fmt(f)
241 }
242}
243
244impl TryFrom<Float> for Integer {
245 type Error = TooLargeForIntegerError;
246
247 #[inline]
248 fn try_from(value: Float) -> Result<Self, Self::Error> {
249 Decimal::try_from(value)
250 .map_err(|_| TooLargeForIntegerError)?
251 .try_into()
252 }
253}
254
255impl TryFrom<Double> for Integer {
256 type Error = TooLargeForIntegerError;
257
258 #[inline]
259 fn try_from(value: Double) -> Result<Self, Self::Error> {
260 Decimal::try_from(value)
261 .map_err(|_| TooLargeForIntegerError)?
262 .try_into()
263 }
264}
265
266#[derive(Debug, Clone, Copy, thiserror::Error)]
270#[error("Value too large for xsd:integer internal representation")]
271pub struct TooLargeForIntegerError;
272
273#[cfg(test)]
274#[allow(clippy::panic_in_result_fn)]
275mod tests {
276 use super::*;
277
278 #[test]
279 fn from_str() -> Result<(), ParseIntError> {
280 assert_eq!(Integer::from_str("0")?.to_string(), "0");
281 assert_eq!(Integer::from_str("-0")?.to_string(), "0");
282 assert_eq!(Integer::from_str("123")?.to_string(), "123");
283 assert_eq!(Integer::from_str("-123")?.to_string(), "-123");
284 Integer::from_str("123456789123456789123456789123456789123456789").unwrap_err();
285 Ok(())
286 }
287
288 #[test]
289 fn from_float() -> Result<(), ParseIntError> {
290 assert_eq!(
291 Integer::try_from(Float::from(0.)).ok(),
292 Some(Integer::from_str("0")?)
293 );
294 assert_eq!(
295 Integer::try_from(Float::from(-0.)).ok(),
296 Some(Integer::from_str("0")?)
297 );
298 assert_eq!(
299 Integer::try_from(Float::from(-123.1)).ok(),
300 Some(Integer::from_str("-123")?)
301 );
302 Integer::try_from(Float::from(f32::NAN)).unwrap_err();
303 Integer::try_from(Float::from(f32::INFINITY)).unwrap_err();
304 Integer::try_from(Float::from(f32::NEG_INFINITY)).unwrap_err();
305 Integer::try_from(Float::from(f32::MIN)).unwrap_err();
306 Integer::try_from(Float::from(f32::MAX)).unwrap_err();
307 assert!(
308 Integer::try_from(Float::from(1_672_507_300_000.))
309 .unwrap()
310 .checked_sub(Integer::from_str("1672507300000")?)
311 .unwrap()
312 .checked_abs()
313 .unwrap()
314 < Integer::from(1_000_000)
315 );
316 Ok(())
317 }
318
319 #[test]
320 fn from_double() -> Result<(), ParseIntError> {
321 assert_eq!(
322 Integer::try_from(Double::from(0.0)).ok(),
323 Some(Integer::from_str("0")?)
324 );
325 assert_eq!(
326 Integer::try_from(Double::from(-0.0)).ok(),
327 Some(Integer::from_str("0")?)
328 );
329 assert_eq!(
330 Integer::try_from(Double::from(-123.1)).ok(),
331 Some(Integer::from_str("-123")?)
332 );
333 assert!(
334 Integer::try_from(Double::from(1_672_507_300_000.))
335 .unwrap()
336 .checked_sub(Integer::from_str("1672507300000").unwrap())
337 .unwrap()
338 .checked_abs()
339 .unwrap()
340 < Integer::from(10)
341 );
342 Integer::try_from(Double::from(f64::NAN)).unwrap_err();
343 Integer::try_from(Double::from(f64::INFINITY)).unwrap_err();
344 Integer::try_from(Double::from(f64::NEG_INFINITY)).unwrap_err();
345 Integer::try_from(Double::from(f64::MIN)).unwrap_err();
346 Integer::try_from(Double::from(f64::MAX)).unwrap_err();
347 Ok(())
348 }
349
350 #[test]
351 fn from_decimal() -> Result<(), ParseIntError> {
352 assert_eq!(
353 Integer::try_from(Decimal::from(0)).ok(),
354 Some(Integer::from_str("0")?)
355 );
356 assert_eq!(
357 Integer::try_from(Decimal::from_str("-123.1").unwrap()).ok(),
358 Some(Integer::from_str("-123")?)
359 );
360 Integer::try_from(Decimal::MIN).unwrap_err();
361 Integer::try_from(Decimal::MAX).unwrap_err();
362 Ok(())
363 }
364
365 #[test]
366 fn add() {
367 assert_eq!(
368 Integer::MIN.checked_add(1),
369 Some(Integer::from(i64::MIN + 1))
370 );
371 assert_eq!(Integer::MAX.checked_add(1), None);
372 }
373
374 #[test]
375 fn sub() {
376 assert_eq!(Integer::MIN.checked_sub(1), None);
377 assert_eq!(
378 Integer::MAX.checked_sub(1),
379 Some(Integer::from(i64::MAX - 1))
380 );
381 }
382
383 #[test]
384 fn mul() {
385 assert_eq!(Integer::MIN.checked_mul(2), None);
386 assert_eq!(Integer::MAX.checked_mul(2), None);
387 }
388
389 #[test]
390 fn div() {
391 assert_eq!(Integer::from(1).checked_div(0), None);
392 }
393
394 #[test]
395 fn rem() {
396 assert_eq!(Integer::from(10).checked_rem(3), Some(Integer::from(1)));
397 assert_eq!(Integer::from(6).checked_rem(-2), Some(Integer::from(0)));
398 assert_eq!(Integer::from(1).checked_rem(0), None);
399 }
400}