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

zbraniecki / icu4x / 6815798908

09 Nov 2023 05:17PM UTC coverage: 72.607% (-2.4%) from 75.01%
6815798908

push

github

web-flow
Implement `Any/BufferProvider` for some smart pointers (#4255)

Allows storing them as a `Box<dyn Any/BufferProvider>` without using a
wrapper type that implements the trait.

44281 of 60987 relevant lines covered (72.61%)

201375.86 hits per line

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

60.0
/components/decimal/src/lib.rs
1
// This file is part of ICU4X. For terms of use, please see the file
1✔
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
#![warn(missing_docs)]
6

7
//! Formatting basic decimal numbers.
8
//!
9
//! This module is published as its own crate ([`icu_decimal`](https://docs.rs/icu_decimal/latest/icu_decimal/))
10
//! and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project.
11
//!
12
//! Support for currencies, measurement units, and compact notation is planned. To track progress,
13
//! follow [icu4x#275](https://github.com/unicode-org/icu4x/issues/275).
14
//!
15
//! # Examples
16
//!
17
//! ## Format a number with Bangla digits
18
//!
19
//! ```
20
//! use fixed_decimal::FixedDecimal;
21
//! use icu::decimal::FixedDecimalFormatter;
22
//! use icu::locid::locale;
23
//! use writeable::assert_writeable_eq;
24
//!
25
//! let fdf = FixedDecimalFormatter::try_new(
26
//!     &locale!("bn").into(),
27
//!     Default::default(),
28
//! )
29
//! .expect("locale should be present");
30
//!
31
//! let fixed_decimal = FixedDecimal::from(1000007);
32
//!
33
//! assert_writeable_eq!(fdf.format(&fixed_decimal), "১০,০০,০০৭");
34
//! ```
35
//!
36
//! ## Format a number with digits after the decimal separator
37
//!
38
//! ```
39
//! use fixed_decimal::FixedDecimal;
40
//! use icu::decimal::FixedDecimalFormatter;
41
//! use icu::locid::Locale;
42
//! use writeable::assert_writeable_eq;
43
//!
44
//! let fdf =
45
//!     FixedDecimalFormatter::try_new(&Locale::UND.into(), Default::default())
46
//!         .expect("locale should be present");
47
//!
48
//! let fixed_decimal = FixedDecimal::from(200050).multiplied_pow10(-2);
49
//!
50
//! assert_writeable_eq!(fdf.format(&fixed_decimal), "2,000.50");
51
//! ```
52
//!
53
//! ### Format a number using an alternative numbering system
54
//!
55
//! Numbering systems specified in the `-u-nu` subtag will be followed as long as the locale has
56
//! symbols for that numbering system.
57
//!
58
//! ```
59
//! use fixed_decimal::FixedDecimal;
60
//! use icu::decimal::FixedDecimalFormatter;
61
//! use icu::locid::locale;
62
//! use writeable::assert_writeable_eq;
63
//!
64
//! let fdf = FixedDecimalFormatter::try_new(
65
//!     &locale!("th-u-nu-thai").into(),
66
//!     Default::default(),
67
//! )
68
//! .expect("locale should be present");
69
//!
70
//! let fixed_decimal = FixedDecimal::from(1000007);
71
//!
72
//! assert_writeable_eq!(fdf.format(&fixed_decimal), "๑,๐๐๐,๐๐๗");
73
//! ```
74
//!
75
//! [`FixedDecimalFormatter`]: FixedDecimalFormatter
76

77
// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations
78
#![cfg_attr(not(any(test, feature = "std")), no_std)]
79
#![cfg_attr(
80
    not(test),
81
    deny(
82
        clippy::indexing_slicing,
83
        clippy::unwrap_used,
84
        clippy::expect_used,
85
        clippy::panic,
86
        clippy::exhaustive_structs,
87
        clippy::exhaustive_enums,
88
        missing_debug_implementations,
89
    )
90
)]
91
#![warn(missing_docs)]
92

93
extern crate alloc;
94

95
mod error;
96
mod format;
97
mod grouper;
98
pub mod options;
99
pub mod provider;
100

101
pub use error::DecimalError;
102
pub use format::FormattedFixedDecimal;
103

104
#[doc(no_inline)]
105
pub use DecimalError as Error;
106

107
use alloc::string::String;
108
use fixed_decimal::FixedDecimal;
109
use icu_provider::prelude::*;
110
use writeable::Writeable;
111

112
/// A formatter for [`FixedDecimal`], rendering decimal digits in an i18n-friendly way.
113
///
114
/// [`FixedDecimalFormatter`] supports:
115
///
116
/// 1. Rendering in the local numbering system
117
/// 2. Locale-sensitive grouping separator positions
118
/// 3. Locale-sensitive plus and minus signs
119
///
120
/// Read more about the options in the [`options`] module.
121
///
122
/// See the crate-level documentation for examples.
123
#[derive(Debug)]
×
124
pub struct FixedDecimalFormatter {
125
    options: options::FixedDecimalFormatterOptions,
×
126
    symbols: DataPayload<provider::DecimalSymbolsV1Marker>,
×
127
}
128

129
impl FixedDecimalFormatter {
130
    icu_provider::gen_any_buffer_data_constructors!(
131
        locale: include,
132
        options: options::FixedDecimalFormatterOptions,
133
        error: DecimalError,
134
        /// Creates a new [`FixedDecimalFormatter`] from compiled locale data and an options bag.
135
        ///
136
        /// ✨ *Enabled with the `compiled_data` Cargo feature.*
137
        ///
138
        /// [📚 Help choosing a constructor](icu_provider::constructors)
139
    );
140

141
    #[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
142
    pub fn try_new_unstable<D: DataProvider<provider::DecimalSymbolsV1Marker> + ?Sized>(
614✔
143
        provider: &D,
144
        locale: &DataLocale,
145
        options: options::FixedDecimalFormatterOptions,
146
    ) -> Result<Self, DecimalError> {
147
        let symbols = provider
614✔
148
            .load(DataRequest {
614✔
149
                locale,
150
                metadata: Default::default(),
614✔
151
            })?
×
152
            .take_payload()?;
×
153
        Ok(Self { options, symbols })
614✔
154
    }
614✔
155

156
    /// Formats a [`FixedDecimal`], returning a [`FormattedFixedDecimal`].
157
    pub fn format<'l>(&'l self, value: &'l FixedDecimal) -> FormattedFixedDecimal<'l> {
6,953✔
158
        FormattedFixedDecimal {
6,953✔
159
            value,
160
            options: &self.options,
6,953✔
161
            symbols: self.symbols.get(),
6,953✔
162
        }
163
    }
6,953✔
164

165
    /// Formats a [`FixedDecimal`], returning a [`String`].
166
    pub fn format_to_string(&self, value: &FixedDecimal) -> String {
×
167
        self.format(value).write_to_string().into_owned()
×
168
    }
×
169
}
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

© 2025 Coveralls, Inc