| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use core::convert::TryFrom; |
| 6 |
use core::fmt; |
| 7 |
|
| 8 |
use core::str::FromStr; |
| 9 |
|
| 10 |
use crate::Decimal; |
| 11 |
use crate::LimitError; |
| 12 |
use crate::ParseError; |
| 13 |
|
| 14 |
/// A [`FixedInteger`] is a [`Decimal`] with no fractional part. |
| 15 |
/// |
| 16 |
/// |
| 17 |
/// # Examples |
| 18 |
/// |
| 19 |
/// ``` |
| 20 |
/// # use std::str::FromStr; |
| 21 |
/// use fixed_decimal::Decimal; |
| 22 |
/// use fixed_decimal::FixedInteger; |
| 23 |
/// use fixed_decimal::LimitError; |
| 24 |
/// |
| 25 |
/// assert_eq!(Decimal::from(FixedInteger::from(5)), Decimal::from(5)); |
| 26 |
/// assert_eq!( |
| 27 |
/// FixedInteger::try_from(Decimal::from(5)), |
| 28 |
/// Ok(FixedInteger::from(5)) |
| 29 |
/// ); |
| 30 |
/// assert_eq!( |
| 31 |
/// FixedInteger::try_from(Decimal::from_str("05").unwrap()), |
| 32 |
/// Ok(FixedInteger::from_str("05").unwrap()) |
| 33 |
/// ); |
| 34 |
/// assert_eq!( |
| 35 |
/// FixedInteger::try_from(Decimal::from_str("5.0").unwrap()), |
| 36 |
/// Err(LimitError) |
| 37 |
/// ); |
| 38 |
/// ``` |
| 39 |
#[derive(Debug, Clone, PartialEq, Default)] |
| 40 |
pub struct FixedInteger(Decimal); |
| 41 |
|
| 42 |
impl From<FixedInteger> for Decimal { |
| 43 |
fn from(value: FixedInteger) -> Self { |
| 44 |
value.0 |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
macro_rules! impl_fixed_integer_from_integer_type { |
| 49 |
($type:ident) => { |
| 50 |
impl From<$type> for FixedInteger { |
| 51 |
fn from(value: $type) -> Self { |
| 52 |
FixedInteger(Decimal::from(value)) |
| 53 |
} |
| 54 |
} |
| 55 |
}; |
| 56 |
} |
| 57 |
|
| 58 |
impl_fixed_integer_from_integer_type!(isize); |
| 59 |
impl_fixed_integer_from_integer_type!(i128); |
| 60 |
impl_fixed_integer_from_integer_type!(i64); |
| 61 |
impl_fixed_integer_from_integer_type!(i32); |
| 62 |
impl_fixed_integer_from_integer_type!(i16); |
| 63 |
impl_fixed_integer_from_integer_type!(i8); |
| 64 |
impl_fixed_integer_from_integer_type!(usize); |
| 65 |
impl_fixed_integer_from_integer_type!(u128); |
| 66 |
impl_fixed_integer_from_integer_type!(u64); |
| 67 |
impl_fixed_integer_from_integer_type!(u32); |
| 68 |
impl_fixed_integer_from_integer_type!(u16); |
| 69 |
impl_fixed_integer_from_integer_type!(u8); |
| 70 |
|
| 71 |
impl writeable::Writeable for FixedInteger { |
| 72 |
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result { |
| 73 |
self.0.write_to(sink) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
impl TryFrom<Decimal> for FixedInteger { |
| 78 |
type Error = LimitError; |
| 79 |
fn try_from(signed_fd: Decimal) -> Result<Self, Self::Error> { |
| 80 |
if signed_fd.absolute.magnitude_range().start() != &0 { |
| 81 |
Err(LimitError) |
| 82 |
} else { |
| 83 |
Ok(FixedInteger(signed_fd)) |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
impl FixedInteger { |
| 89 |
#[inline] |
| 90 |
/// Parses a [`FixedInteger`] |
| 91 |
pub fn try_from_str(s: &str) -> Result<Self, ParseError> { |
| 92 |
Self::try_from_utf8(s.as_bytes()) |
| 93 |
} |
| 94 |
|
| 95 |
/// See [`Self::try_from_utf8`] |
| 96 |
pub fn try_from_utf8(code_units: &[u8]) -> Result<Self, ParseError> { |
| 97 |
FixedInteger::try_from(Decimal::try_from_utf8(code_units)?) |
| 98 |
.map_err(|LimitError| ParseError::Limit) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
impl FromStr for FixedInteger { |
| 103 |
type Err = ParseError; |
| 104 |
#[inline] |
| 105 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 106 |
Self::try_from_str(s) |
| 107 |
} |
| 108 |
} |