lexical_util/
format_builder.rs

1//! Builder for the number format.
2
3use core::{mem, num};
4
5use static_assertions::const_assert;
6
7use crate::format_flags as flags;
8
9/// Type with the exact same size as a `u8`.
10pub type OptionU8 = Option<num::NonZeroU8>;
11
12// Ensure the sizes are identical.
13const_assert!(mem::size_of::<OptionU8>() == mem::size_of::<u8>());
14
15/// Add single flag to `SyntaxFormat`.
16macro_rules! add_flag {
17    ($format:ident, $bool:expr, $flag:ident) => {
18        if $bool {
19            $format |= flags::$flag;
20        }
21    };
22}
23
24/// Add multiple flags to `SyntaxFormat`.
25macro_rules! add_flags {
26    ($format:ident ; $($bool:expr, $flag:ident ;)*) => {{
27        $(add_flag!($format, $bool, $flag);)*
28    }};
29}
30
31/// Determine if a flag is set in the format.
32macro_rules! has_flag {
33    ($format:ident, $flag:ident) => {
34        $format & flags::$flag != 0
35    };
36}
37
38/// Unwrap `Option` as a const fn.
39#[inline(always)]
40const fn unwrap_or_zero(option: OptionU8) -> u8 {
41    match option {
42        Some(x) => x.get(),
43        None => 0,
44    }
45}
46
47/// Build number format from specifications.
48///
49/// Returns the format on calling build if it was able to compile the format,
50/// otherwise, returns None.
51///
52/// # Fields
53///
54/// * `digit_separator`                         - Character to separate digits.
55/// * `mantissa_radix`                          - Radix for mantissa digits.
56/// * `exponent_base`                           - Base for the exponent.
57/// * `exponent_radix`                          - Radix for the exponent digits.
58/// * `base_prefix`                             - Optional character for the
59///   base prefix.
60/// * `base_suffix`                             - Optional character for the
61///   base suffix.
62/// * `required_integer_digits`                 - If digits are required before
63///   the decimal point.
64/// * `required_fraction_digits`                - If digits are required after
65///   the decimal point.
66/// * `required_exponent_digits`                - If digits are required after
67///   the exponent character.
68/// * `required_mantissa_digits`                - If at least 1 significant
69///   digit is required.
70/// * `no_positive_mantissa_sign`               - If positive sign before the
71///   mantissa is not allowed.
72/// * `required_mantissa_sign`                  - If positive sign before the
73///   mantissa is required.
74/// * `no_exponent_notation`                    - If exponent notation is not
75///   allowed.
76/// * `no_positive_exponent_sign`               - If positive sign before the
77///   exponent is not allowed.
78/// * `required_exponent_sign`                  - If sign before the exponent is
79///   required.
80/// * `no_exponent_without_fraction`            - If exponent without fraction
81///   is not allowed.
82/// * `no_special`                              - If special (non-finite) values
83///   are not allowed.
84/// * `case_sensitive_special`                  - If special (non-finite) values
85///   are case-sensitive.
86/// * `no_integer_leading_zeros`                - If leading zeros before an
87///   integer are not allowed.
88/// * `no_float_leading_zeros`                  - If leading zeros before a
89///   float are not allowed.
90/// * `required_exponent_notation`              - If exponent notation is
91///   required.
92/// * `case_sensitive_exponent`                 - If exponent characters are
93///   case-sensitive.
94/// * `case_sensitive_base_prefix`              - If base prefixes are
95///   case-sensitive.
96/// * `case_sensitive_base_suffix`              - If base suffixes are
97///   case-sensitive.
98/// * `integer_internal_digit_separator`        - If digit separators are
99///   allowed between integer digits.
100/// * `fraction_internal_digit_separator`       - If digit separators are
101///   allowed between fraction digits.
102/// * `exponent_internal_digit_separator`       - If digit separators are
103///   allowed between exponent digits.
104/// * `integer_leading_digit_separator`         - If a digit separator is
105///   allowed before any integer digits.
106/// * `fraction_leading_digit_separator`        - If a digit separator is
107///   allowed before any fraction digits.
108/// * `exponent_leading_digit_separator`        - If a digit separator is
109///   allowed before any exponent digits.
110/// * `integer_trailing_digit_separator`        - If a digit separator is
111///   allowed after any integer digits.
112/// * `fraction_trailing_digit_separator`       - If a digit separator is
113///   allowed after any fraction digits.
114/// * `exponent_trailing_digit_separator`       - If a digit separator is
115///   allowed after any exponent digits.
116/// * `integer_consecutive_digit_separator`     - If multiple consecutive
117///   integer digit separators are allowed.
118/// * `fraction_consecutive_digit_separator`    - If multiple consecutive
119///   fraction digit separators are allowed.
120/// * `special_digit_separator`                 - If any digit separators are
121///   allowed in special (non-finite) values.
122///
123/// # Write Integer Fields
124///
125/// No fields are used for writing integers.
126///
127/// # Parse Integer Fields
128///
129/// These fields are used for parsing integers:
130///
131/// * `digit_separator`
132/// * `mantissa_radix`
133/// * `base_prefix`
134/// * `base_suffix`
135/// * `no_positive_mantissa_sign`
136/// * `required_mantissa_sign`
137/// * `no_integer_leading_zeros`
138/// * `integer_internal_digit_separator`
139/// * `integer_leading_digit_separator`
140/// * `integer_trailing_digit_separator`
141/// * `integer_consecutive_digit_separator`
142///
143/// # Write Float Fields
144///
145/// These fields are used for writing floats:
146///
147/// * `mantissa_radix`
148/// * `exponent_base`
149/// * `exponent_radix`
150/// * `no_positive_mantissa_sign`
151/// * `required_mantissa_sign`
152/// * `no_exponent_notation`
153/// * `no_positive_exponent_sign`
154/// * `required_exponent_sign`
155/// * `required_exponent_notation`
156///
157/// # Parse Float Fields
158///
159/// These fields are used for parsing floats:
160///
161/// * `digit_separator`
162/// * `mantissa_radix`
163/// * `exponent_base`
164/// * `exponent_radix`
165/// * `base_prefix`
166/// * `base_suffix`
167/// * `required_integer_digits`
168/// * `required_fraction_digits`
169/// * `required_exponent_digits`
170/// * `no_positive_mantissa_sign`
171/// * `required_mantissa_sign`
172/// * `no_exponent_notation`
173/// * `no_positive_exponent_sign`
174/// * `required_exponent_sign`
175/// * `no_exponent_without_fraction`
176/// * `no_special`
177/// * `case_sensitive_special`
178/// * `no_integer_leading_zeros`
179/// * `no_float_leading_zeros`
180/// * `required_exponent_notation`
181/// * `case_sensitive_exponent`
182/// * `case_sensitive_base_prefix`
183/// * `case_sensitive_base_suffix`
184/// * `integer_internal_digit_separator`
185/// * `fraction_internal_digit_separator`
186/// * `exponent_internal_digit_separator`
187/// * `integer_leading_digit_separator`
188/// * `fraction_leading_digit_separator`
189/// * `exponent_leading_digit_separator`
190/// * `integer_trailing_digit_separator`
191/// * `fraction_trailing_digit_separator`
192/// * `exponent_trailing_digit_separator`
193/// * `integer_consecutive_digit_separator`
194/// * `fraction_consecutive_digit_separator`
195/// * `special_digit_separator`
196pub struct NumberFormatBuilder {
197    digit_separator: OptionU8,
198    base_prefix: OptionU8,
199    base_suffix: OptionU8,
200    mantissa_radix: u8,
201    exponent_base: OptionU8,
202    exponent_radix: OptionU8,
203    required_integer_digits: bool,
204    required_fraction_digits: bool,
205    required_exponent_digits: bool,
206    required_mantissa_digits: bool,
207    no_positive_mantissa_sign: bool,
208    required_mantissa_sign: bool,
209    no_exponent_notation: bool,
210    no_positive_exponent_sign: bool,
211    required_exponent_sign: bool,
212    no_exponent_without_fraction: bool,
213    no_special: bool,
214    case_sensitive_special: bool,
215    no_integer_leading_zeros: bool,
216    no_float_leading_zeros: bool,
217    required_exponent_notation: bool,
218    case_sensitive_exponent: bool,
219    case_sensitive_base_prefix: bool,
220    case_sensitive_base_suffix: bool,
221    integer_internal_digit_separator: bool,
222    fraction_internal_digit_separator: bool,
223    exponent_internal_digit_separator: bool,
224    integer_leading_digit_separator: bool,
225    fraction_leading_digit_separator: bool,
226    exponent_leading_digit_separator: bool,
227    integer_trailing_digit_separator: bool,
228    fraction_trailing_digit_separator: bool,
229    exponent_trailing_digit_separator: bool,
230    integer_consecutive_digit_separator: bool,
231    fraction_consecutive_digit_separator: bool,
232    exponent_consecutive_digit_separator: bool,
233    special_digit_separator: bool,
234}
235
236impl NumberFormatBuilder {
237    // CONSTRUCTORS
238
239    /// Create new `NumberFormatBuilder` with default arguments.
240    #[inline(always)]
241    pub const fn new() -> Self {
242        Self {
243            digit_separator: None,
244            base_prefix: None,
245            base_suffix: None,
246            mantissa_radix: 10,
247            exponent_base: None,
248            exponent_radix: None,
249            required_integer_digits: false,
250            required_fraction_digits: false,
251            required_exponent_digits: true,
252            required_mantissa_digits: true,
253            no_positive_mantissa_sign: false,
254            required_mantissa_sign: false,
255            no_exponent_notation: false,
256            no_positive_exponent_sign: false,
257            required_exponent_sign: false,
258            no_exponent_without_fraction: false,
259            no_special: false,
260            case_sensitive_special: false,
261            no_integer_leading_zeros: false,
262            no_float_leading_zeros: false,
263            required_exponent_notation: false,
264            case_sensitive_exponent: false,
265            case_sensitive_base_prefix: false,
266            case_sensitive_base_suffix: false,
267            integer_internal_digit_separator: false,
268            fraction_internal_digit_separator: false,
269            exponent_internal_digit_separator: false,
270            integer_leading_digit_separator: false,
271            fraction_leading_digit_separator: false,
272            exponent_leading_digit_separator: false,
273            integer_trailing_digit_separator: false,
274            fraction_trailing_digit_separator: false,
275            exponent_trailing_digit_separator: false,
276            integer_consecutive_digit_separator: false,
277            fraction_consecutive_digit_separator: false,
278            exponent_consecutive_digit_separator: false,
279            special_digit_separator: false,
280        }
281    }
282
283    /// Create number format for standard, binary number.
284    #[cfg(feature = "power-of-two")]
285    pub const fn binary() -> u128 {
286        Self::from_radix(2)
287    }
288
289    /// Create number format for standard, octal number.
290    #[cfg(feature = "power-of-two")]
291    pub const fn octal() -> u128 {
292        Self::from_radix(8)
293    }
294
295    /// Create number format for standard, decimal number.
296    pub const fn decimal() -> u128 {
297        let mut builder = Self::new();
298        builder.mantissa_radix = 10;
299        builder.exponent_base = num::NonZeroU8::new(10);
300        builder.exponent_radix = num::NonZeroU8::new(10);
301        builder.build()
302    }
303
304    /// Create number format for standard, hexadecimal number.
305    #[cfg(feature = "power-of-two")]
306    pub const fn hexadecimal() -> u128 {
307        Self::from_radix(16)
308    }
309
310    /// Create number format from radix.
311    #[cfg(feature = "power-of-two")]
312    pub const fn from_radix(radix: u8) -> u128 {
313        Self::new()
314            .radix(radix)
315            .exponent_base(num::NonZeroU8::new(radix))
316            .exponent_radix(num::NonZeroU8::new(radix))
317            .build()
318    }
319
320    // GETTERS
321
322    /// Get the digit separator for the number format.
323    #[inline(always)]
324    pub const fn get_digit_separator(&self) -> OptionU8 {
325        self.digit_separator
326    }
327
328    /// Get the radix for mantissa digits.
329    #[inline(always)]
330    pub const fn get_mantissa_radix(&self) -> u8 {
331        self.mantissa_radix
332    }
333
334    /// Get the radix for the exponent.
335    #[inline(always)]
336    pub const fn get_exponent_base(&self) -> OptionU8 {
337        self.exponent_base
338    }
339
340    /// Get the radix for exponent digits.
341    #[inline(always)]
342    pub const fn get_exponent_radix(&self) -> OptionU8 {
343        self.exponent_radix
344    }
345
346    /// Get the optional character for the base prefix.
347    #[inline(always)]
348    pub const fn get_base_prefix(&self) -> OptionU8 {
349        self.base_prefix
350    }
351
352    /// Get the optional character for the base suffix.
353    #[inline(always)]
354    pub const fn get_base_suffix(&self) -> OptionU8 {
355        self.base_suffix
356    }
357
358    /// Get if digits are required before the decimal point.
359    #[inline(always)]
360    pub const fn get_required_integer_digits(&self) -> bool {
361        self.required_integer_digits
362    }
363
364    /// Get if digits are required after the decimal point.
365    #[inline(always)]
366    pub const fn get_required_fraction_digits(&self) -> bool {
367        self.required_fraction_digits
368    }
369
370    /// Get if digits are required after the exponent character.
371    #[inline(always)]
372    pub const fn get_required_exponent_digits(&self) -> bool {
373        self.required_exponent_digits
374    }
375
376    /// Get if at least 1 significant digit is required.
377    #[inline(always)]
378    pub const fn get_required_mantissa_digits(&self) -> bool {
379        self.required_mantissa_digits
380    }
381
382    /// Get if a positive sign before the mantissa is not allowed.
383    #[inline(always)]
384    pub const fn get_no_positive_mantissa_sign(&self) -> bool {
385        self.no_positive_mantissa_sign
386    }
387
388    /// Get if a sign symbol before the mantissa is required.
389    #[inline(always)]
390    pub const fn get_required_mantissa_sign(&self) -> bool {
391        self.required_mantissa_sign
392    }
393
394    /// Get if exponent notation is not allowed.
395    #[inline(always)]
396    pub const fn get_no_exponent_notation(&self) -> bool {
397        self.no_exponent_notation
398    }
399
400    /// Get if a positive sign before the exponent is not allowed.
401    #[inline(always)]
402    pub const fn get_no_positive_exponent_sign(&self) -> bool {
403        self.no_positive_exponent_sign
404    }
405
406    /// Get if a sign symbol before the exponent is required.
407    #[inline(always)]
408    pub const fn get_required_exponent_sign(&self) -> bool {
409        self.required_exponent_sign
410    }
411
412    /// Get if an exponent without fraction is not allowed.
413    #[inline(always)]
414    pub const fn get_no_exponent_without_fraction(&self) -> bool {
415        self.no_exponent_without_fraction
416    }
417
418    /// Get if special (non-finite) values are not allowed.
419    #[inline(always)]
420    pub const fn get_no_special(&self) -> bool {
421        self.no_special
422    }
423
424    /// Get if special (non-finite) values are case-sensitive.
425    #[inline(always)]
426    pub const fn get_case_sensitive_special(&self) -> bool {
427        self.case_sensitive_special
428    }
429
430    /// Get if leading zeros before an integer are not allowed.
431    #[inline(always)]
432    pub const fn get_no_integer_leading_zeros(&self) -> bool {
433        self.no_integer_leading_zeros
434    }
435
436    /// Get if leading zeros before a float are not allowed.
437    #[inline(always)]
438    pub const fn get_no_float_leading_zeros(&self) -> bool {
439        self.no_float_leading_zeros
440    }
441
442    /// Get if exponent notation is required.
443    #[inline(always)]
444    pub const fn get_required_exponent_notation(&self) -> bool {
445        self.required_exponent_notation
446    }
447
448    /// Get if exponent characters are case-sensitive.
449    #[inline(always)]
450    pub const fn get_case_sensitive_exponent(&self) -> bool {
451        self.case_sensitive_exponent
452    }
453
454    /// Get if base prefixes are case-sensitive.
455    #[inline(always)]
456    pub const fn get_case_sensitive_base_prefix(&self) -> bool {
457        self.case_sensitive_base_prefix
458    }
459
460    /// Get if base suffixes are case-sensitive.
461    #[inline(always)]
462    pub const fn get_case_sensitive_base_suffix(&self) -> bool {
463        self.case_sensitive_base_suffix
464    }
465
466    /// Get if digit separators are allowed between integer digits.
467    ///
468    /// This will not consider an input of only the digit separator
469    /// to be a valid separator: the digit separator must be surrounded by
470    /// digits.
471    #[inline(always)]
472    pub const fn get_integer_internal_digit_separator(&self) -> bool {
473        self.integer_internal_digit_separator
474    }
475
476    /// Get if digit separators are allowed between fraction digits.
477    ///
478    /// This will not consider an input of only the digit separator
479    /// to be a valid separator: the digit separator must be surrounded by
480    /// digits.
481    #[inline(always)]
482    pub const fn get_fraction_internal_digit_separator(&self) -> bool {
483        self.fraction_internal_digit_separator
484    }
485
486    /// Get if digit separators are allowed between exponent digits.
487    ///
488    /// This will not consider an input of only the digit separator
489    /// to be a valid separator: the digit separator must be surrounded by
490    /// digits.
491    #[inline(always)]
492    pub const fn get_exponent_internal_digit_separator(&self) -> bool {
493        self.exponent_internal_digit_separator
494    }
495
496    /// Get if a digit separator is allowed before any integer digits.
497    ///
498    /// This will consider an input of only the digit separator
499    /// to be a identical to empty input.
500    #[inline(always)]
501    pub const fn get_integer_leading_digit_separator(&self) -> bool {
502        self.integer_leading_digit_separator
503    }
504
505    /// Get if a digit separator is allowed before any fraction digits.
506    ///
507    /// This will consider an input of only the digit separator
508    /// to be a identical to empty input.
509    #[inline(always)]
510    pub const fn get_fraction_leading_digit_separator(&self) -> bool {
511        self.fraction_leading_digit_separator
512    }
513
514    /// Get if a digit separator is allowed before any exponent digits.
515    ///
516    /// This will consider an input of only the digit separator
517    /// to be a identical to empty input.
518    #[inline(always)]
519    pub const fn get_exponent_leading_digit_separator(&self) -> bool {
520        self.exponent_leading_digit_separator
521    }
522
523    /// Get if a digit separator is allowed after any integer digits.
524    ///
525    /// This will consider an input of only the digit separator
526    /// to be a identical to empty input.
527    #[inline(always)]
528    pub const fn get_integer_trailing_digit_separator(&self) -> bool {
529        self.integer_trailing_digit_separator
530    }
531
532    /// Get if a digit separator is allowed after any fraction digits.
533    ///
534    /// This will consider an input of only the digit separator
535    /// to be a identical to empty input.
536    #[inline(always)]
537    pub const fn get_fraction_trailing_digit_separator(&self) -> bool {
538        self.fraction_trailing_digit_separator
539    }
540
541    /// Get if a digit separator is allowed after any exponent digits.
542    ///
543    /// This will consider an input of only the digit separator
544    /// to be a identical to empty input.
545    #[inline(always)]
546    pub const fn get_exponent_trailing_digit_separator(&self) -> bool {
547        self.exponent_trailing_digit_separator
548    }
549
550    /// Get if multiple consecutive integer digit separators are allowed.
551    #[inline(always)]
552    pub const fn get_integer_consecutive_digit_separator(&self) -> bool {
553        self.integer_consecutive_digit_separator
554    }
555
556    /// Get if multiple consecutive fraction digit separators are allowed.
557    #[inline(always)]
558    pub const fn get_fraction_consecutive_digit_separator(&self) -> bool {
559        self.fraction_consecutive_digit_separator
560    }
561
562    /// Get if multiple consecutive exponent digit separators are allowed.
563    #[inline(always)]
564    pub const fn get_exponent_consecutive_digit_separator(&self) -> bool {
565        self.exponent_consecutive_digit_separator
566    }
567
568    /// Get if any digit separators are allowed in special (non-finite) values.
569    #[inline(always)]
570    pub const fn get_special_digit_separator(&self) -> bool {
571        self.special_digit_separator
572    }
573
574    // SETTERS
575
576    /// Set the digit separator for the number format.
577    #[inline(always)]
578    #[cfg(feature = "format")]
579    pub const fn digit_separator(mut self, character: OptionU8) -> Self {
580        self.digit_separator = character;
581        self
582    }
583
584    /// Alias for mantissa radix.
585    #[inline(always)]
586    #[cfg(feature = "power-of-two")]
587    pub const fn radix(self, radix: u8) -> Self {
588        self.mantissa_radix(radix)
589    }
590
591    /// Set the radix for mantissa digits.
592    #[inline(always)]
593    #[cfg(feature = "power-of-two")]
594    pub const fn mantissa_radix(mut self, radix: u8) -> Self {
595        self.mantissa_radix = radix;
596        self
597    }
598
599    /// Set the radix for the exponent.
600    #[inline(always)]
601    #[cfg(feature = "power-of-two")]
602    pub const fn exponent_base(mut self, base: OptionU8) -> Self {
603        self.exponent_base = base;
604        self
605    }
606
607    /// Set the radix for exponent digits.
608    #[inline(always)]
609    #[cfg(feature = "power-of-two")]
610    pub const fn exponent_radix(mut self, radix: OptionU8) -> Self {
611        self.exponent_radix = radix;
612        self
613    }
614
615    /// Set the optional character for the base prefix.
616    #[inline(always)]
617    #[cfg(all(feature = "power-of-two", feature = "format"))]
618    pub const fn base_prefix(mut self, base_prefix: OptionU8) -> Self {
619        self.base_prefix = base_prefix;
620        self
621    }
622
623    /// Set the optional character for the base suffix.
624    #[inline(always)]
625    #[cfg(all(feature = "power-of-two", feature = "format"))]
626    pub const fn base_suffix(mut self, base_suffix: OptionU8) -> Self {
627        self.base_suffix = base_suffix;
628        self
629    }
630
631    /// Set if digits are required before the decimal point.
632    #[inline(always)]
633    #[cfg(feature = "format")]
634    pub const fn required_integer_digits(mut self, flag: bool) -> Self {
635        self.required_integer_digits = flag;
636        self
637    }
638
639    /// Set if digits are required after the decimal point.
640    #[inline(always)]
641    #[cfg(feature = "format")]
642    pub const fn required_fraction_digits(mut self, flag: bool) -> Self {
643        self.required_fraction_digits = flag;
644        self
645    }
646
647    /// Set if digits are required after the exponent character.
648    #[inline(always)]
649    #[cfg(feature = "format")]
650    pub const fn required_exponent_digits(mut self, flag: bool) -> Self {
651        self.required_exponent_digits = flag;
652        self
653    }
654
655    /// Set if at least 1 significant digit is required.
656    #[inline(always)]
657    #[cfg(feature = "format")]
658    pub const fn required_mantissa_digits(mut self, flag: bool) -> Self {
659        self.required_mantissa_digits = flag;
660        self
661    }
662
663    /// Set if digits are required for all float components.
664    #[inline(always)]
665    #[cfg(feature = "format")]
666    pub const fn required_digits(mut self, flag: bool) -> Self {
667        self = self.required_integer_digits(flag);
668        self = self.required_fraction_digits(flag);
669        self = self.required_exponent_digits(flag);
670        self = self.required_mantissa_digits(flag);
671        self
672    }
673
674    /// Set if a positive sign before the mantissa is not allowed.
675    #[inline(always)]
676    #[cfg(feature = "format")]
677    pub const fn no_positive_mantissa_sign(mut self, flag: bool) -> Self {
678        self.no_positive_mantissa_sign = flag;
679        self
680    }
681
682    /// Set if a sign symbol before the mantissa is required.
683    #[inline(always)]
684    #[cfg(feature = "format")]
685    pub const fn required_mantissa_sign(mut self, flag: bool) -> Self {
686        self.required_mantissa_sign = flag;
687        self
688    }
689
690    /// Set if exponent notation is not allowed.
691    #[inline(always)]
692    #[cfg(feature = "format")]
693    pub const fn no_exponent_notation(mut self, flag: bool) -> Self {
694        self.no_exponent_notation = flag;
695        self
696    }
697
698    /// Set if a positive sign before the exponent is not allowed.
699    #[inline(always)]
700    #[cfg(feature = "format")]
701    pub const fn no_positive_exponent_sign(mut self, flag: bool) -> Self {
702        self.no_positive_exponent_sign = flag;
703        self
704    }
705
706    /// Set if a sign symbol before the exponent is required.
707    #[inline(always)]
708    #[cfg(feature = "format")]
709    pub const fn required_exponent_sign(mut self, flag: bool) -> Self {
710        self.required_exponent_sign = flag;
711        self
712    }
713
714    /// Set if an exponent without fraction is not allowed.
715    #[inline(always)]
716    #[cfg(feature = "format")]
717    pub const fn no_exponent_without_fraction(mut self, flag: bool) -> Self {
718        self.no_exponent_without_fraction = flag;
719        self
720    }
721
722    /// Set if special (non-finite) values are not allowed.
723    #[inline(always)]
724    #[cfg(feature = "format")]
725    pub const fn no_special(mut self, flag: bool) -> Self {
726        self.no_special = flag;
727        self
728    }
729
730    /// Set if special (non-finite) values are case-sensitive.
731    #[inline(always)]
732    #[cfg(feature = "format")]
733    pub const fn case_sensitive_special(mut self, flag: bool) -> Self {
734        self.case_sensitive_special = flag;
735        self
736    }
737
738    /// Set if leading zeros before an integer are not allowed.
739    #[inline(always)]
740    #[cfg(feature = "format")]
741    pub const fn no_integer_leading_zeros(mut self, flag: bool) -> Self {
742        self.no_integer_leading_zeros = flag;
743        self
744    }
745
746    /// Set if leading zeros before a float are not allowed.
747    #[inline(always)]
748    #[cfg(feature = "format")]
749    pub const fn no_float_leading_zeros(mut self, flag: bool) -> Self {
750        self.no_float_leading_zeros = flag;
751        self
752    }
753
754    /// Set if exponent notation is required.
755    #[inline(always)]
756    #[cfg(feature = "format")]
757    pub const fn required_exponent_notation(mut self, flag: bool) -> Self {
758        self.required_exponent_notation = flag;
759        self
760    }
761
762    /// Set if exponent characters are case-sensitive.
763    #[inline(always)]
764    #[cfg(feature = "format")]
765    pub const fn case_sensitive_exponent(mut self, flag: bool) -> Self {
766        self.case_sensitive_exponent = flag;
767        self
768    }
769
770    /// Set if base prefixes are case-sensitive.
771    #[inline(always)]
772    #[cfg(all(feature = "power-of-two", feature = "format"))]
773    pub const fn case_sensitive_base_prefix(mut self, flag: bool) -> Self {
774        self.case_sensitive_base_prefix = flag;
775        self
776    }
777
778    /// Set if base suffixes are case-sensitive.
779    #[inline(always)]
780    #[cfg(all(feature = "power-of-two", feature = "format"))]
781    pub const fn case_sensitive_base_suffix(mut self, flag: bool) -> Self {
782        self.case_sensitive_base_suffix = flag;
783        self
784    }
785
786    /// Set if digit separators are allowed between integer digits.
787    ///
788    /// This will not consider an input of only the digit separator
789    /// to be a valid separator: the digit separator must be surrounded by
790    /// digits.
791    #[inline(always)]
792    #[cfg(feature = "format")]
793    pub const fn integer_internal_digit_separator(mut self, flag: bool) -> Self {
794        self.integer_internal_digit_separator = flag;
795        self
796    }
797
798    /// Set if digit separators are allowed between fraction digits.
799    ///
800    /// This will not consider an input of only the digit separator
801    /// to be a valid separator: the digit separator must be surrounded by
802    /// digits.
803    #[inline(always)]
804    #[cfg(feature = "format")]
805    pub const fn fraction_internal_digit_separator(mut self, flag: bool) -> Self {
806        self.fraction_internal_digit_separator = flag;
807        self
808    }
809
810    /// Set if digit separators are allowed between exponent digits.
811    ///
812    /// This will not consider an input of only the digit separator
813    /// to be a valid separator: the digit separator must be surrounded by
814    /// digits.
815    #[inline(always)]
816    #[cfg(feature = "format")]
817    pub const fn exponent_internal_digit_separator(mut self, flag: bool) -> Self {
818        self.exponent_internal_digit_separator = flag;
819        self
820    }
821
822    /// Set all internal digit separator flags.
823    ///
824    /// This will not consider an input of only the digit separator
825    /// to be a valid separator: the digit separator must be surrounded by
826    /// digits.
827    #[inline(always)]
828    #[cfg(feature = "format")]
829    pub const fn internal_digit_separator(mut self, flag: bool) -> Self {
830        self = self.integer_internal_digit_separator(flag);
831        self = self.fraction_internal_digit_separator(flag);
832        self = self.exponent_internal_digit_separator(flag);
833        self
834    }
835
836    /// Set if a digit separator is allowed before any integer digits.
837    ///
838    /// This will consider an input of only the digit separator
839    /// to be a identical to empty input.
840    #[inline(always)]
841    #[cfg(feature = "format")]
842    pub const fn integer_leading_digit_separator(mut self, flag: bool) -> Self {
843        self.integer_leading_digit_separator = flag;
844        self
845    }
846
847    /// Set if a digit separator is allowed before any fraction digits.
848    ///
849    /// This will consider an input of only the digit separator
850    /// to be a identical to empty input.
851    #[inline(always)]
852    #[cfg(feature = "format")]
853    pub const fn fraction_leading_digit_separator(mut self, flag: bool) -> Self {
854        self.fraction_leading_digit_separator = flag;
855        self
856    }
857
858    /// Set if a digit separator is allowed before any exponent digits.
859    ///
860    /// This will consider an input of only the digit separator
861    /// to be a identical to empty input.
862    #[inline(always)]
863    #[cfg(feature = "format")]
864    pub const fn exponent_leading_digit_separator(mut self, flag: bool) -> Self {
865        self.exponent_leading_digit_separator = flag;
866        self
867    }
868
869    /// Set all leading digit separator flags.
870    ///
871    /// This will consider an input of only the digit separator
872    /// to be a identical to empty input.
873    #[inline(always)]
874    #[cfg(feature = "format")]
875    pub const fn leading_digit_separator(mut self, flag: bool) -> Self {
876        self = self.integer_leading_digit_separator(flag);
877        self = self.fraction_leading_digit_separator(flag);
878        self = self.exponent_leading_digit_separator(flag);
879        self
880    }
881
882    /// Set if a digit separator is allowed after any integer digits.
883    ///
884    /// This will consider an input of only the digit separator
885    /// to be a identical to empty input.
886    #[inline(always)]
887    #[cfg(feature = "format")]
888    pub const fn integer_trailing_digit_separator(mut self, flag: bool) -> Self {
889        self.integer_trailing_digit_separator = flag;
890        self
891    }
892
893    /// Set if a digit separator is allowed after any fraction digits.
894    ///
895    /// This will consider an input of only the digit separator
896    /// to be a identical to empty input.
897    #[inline(always)]
898    #[cfg(feature = "format")]
899    pub const fn fraction_trailing_digit_separator(mut self, flag: bool) -> Self {
900        self.fraction_trailing_digit_separator = flag;
901        self
902    }
903
904    /// Set if a digit separator is allowed after any exponent digits.
905    ///
906    /// This will consider an input of only the digit separator
907    /// to be a identical to empty input.
908    #[inline(always)]
909    #[cfg(feature = "format")]
910    pub const fn exponent_trailing_digit_separator(mut self, flag: bool) -> Self {
911        self.exponent_trailing_digit_separator = flag;
912        self
913    }
914
915    /// Set all trailing digit separator flags.
916    ///
917    /// This will consider an input of only the digit separator
918    /// to be a identical to empty input.
919    #[inline(always)]
920    #[cfg(feature = "format")]
921    pub const fn trailing_digit_separator(mut self, flag: bool) -> Self {
922        self = self.integer_trailing_digit_separator(flag);
923        self = self.fraction_trailing_digit_separator(flag);
924        self = self.exponent_trailing_digit_separator(flag);
925        self
926    }
927
928    /// Set if multiple consecutive integer digit separators are allowed.
929    #[inline(always)]
930    #[cfg(feature = "format")]
931    pub const fn integer_consecutive_digit_separator(mut self, flag: bool) -> Self {
932        self.integer_consecutive_digit_separator = flag;
933        self
934    }
935
936    /// Set if multiple consecutive fraction digit separators are allowed.
937    #[inline(always)]
938    #[cfg(feature = "format")]
939    pub const fn fraction_consecutive_digit_separator(mut self, flag: bool) -> Self {
940        self.fraction_consecutive_digit_separator = flag;
941        self
942    }
943
944    /// Set if multiple consecutive exponent digit separators are allowed.
945    #[inline(always)]
946    #[cfg(feature = "format")]
947    pub const fn exponent_consecutive_digit_separator(mut self, flag: bool) -> Self {
948        self.exponent_consecutive_digit_separator = flag;
949        self
950    }
951
952    /// Set all consecutive digit separator flags.
953    #[inline(always)]
954    #[cfg(feature = "format")]
955    pub const fn consecutive_digit_separator(mut self, flag: bool) -> Self {
956        self = self.integer_consecutive_digit_separator(flag);
957        self = self.fraction_consecutive_digit_separator(flag);
958        self = self.exponent_consecutive_digit_separator(flag);
959        self
960    }
961
962    /// Set if any digit separators are allowed in special (non-finite) values.
963    #[inline(always)]
964    #[cfg(feature = "format")]
965    pub const fn special_digit_separator(mut self, flag: bool) -> Self {
966        self.special_digit_separator = flag;
967        self
968    }
969
970    /// Set all digit separator flag masks.
971    #[inline(always)]
972    #[cfg(feature = "format")]
973    pub const fn digit_separator_flags(mut self, flag: bool) -> Self {
974        self = self.integer_digit_separator_flags(flag);
975        self = self.fraction_digit_separator_flags(flag);
976        self = self.exponent_digit_separator_flags(flag);
977        self = self.special_digit_separator(flag);
978        self
979    }
980
981    /// Set all integer digit separator flag masks.
982    #[inline(always)]
983    #[cfg(feature = "format")]
984    pub const fn integer_digit_separator_flags(mut self, flag: bool) -> Self {
985        self = self.integer_internal_digit_separator(flag);
986        self = self.integer_leading_digit_separator(flag);
987        self = self.integer_trailing_digit_separator(flag);
988        self = self.integer_consecutive_digit_separator(flag);
989        self
990    }
991
992    /// Set all fraction digit separator flag masks.
993    #[inline(always)]
994    #[cfg(feature = "format")]
995    pub const fn fraction_digit_separator_flags(mut self, flag: bool) -> Self {
996        self = self.fraction_internal_digit_separator(flag);
997        self = self.fraction_leading_digit_separator(flag);
998        self = self.fraction_trailing_digit_separator(flag);
999        self = self.fraction_consecutive_digit_separator(flag);
1000        self
1001    }
1002
1003    /// Set all exponent digit separator flag masks.
1004    #[inline(always)]
1005    #[cfg(feature = "format")]
1006    pub const fn exponent_digit_separator_flags(mut self, flag: bool) -> Self {
1007        self = self.exponent_internal_digit_separator(flag);
1008        self = self.exponent_leading_digit_separator(flag);
1009        self = self.exponent_trailing_digit_separator(flag);
1010        self = self.exponent_consecutive_digit_separator(flag);
1011        self
1012    }
1013
1014    // BUILDER
1015
1016    /// Create 128-bit, packed number format struct from builder options.
1017    ///
1018    /// NOTE: This function will never fail, due to issues with panicking
1019    /// (and therefore unwrapping Errors/Options) in const fns. It is
1020    /// therefore up to you to ensure the format is valid, called via the
1021    /// `is_valid` function on `NumberFormat`.
1022    #[inline(always)]
1023    pub const fn build(&self) -> u128 {
1024        let mut format: u128 = 0;
1025        add_flags!(
1026            format ;
1027            self.required_integer_digits, REQUIRED_INTEGER_DIGITS ;
1028            self.required_fraction_digits, REQUIRED_FRACTION_DIGITS ;
1029            self.required_exponent_digits, REQUIRED_EXPONENT_DIGITS ;
1030            self.required_mantissa_digits, REQUIRED_MANTISSA_DIGITS ;
1031            self.no_positive_mantissa_sign, NO_POSITIVE_MANTISSA_SIGN ;
1032            self.required_mantissa_sign, REQUIRED_MANTISSA_SIGN ;
1033            self.no_exponent_notation, NO_EXPONENT_NOTATION ;
1034            self.no_positive_exponent_sign, NO_POSITIVE_EXPONENT_SIGN ;
1035            self.required_exponent_sign, REQUIRED_EXPONENT_SIGN ;
1036            self.no_exponent_without_fraction, NO_EXPONENT_WITHOUT_FRACTION ;
1037            self.no_special, NO_SPECIAL ;
1038            self.case_sensitive_special, CASE_SENSITIVE_SPECIAL ;
1039            self.no_integer_leading_zeros, NO_INTEGER_LEADING_ZEROS ;
1040            self.no_float_leading_zeros, NO_FLOAT_LEADING_ZEROS ;
1041            self.required_exponent_notation, REQUIRED_EXPONENT_NOTATION ;
1042            self.case_sensitive_exponent, CASE_SENSITIVE_EXPONENT ;
1043            self.case_sensitive_base_prefix, CASE_SENSITIVE_BASE_PREFIX ;
1044            self.case_sensitive_base_suffix, CASE_SENSITIVE_BASE_SUFFIX ;
1045            self.integer_internal_digit_separator, INTEGER_INTERNAL_DIGIT_SEPARATOR ;
1046            self.fraction_internal_digit_separator, FRACTION_INTERNAL_DIGIT_SEPARATOR ;
1047            self.exponent_internal_digit_separator, EXPONENT_INTERNAL_DIGIT_SEPARATOR ;
1048            self.integer_leading_digit_separator, INTEGER_LEADING_DIGIT_SEPARATOR ;
1049            self.fraction_leading_digit_separator, FRACTION_LEADING_DIGIT_SEPARATOR ;
1050            self.exponent_leading_digit_separator, EXPONENT_LEADING_DIGIT_SEPARATOR ;
1051            self.integer_trailing_digit_separator, INTEGER_TRAILING_DIGIT_SEPARATOR ;
1052            self.fraction_trailing_digit_separator, FRACTION_TRAILING_DIGIT_SEPARATOR ;
1053            self.exponent_trailing_digit_separator, EXPONENT_TRAILING_DIGIT_SEPARATOR ;
1054            self.integer_consecutive_digit_separator, INTEGER_CONSECUTIVE_DIGIT_SEPARATOR ;
1055            self.fraction_consecutive_digit_separator, FRACTION_CONSECUTIVE_DIGIT_SEPARATOR ;
1056            self.exponent_consecutive_digit_separator, EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR ;
1057            self.special_digit_separator, SPECIAL_DIGIT_SEPARATOR ;
1058        );
1059        if format & flags::DIGIT_SEPARATOR_FLAG_MASK != 0 {
1060            format |=
1061                (unwrap_or_zero(self.digit_separator) as u128) << flags::DIGIT_SEPARATOR_SHIFT;
1062        }
1063        format |= (unwrap_or_zero(self.base_prefix) as u128) << flags::BASE_PREFIX_SHIFT;
1064        format |= (unwrap_or_zero(self.base_suffix) as u128) << flags::BASE_SUFFIX_SHIFT;
1065        format |= (self.mantissa_radix as u128) << flags::MANTISSA_RADIX_SHIFT;
1066        format |= (unwrap_or_zero(self.exponent_base) as u128) << flags::EXPONENT_BASE_SHIFT;
1067        format |= (unwrap_or_zero(self.exponent_radix) as u128) << flags::EXPONENT_RADIX_SHIFT;
1068
1069        format
1070    }
1071
1072    /// Re-create builder from format.
1073    #[inline(always)]
1074    pub const fn rebuild(format: u128) -> Self {
1075        NumberFormatBuilder {
1076            digit_separator: num::NonZeroU8::new(flags::digit_separator(format)),
1077            base_prefix: num::NonZeroU8::new(flags::base_prefix(format)),
1078            base_suffix: num::NonZeroU8::new(flags::base_suffix(format)),
1079            mantissa_radix: flags::mantissa_radix(format) as u8,
1080            exponent_base: num::NonZeroU8::new(flags::exponent_base(format) as u8),
1081            exponent_radix: num::NonZeroU8::new(flags::exponent_radix(format) as u8),
1082            required_integer_digits: has_flag!(format, REQUIRED_INTEGER_DIGITS),
1083            required_fraction_digits: has_flag!(format, REQUIRED_FRACTION_DIGITS),
1084            required_exponent_digits: has_flag!(format, REQUIRED_EXPONENT_DIGITS),
1085            required_mantissa_digits: has_flag!(format, REQUIRED_MANTISSA_DIGITS),
1086            no_positive_mantissa_sign: has_flag!(format, NO_POSITIVE_MANTISSA_SIGN),
1087            required_mantissa_sign: has_flag!(format, REQUIRED_MANTISSA_SIGN),
1088            no_exponent_notation: has_flag!(format, NO_EXPONENT_NOTATION),
1089            no_positive_exponent_sign: has_flag!(format, NO_POSITIVE_EXPONENT_SIGN),
1090            required_exponent_sign: has_flag!(format, REQUIRED_EXPONENT_SIGN),
1091            no_exponent_without_fraction: has_flag!(format, NO_EXPONENT_WITHOUT_FRACTION),
1092            no_special: has_flag!(format, NO_SPECIAL),
1093            case_sensitive_special: has_flag!(format, CASE_SENSITIVE_SPECIAL),
1094            no_integer_leading_zeros: has_flag!(format, NO_INTEGER_LEADING_ZEROS),
1095            no_float_leading_zeros: has_flag!(format, NO_FLOAT_LEADING_ZEROS),
1096            required_exponent_notation: has_flag!(format, REQUIRED_EXPONENT_NOTATION),
1097            case_sensitive_exponent: has_flag!(format, CASE_SENSITIVE_EXPONENT),
1098            case_sensitive_base_prefix: has_flag!(format, CASE_SENSITIVE_BASE_PREFIX),
1099            case_sensitive_base_suffix: has_flag!(format, CASE_SENSITIVE_BASE_SUFFIX),
1100            integer_internal_digit_separator: has_flag!(format, INTEGER_INTERNAL_DIGIT_SEPARATOR),
1101            fraction_internal_digit_separator: has_flag!(format, FRACTION_INTERNAL_DIGIT_SEPARATOR),
1102            exponent_internal_digit_separator: has_flag!(format, EXPONENT_INTERNAL_DIGIT_SEPARATOR),
1103            integer_leading_digit_separator: has_flag!(format, INTEGER_LEADING_DIGIT_SEPARATOR),
1104            fraction_leading_digit_separator: has_flag!(format, FRACTION_LEADING_DIGIT_SEPARATOR),
1105            exponent_leading_digit_separator: has_flag!(format, EXPONENT_LEADING_DIGIT_SEPARATOR),
1106            integer_trailing_digit_separator: has_flag!(format, INTEGER_TRAILING_DIGIT_SEPARATOR),
1107            fraction_trailing_digit_separator: has_flag!(format, FRACTION_TRAILING_DIGIT_SEPARATOR),
1108            exponent_trailing_digit_separator: has_flag!(format, EXPONENT_TRAILING_DIGIT_SEPARATOR),
1109            integer_consecutive_digit_separator: has_flag!(
1110                format,
1111                INTEGER_CONSECUTIVE_DIGIT_SEPARATOR
1112            ),
1113            fraction_consecutive_digit_separator: has_flag!(
1114                format,
1115                FRACTION_CONSECUTIVE_DIGIT_SEPARATOR
1116            ),
1117            exponent_consecutive_digit_separator: has_flag!(
1118                format,
1119                EXPONENT_CONSECUTIVE_DIGIT_SEPARATOR
1120            ),
1121            special_digit_separator: has_flag!(format, SPECIAL_DIGIT_SEPARATOR),
1122        }
1123    }
1124}
1125
1126impl Default for NumberFormatBuilder {
1127    #[inline(always)]
1128    fn default() -> Self {
1129        Self::new()
1130    }
1131}