Debian Codegraph @

debian/rust-vendor/fixed_decimal/src/variations.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 //! 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.
13 pub 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 ///
25 /// **The primary definition of this type is in the [`fixed_decimal`](https://docs.rs/fixed_decimal) crate. Other ICU4X crates re-export it for convenience.**
26 #[non_exhaustive]
27 #[derive(Debug, Eq, PartialEq, Clone, Copy, Default)]
28 pub enum SignDisplay {
29 /// Render the sign according to locale preferences. In most cases, this means a minus sign
30 /// will be shown on negative numbers, and no sign will be shown on positive numbers.
31 #[default]
32 Auto,
33
34 /// Do not display the sign. Positive and negative numbers are indistinguishable.
35 Never,
36
37 /// Show a minus sign on negative numbers and a plus sign on positive numbers, including zero.
38 Always,
39
40 /// Show a minus sign on negative numbers and a plus sign on positive numbers, except do not
41 /// show any sign on positive or negative zero.
42 ExceptZero,
43
44 /// Show a minus sign on strictly negative numbers. Do not show a sign on positive numbers or
45 /// on positive or negative zero.
46 ///
47 /// This differs from [`Auto`](SignDisplay::Auto) in that it does not render a sign on negative zero.
48 Negative,
49 }
50
51 /// The `Signed` struct represents a numeric value with an associated sign.
52 #[non_exhaustive]
53 #[derive(Debug, Clone, PartialEq, Default)]
54 pub struct Signed<T> {
55 /// The sign
56 pub sign: Sign,
57 /// The absolute value
58 pub absolute: T,
59 }
60
61 impl<T> Signed<T> {
62 /// Returns the sign of this signed number.
63 pub fn sign(&self) -> Sign {
64 self.sign
65 }
66
67 /// Changes the sign of this signed number to the one given.
68 pub fn set_sign(&mut self, sign: Sign) {
69 self.sign = sign;
70 }
71
72 /// Returns this number with the sign changed to the one given.
73 pub fn with_sign(mut self, sign: Sign) -> Self {
74 self.set_sign(sign);
75 self
76 }
77 }
78
79 // TODO(#5065): implement `WithCompactExponent` and `WithScientificExponent`.
80 // /// The `WithInfinity` enum represents a numeric value that may be either infinite or finite.
81 // #[derive(Debug)]
82 // pub enum WithInfinity<T> {
83 // Infinity,
84 // Finite(T),
85 // }
86
87 // /// The `WithNaN` enum represents a numeric value that may be NaN.
88 // #[derive(Debug)]
89 // pub enum WithNaN<T> {
90 // NaN,
91 // N(T),
92 // }
93
94 // TODO(#5065): implement `WithCompactExponent` and `WithScientificExponent`.
95 // /// The `WithCompactExponent` struct represents a numeric value with a compact exponent.
96 // #[derive(Debug)]
97 // pub struct WithCompactExponent<T> {
98 // pub exponent: u8,
99 // pub significand: T,
100 // }
101
102 // /// The `WithScientificExponent` struct represents a numeric value with a scientific exponent.
103 // #[derive(Debug)]
104 // pub struct WithScientificExponent<T> {
105 // pub exponent: i16,
106 // pub significand: T,
107 // }