• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zbraniecki / icu4x / 13958601093

19 Mar 2025 04:17PM UTC coverage: 74.164% (-1.5%) from 75.71%
13958601093

push

github

web-flow
Clean up properties docs (#6315)

58056 of 78281 relevant lines covered (74.16%)

819371.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.23
/utils/fixed_decimal/src/integer.rs
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 {
1✔
44
        value.0
1✔
45
    }
1✔
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 {
3✔
52
                FixedInteger(Decimal::from(value))
3✔
53
            }
3✔
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 {
6✔
73
        self.0.write_to(sink)
6✔
74
    }
6✔
75
}
76

77
impl TryFrom<Decimal> for FixedInteger {
78
    type Error = LimitError;
79
    fn try_from(signed_fd: Decimal) -> Result<Self, Self::Error> {
8✔
80
        if signed_fd.absolute.magnitude_range().start() != &0 {
8✔
81
            Err(LimitError)
2✔
82
        } else {
83
            Ok(FixedInteger(signed_fd))
6✔
84
        }
85
    }
8✔
86
}
87

88
impl FixedInteger {
89
    #[inline]
90
    pub fn try_from_str(s: &str) -> Result<Self, ParseError> {
×
91
        Self::try_from_utf8(s.as_bytes())
×
92
    }
×
93

94
    pub fn try_from_utf8(code_units: &[u8]) -> Result<Self, ParseError> {
7✔
95
        FixedInteger::try_from(Decimal::try_from_utf8(code_units)?)
7✔
96
            .map_err(|LimitError| ParseError::Limit)
1✔
97
    }
7✔
98
}
99

100
impl FromStr for FixedInteger {
101
    type Err = ParseError;
102
    #[inline]
103
    fn from_str(s: &str) -> Result<Self, Self::Err> {
×
104
        Self::try_from_str(s)
×
105
    }
×
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc