fixed_decimal/variations.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//! This module defines variations of numeric types, including signed values,
6//! values with infinity, and values with NaN.
7
8// TODO: move to sign.rs
9/// A specification of the sign used when formatting a number.
10#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
11#[allow(clippy::exhaustive_enums)]
12// There are only 3 sign values, and they correspond to the low-level data model of FixedDecimal and UTS 35.
13pub enum Sign {
14 /// No sign (implicitly positive, e.g., 1729).
15 #[default]
16 None,
17 /// A negative sign, e.g., -1729.
18 Negative,
19 /// An explicit positive sign, e.g., +1729.
20 Positive,
21}
22
23/// Configuration for when to render the minus sign or plus sign.
24#[non_exhaustive]
25#[derive(Debug, Eq, PartialEq, Clone, Copy)]
26pub enum SignDisplay {
27 /// Render the sign according to locale preferences. In most cases, this means a minus sign
28 /// will be shown on negative numbers, and no sign will be shown on positive numbers.
29 Auto,
30
31 /// Do not display the sign. Positive and negative numbers are indistinguishable.
32 Never,
33
34 /// Show a minus sign on negative numbers and a plus sign on positive numbers, including zero.
35 Always,
36
37 /// Show a minus sign on negative numbers and a plus sign on positive numbers, except do not
38 /// show any sign on positive or negative zero.
39 ExceptZero,
40
41 /// Show a minus sign on strictly negative numbers. Do not show a sign on positive numbers or
42 /// on positive or negative zero.
43 ///
44 /// This differs from [`Auto`](SignDisplay::Auto) in that it does not render a sign on negative zero.
45 Negative,
46}
47
48/// The `Signed` struct represents a numeric value with an associated sign.
49#[non_exhaustive]
50#[derive(Debug, Clone, PartialEq, Default)]
51pub struct Signed<T> {
52 pub sign: Sign,
53 pub absolute: T,
54}
55
56impl<T> Signed<T> {
57 /// Returns the sign of this signed number.
58 pub fn sign(&self) -> Sign {
59 self.sign
60 }
61
62 /// Changes the sign of this signed number to the one given.
63 pub fn set_sign(&mut self, sign: Sign) {
64 self.sign = sign;
65 }
66
67 /// Returns this number with the sign changed to the one given.
68 pub fn with_sign(mut self, sign: Sign) -> Self {
69 self.set_sign(sign);
70 self
71 }
72}
73
74// TODO(#5065): implement `WithCompactExponent` and `WithScientificExponent`.
75// /// The `WithInfinity` enum represents a numeric value that may be either infinite or finite.
76// #[derive(Debug)]
77// pub enum WithInfinity<T> {
78// Infinity,
79// Finite(T),
80// }
81
82// /// The `WithNaN` enum represents a numeric value that may be NaN.
83// #[derive(Debug)]
84// pub enum WithNaN<T> {
85// NaN,
86// N(T),
87// }
88
89// TODO(#5065): implement `WithCompactExponent` and `WithScientificExponent`.
90// /// The `WithCompactExponent` struct represents a numeric value with a compact exponent.
91// #[derive(Debug)]
92// pub struct WithCompactExponent<T> {
93// pub exponent: u8,
94// pub significand: T,
95// }
96
97// /// The `WithScientificExponent` struct represents a numeric value with a scientific exponent.
98// #[derive(Debug)]
99// pub struct WithScientificExponent<T> {
100// pub exponent: i16,
101// pub significand: T,
102// }