Coverage Report

Created: 2025-06-16 06:50

/rust/registry/src/index.crates.io-6f17d22bba15001f/icu_decimal-1.5.0/src/lib.rs
Line
Count
Source (jump to first uncovered line)
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
//! Formatting basic decimal numbers.
6
//!
7
//! This module is published as its own crate ([`icu_decimal`](https://docs.rs/icu_decimal/latest/icu_decimal/))
8
//! and as part of the [`icu`](https://docs.rs/icu/latest/icu/) crate. See the latter for more details on the ICU4X project.
9
//!
10
//! Support for currencies, measurement units, and compact notation is planned. To track progress,
11
//! follow [icu4x#275](https://github.com/unicode-org/icu4x/issues/275).
12
//!
13
//! # Examples
14
//!
15
//! ## Format a number with Bangla digits
16
//!
17
//! ```
18
//! use fixed_decimal::FixedDecimal;
19
//! use icu::decimal::FixedDecimalFormatter;
20
//! use icu::locid::locale;
21
//! use writeable::assert_writeable_eq;
22
//!
23
//! let fdf = FixedDecimalFormatter::try_new(
24
//!     &locale!("bn").into(),
25
//!     Default::default(),
26
//! )
27
//! .expect("locale should be present");
28
//!
29
//! let fixed_decimal = FixedDecimal::from(1000007);
30
//!
31
//! assert_writeable_eq!(fdf.format(&fixed_decimal), "১০,০০,০০৭");
32
//! ```
33
//!
34
//! ## Format a number with digits after the decimal separator
35
//!
36
//! ```
37
//! use fixed_decimal::FixedDecimal;
38
//! use icu::decimal::FixedDecimalFormatter;
39
//! use icu::locid::Locale;
40
//! use writeable::assert_writeable_eq;
41
//!
42
//! let fdf =
43
//!     FixedDecimalFormatter::try_new(&Locale::UND.into(), Default::default())
44
//!         .expect("locale should be present");
45
//!
46
//! let fixed_decimal = FixedDecimal::from(200050).multiplied_pow10(-2);
47
//!
48
//! assert_writeable_eq!(fdf.format(&fixed_decimal), "2,000.50");
49
//! ```
50
//!
51
//! ### Format a number using an alternative numbering system
52
//!
53
//! Numbering systems specified in the `-u-nu` subtag will be followed as long as the locale has
54
//! symbols for that numbering system.
55
//!
56
//! ```
57
//! use fixed_decimal::FixedDecimal;
58
//! use icu::decimal::FixedDecimalFormatter;
59
//! use icu::locid::locale;
60
//! use writeable::assert_writeable_eq;
61
//!
62
//! let fdf = FixedDecimalFormatter::try_new(
63
//!     &locale!("th-u-nu-thai").into(),
64
//!     Default::default(),
65
//! )
66
//! .expect("locale should be present");
67
//!
68
//! let fixed_decimal = FixedDecimal::from(1000007);
69
//!
70
//! assert_writeable_eq!(fdf.format(&fixed_decimal), "๑,๐๐๐,๐๐๗");
71
//! ```
72
//!
73
//! [`FixedDecimalFormatter`]: FixedDecimalFormatter
74
75
// https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
76
#![cfg_attr(not(any(test, feature = "std")), no_std)]
77
#![cfg_attr(
78
    not(test),
79
    deny(
80
        clippy::indexing_slicing,
81
        clippy::unwrap_used,
82
        clippy::expect_used,
83
        clippy::panic,
84
        clippy::exhaustive_structs,
85
        clippy::exhaustive_enums,
86
        missing_debug_implementations,
87
    )
88
)]
89
#![warn(missing_docs)]
90
91
extern crate alloc;
92
93
mod error;
94
mod format;
95
mod grouper;
96
pub mod options;
97
pub mod provider;
98
99
pub use error::DecimalError;
100
pub use format::FormattedFixedDecimal;
101
102
#[doc(no_inline)]
103
pub use DecimalError as Error;
104
105
use alloc::string::String;
106
use fixed_decimal::FixedDecimal;
107
use icu_provider::prelude::*;
108
use writeable::Writeable;
109
110
/// A formatter for [`FixedDecimal`], rendering decimal digits in an i18n-friendly way.
111
///
112
/// [`FixedDecimalFormatter`] supports:
113
///
114
/// 1. Rendering in the local numbering system
115
/// 2. Locale-sensitive grouping separator positions
116
/// 3. Locale-sensitive plus and minus signs
117
///
118
/// Read more about the options in the [`options`] module.
119
///
120
/// See the crate-level documentation for examples.
121
#[derive(Debug)]
122
pub struct FixedDecimalFormatter {
123
    options: options::FixedDecimalFormatterOptions,
124
    symbols: DataPayload<provider::DecimalSymbolsV1Marker>,
125
}
126
127
impl FixedDecimalFormatter {
128
    icu_provider::gen_any_buffer_data_constructors!(
129
        locale: include,
130
        options: options::FixedDecimalFormatterOptions,
131
        error: DecimalError,
132
        /// Creates a new [`FixedDecimalFormatter`] from compiled data and an options bag.
133
        ///
134
        /// ✨ *Enabled with the `compiled_data` Cargo feature.*
135
        ///
136
        /// [📚 Help choosing a constructor](icu_provider::constructors)
137
    );
138
139
    #[doc = icu_provider::gen_any_buffer_unstable_docs!(UNSTABLE, Self::try_new)]
140
0
    pub fn try_new_unstable<D: DataProvider<provider::DecimalSymbolsV1Marker> + ?Sized>(
141
0
        provider: &D,
142
0
        locale: &DataLocale,
143
0
        options: options::FixedDecimalFormatterOptions,
144
0
    ) -> Result<Self, DecimalError> {
145
0
        let symbols = provider
146
0
            .load(DataRequest {
147
0
                locale,
148
0
                metadata: Default::default(),
149
0
            })?
150
0
            .take_payload()?;
151
0
        Ok(Self { options, symbols })
152
0
    }
Unexecuted instantiation: <icu_decimal::FixedDecimalFormatter>::try_new_unstable::<icu_provider::any::DowncastingAnyProvider<icu_provider_adapters::any_payload::AnyPayloadProvider>>
Unexecuted instantiation: <icu_decimal::FixedDecimalFormatter>::try_new_unstable::<icu_provider::any::DowncastingAnyProvider<icu_provider_adapters::empty::EmptyDataProvider>>
Unexecuted instantiation: <icu_decimal::FixedDecimalFormatter>::try_new_unstable::<icu_decimal::provider::Baked>
153
154
    /// Formats a [`FixedDecimal`], returning a [`FormattedFixedDecimal`].
155
0
    pub fn format<'l>(&'l self, value: &'l FixedDecimal) -> FormattedFixedDecimal<'l> {
156
0
        FormattedFixedDecimal {
157
0
            value,
158
0
            options: &self.options,
159
0
            symbols: self.symbols.get(),
160
0
        }
161
0
    }
162
163
    /// Formats a [`FixedDecimal`], returning a [`String`].
164
0
    pub fn format_to_string(&self, value: &FixedDecimal) -> String {
165
0
        self.format(value).write_to_string().into_owned()
166
0
    }
167
}