Debian Codegraph @

debian/rust-vendor/icu_decimal/src/lib.rs

raw
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 icu::decimal::DecimalFormatter;
19 //! use icu::decimal::input::Decimal;
20 //! use icu::locale::locale;
21 //! use writeable::assert_writeable_eq;
22 //!
23 //! let formatter =
24 //! DecimalFormatter::try_new(locale!("bn").into(), Default::default())
25 //! .expect("locale should be present");
26 //!
27 //! let decimal = Decimal::from(1000007);
28 //!
29 //! assert_writeable_eq!(formatter.format(&decimal), "১০,০০,০০৭");
30 //! ```
31 //!
32 //! ## Format a number with digits after the decimal separator
33 //!
34 //! ```
35 //! use icu::decimal::DecimalFormatter;
36 //! use icu::decimal::input::Decimal;
37 //! use icu::locale::Locale;
38 //! use writeable::assert_writeable_eq;
39 //!
40 //! let formatter =
41 //! DecimalFormatter::try_new(Default::default(), Default::default())
42 //! .expect("locale should be present");
43 //!
44 //! let decimal = {
45 //! let mut decimal = Decimal::from(200050);
46 //! decimal.multiply_pow10(-2);
47 //! decimal
48 //! };
49 //!
50 //! assert_writeable_eq!(formatter.format(&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.
56 //!
57 //! ```
58 //! use icu::decimal::DecimalFormatter;
59 //! use icu::decimal::input::Decimal;
60 //! use icu::locale::locale;
61 //! use writeable::assert_writeable_eq;
62 //!
63 //! let formatter = DecimalFormatter::try_new(
64 //! locale!("th-u-nu-thai").into(),
65 //! Default::default(),
66 //! )
67 //! .expect("locale should be present");
68 //!
69 //! let decimal = Decimal::from(1000007);
70 //!
71 //! assert_writeable_eq!(formatter.format(&decimal), "๑,๐๐๐,๐๐๗");
72 //! ```
73 //!
74 //! [`DecimalFormatter`]: DecimalFormatter
75
76 // https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
77 #![cfg_attr(not(any(test, doc)), no_std)]
78 #![cfg_attr(
79 not(test),
80 deny(
81 clippy::indexing_slicing,
82 clippy::unwrap_used,
83 clippy::expect_used,
84 clippy::panic,
85 )
86 )]
87 #![warn(missing_docs)]
88
89 #[cfg(feature = "alloc")]
90 extern crate alloc;
91
92 #[cfg(feature = "alloc")]
93 #[doc(hidden)] // TODO(#3647): should be private
94 pub use alloc::borrow::Cow;
95
96 #[cfg(not(feature = "alloc"))]
97 #[derive(Debug, PartialEq, Eq, Clone)]
98 #[doc(hidden)] // TODO(#3647): should be private
99 pub enum Cow<'a, T> {
100 Borrowed(&'a T),
101 Owned(T),
102 }
103
104 #[cfg(not(feature = "alloc"))]
105 impl<'a, T> core::ops::Deref for Cow<'a, T> {
106 type Target = T;
107
108 fn deref(&self) -> &Self::Target {
109 match self {
110 Self::Borrowed(r) => r,
111 Self::Owned(r) => r,
112 }
113 }
114 }
115
116 #[cfg(feature = "unstable")]
117 mod abstract_formatter;
118 #[cfg(feature = "unstable")]
119 mod compact_formatter;
120 mod decimal_formatter;
121 #[cfg(feature = "unstable")]
122 pub mod error;
123 mod grouper;
124 pub mod options;
125 pub mod parts;
126 pub mod preferences;
127 pub mod provider;
128 mod size_test_macro;
129
130 pub use decimal_formatter::{
131 DecimalFormatter, FormattedDecimal, FormattedSign, FormattedUnsignedDecimal,
132 };
133
134 #[cfg(feature = "unstable")]
135 pub use compact_formatter::{CompactDecimalFormatter, FormattedUnsignedCompactDecimal};
136
137 #[cfg(feature = "unstable")]
138 pub use abstract_formatter::AbstractFormatter;
139
140 pub use preferences::DecimalFormatterPreferences;
141
142 /// Types that can be fed to [`DecimalFormatter`] and their utilities
143 ///
144 /// This module contains re-exports from the [`fixed_decimal`] crate.
145 pub mod input {
146 pub use fixed_decimal::Decimal;
147 #[cfg(feature = "ryu")]
148 pub use fixed_decimal::FloatPrecision;
149 pub use fixed_decimal::SignDisplay;
150 }