Skip to main content

ferroui_base/utilities/
number_styles.rs

1//! A counterpart of .NET's `System.Globalization.NumberStyles`.
2
3use bitflags::bitflags;
4
5bitflags! {
6    /// The styles permitted in the text of a number that is parsed.
7    #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
8    pub struct NumberStyles: i32 {
9        /// No style elements: decimal digits only.
10        const NONE = 0x0000;
11
12        /// White space may come first.
13        const ALLOW_LEADING_WHITE = 0x0001;
14
15        /// White space may come last.
16        const ALLOW_TRAILING_WHITE = 0x0002;
17
18        /// A sign may come first.
19        const ALLOW_LEADING_SIGN = 0x0004;
20
21        /// A sign may come last.
22        const ALLOW_TRAILING_SIGN = 0x0008;
23
24        /// One pair of parentheses may enclose the number, which is then
25        /// negative.
26        const ALLOW_PARENTHESES = 0x0010;
27
28        /// A decimal point is permitted.
29        const ALLOW_DECIMAL_POINT = 0x0020;
30
31        /// Group separators are permitted.
32        const ALLOW_THOUSANDS = 0x0040;
33
34        /// An exponent (`E` or `e`, an optional sign, digits) is permitted.
35        const ALLOW_EXPONENT = 0x0080;
36
37        /// A currency symbol is permitted.
38        const ALLOW_CURRENCY_SYMBOL = 0x0100;
39
40        /// The text is a hexadecimal number.
41        const ALLOW_HEX_SPECIFIER = 0x0200;
42
43        /// The text is a binary number.
44        const ALLOW_BINARY_SPECIFIER = 0x0400;
45
46        /// Leading and trailing white space and a leading sign.
47        const INTEGER = Self::ALLOW_LEADING_WHITE.bits() | Self::ALLOW_TRAILING_WHITE.bits()
48            | Self::ALLOW_LEADING_SIGN.bits();
49
50        /// Leading and trailing white space and hexadecimal digits.
51        const HEX_NUMBER = Self::ALLOW_LEADING_WHITE.bits() | Self::ALLOW_TRAILING_WHITE.bits()
52            | Self::ALLOW_HEX_SPECIFIER.bits();
53
54        /// Leading and trailing white space and binary digits.
55        const BINARY_NUMBER = Self::ALLOW_LEADING_WHITE.bits() | Self::ALLOW_TRAILING_WHITE.bits()
56            | Self::ALLOW_BINARY_SPECIFIER.bits();
57
58        /// White space, a leading or trailing sign, a decimal point and
59        /// group separators.
60        const NUMBER = Self::ALLOW_LEADING_WHITE.bits() | Self::ALLOW_TRAILING_WHITE.bits()
61            | Self::ALLOW_LEADING_SIGN.bits() | Self::ALLOW_TRAILING_SIGN.bits()
62            | Self::ALLOW_DECIMAL_POINT.bits() | Self::ALLOW_THOUSANDS.bits();
63
64        /// White space, a leading sign, a decimal point and an exponent.
65        const FLOAT = Self::ALLOW_LEADING_WHITE.bits() | Self::ALLOW_TRAILING_WHITE.bits()
66            | Self::ALLOW_LEADING_SIGN.bits() | Self::ALLOW_DECIMAL_POINT.bits()
67            | Self::ALLOW_EXPONENT.bits();
68
69        /// Every style except the exponent and the hexadecimal and binary
70        /// specifiers.
71        const CURRENCY = Self::ALLOW_LEADING_WHITE.bits() | Self::ALLOW_TRAILING_WHITE.bits()
72            | Self::ALLOW_LEADING_SIGN.bits() | Self::ALLOW_TRAILING_SIGN.bits()
73            | Self::ALLOW_PARENTHESES.bits() | Self::ALLOW_DECIMAL_POINT.bits()
74            | Self::ALLOW_THOUSANDS.bits() | Self::ALLOW_CURRENCY_SYMBOL.bits();
75
76        /// Every style except the hexadecimal and binary specifiers.
77        const ANY = Self::ALLOW_LEADING_WHITE.bits() | Self::ALLOW_TRAILING_WHITE.bits()
78            | Self::ALLOW_LEADING_SIGN.bits() | Self::ALLOW_TRAILING_SIGN.bits()
79            | Self::ALLOW_PARENTHESES.bits() | Self::ALLOW_DECIMAL_POINT.bits()
80            | Self::ALLOW_THOUSANDS.bits() | Self::ALLOW_CURRENCY_SYMBOL.bits()
81            | Self::ALLOW_EXPONENT.bits();
82    }
83}
84
85crate::ferro_markup_enum!(flags NumberStyles {
86    None = NumberStyles::NONE,
87    AllowLeadingWhite = NumberStyles::ALLOW_LEADING_WHITE,
88    AllowTrailingWhite = NumberStyles::ALLOW_TRAILING_WHITE,
89    AllowLeadingSign = NumberStyles::ALLOW_LEADING_SIGN,
90    AllowTrailingSign = NumberStyles::ALLOW_TRAILING_SIGN,
91    AllowParentheses = NumberStyles::ALLOW_PARENTHESES,
92    AllowDecimalPoint = NumberStyles::ALLOW_DECIMAL_POINT,
93    AllowThousands = NumberStyles::ALLOW_THOUSANDS,
94    AllowExponent = NumberStyles::ALLOW_EXPONENT,
95    AllowCurrencySymbol = NumberStyles::ALLOW_CURRENCY_SYMBOL,
96    AllowHexSpecifier = NumberStyles::ALLOW_HEX_SPECIFIER,
97    AllowBinarySpecifier = NumberStyles::ALLOW_BINARY_SPECIFIER,
98    Integer = NumberStyles::INTEGER,
99    HexNumber = NumberStyles::HEX_NUMBER,
100    BinaryNumber = NumberStyles::BINARY_NUMBER,
101    Number = NumberStyles::NUMBER,
102    Float = NumberStyles::FLOAT,
103    Currency = NumberStyles::CURRENCY,
104    Any = NumberStyles::ANY,
105}, { namespace: "System.Globalization" });
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    #[test]
112    fn values_match_the_managed_enumeration() {
113        assert_eq!(NumberStyles::INTEGER.bits(), 0x0007);
114        assert_eq!(NumberStyles::HEX_NUMBER.bits(), 0x0203);
115        assert_eq!(NumberStyles::BINARY_NUMBER.bits(), 0x0403);
116        assert_eq!(NumberStyles::NUMBER.bits(), 0x006F);
117        assert_eq!(NumberStyles::FLOAT.bits(), 0x00A7);
118        assert_eq!(NumberStyles::CURRENCY.bits(), 0x017F);
119        assert_eq!(NumberStyles::ANY.bits(), 0x01FF);
120        assert_eq!(NumberStyles::default(), NumberStyles::NONE);
121    }
122}