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
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

use core::convert::TryFrom;
use core::fmt;

use core::str::FromStr;

use crate::LimitError;
use crate::ParseError;
use crate::SignedFixedDecimal;

/// A [`FixedInteger`] is a [`SignedFixedDecimal`] with no fractional part.
///
///
/// # Examples
///
/// ```
/// # use std::str::FromStr;
/// use fixed_decimal::SignedFixedDecimal;
/// use fixed_decimal::FixedInteger;
/// use fixed_decimal::LimitError;
///
/// assert_eq!(
///     SignedFixedDecimal::from(FixedInteger::from(5)),
///     SignedFixedDecimal::from(5)
/// );
/// assert_eq!(
///     FixedInteger::try_from(SignedFixedDecimal::from(5)),
///     Ok(FixedInteger::from(5))
/// );
/// assert_eq!(
///     FixedInteger::try_from(SignedFixedDecimal::from_str("05").unwrap()),
///     Ok(FixedInteger::from_str("05").unwrap())
/// );
/// assert_eq!(
///     FixedInteger::try_from(SignedFixedDecimal::from_str("5.0").unwrap()),
///     Err(LimitError)
/// );
/// ```
#[derive(Debug, Clone, PartialEq, Default)]
pub struct FixedInteger(SignedFixedDecimal);

impl From<FixedInteger> for SignedFixedDecimal {
    fn from(value: FixedInteger) -> Self {
        value.0
    }
}

macro_rules! impl_fixed_integer_from_integer_type {
    ($type:ident) => {
        impl From<$type> for FixedInteger {
            fn from(value: $type) -> Self {
                FixedInteger(SignedFixedDecimal::from(value))
            }
        }
    };
}

impl_fixed_integer_from_integer_type!(isize);
impl_fixed_integer_from_integer_type!(i128);
impl_fixed_integer_from_integer_type!(i64);
impl_fixed_integer_from_integer_type!(i32);
impl_fixed_integer_from_integer_type!(i16);
impl_fixed_integer_from_integer_type!(i8);
impl_fixed_integer_from_integer_type!(usize);
impl_fixed_integer_from_integer_type!(u128);
impl_fixed_integer_from_integer_type!(u64);
impl_fixed_integer_from_integer_type!(u32);
impl_fixed_integer_from_integer_type!(u16);
impl_fixed_integer_from_integer_type!(u8);

impl writeable::Writeable for FixedInteger {
    fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
        self.0.write_to(sink)
    }
}

impl TryFrom<SignedFixedDecimal> for FixedInteger {
    type Error = LimitError;
    fn try_from(signed_fd: SignedFixedDecimal) -> Result<Self, Self::Error> {
        if signed_fd.absolute.magnitude_range().start() != &0 {
            Err(LimitError)
        } else {
            Ok(FixedInteger(signed_fd))
        }
    }
}

impl FixedInteger {
    #[inline]
    pub fn try_from_str(s: &str) -> Result<Self, ParseError> {
        Self::try_from_utf8(s.as_bytes())
    }

    pub fn try_from_utf8(code_units: &[u8]) -> Result<Self, ParseError> {
        FixedInteger::try_from(SignedFixedDecimal::try_from_utf8(code_units)?)
            .map_err(|LimitError| ParseError::Limit)
    }
}

impl FromStr for FixedInteger {
    type Err = ParseError;
    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::try_from_str(s)
    }
}