16
16
17
17
pub use Alignment :: * ;
18
18
pub use Count :: * ;
19
- pub use Flag :: * ;
20
19
pub use Piece :: * ;
21
20
pub use Position :: * ;
22
21
@@ -111,8 +110,14 @@ pub struct FormatSpec<'a> {
111
110
pub fill : Option < char > ,
112
111
/// Optionally specified alignment.
113
112
pub align : Alignment ,
114
- /// Packed version of various flags provided.
115
- pub flags : u32 ,
113
+ /// The `+` or `-` flag.
114
+ pub sign : Option < Sign > ,
115
+ /// The `#` flag.
116
+ pub alternate : bool ,
117
+ /// The `0` flag.
118
+ pub zero_pad : bool ,
119
+ /// The `x` or `X` flag. (Only for `Debug`.)
120
+ pub debug_hex : Option < DebugHex > ,
116
121
/// The integer precision to use.
117
122
pub precision : Count < ' a > ,
118
123
/// The span of the precision formatting flag (for diagnostics).
@@ -162,24 +167,22 @@ pub enum Alignment {
162
167
AlignUnknown ,
163
168
}
164
169
165
- /// Various flags which can be applied to format strings. The meaning of these
166
- /// flags is defined by the formatters themselves.
170
+ /// Enum for the sign flags.
167
171
#[ derive( Copy , Clone , Debug , PartialEq ) ]
168
- pub enum Flag {
169
- /// A `+` will be used to denote positive numbers.
170
- FlagSignPlus ,
171
- /// A `-` will be used to denote negative numbers. This is the default.
172
- FlagSignMinus ,
173
- /// An alternate form will be used for the value. In the case of numbers,
174
- /// this means that the number will be prefixed with the supplied string.
175
- FlagAlternate ,
176
- /// For numbers, this means that the number will be padded with zeroes,
177
- /// and the sign (`+` or `-`) will precede them.
178
- FlagSignAwareZeroPad ,
179
- /// For Debug / `?`, format integers in lower-case hexadecimal.
180
- FlagDebugLowerHex ,
181
- /// For Debug / `?`, format integers in upper-case hexadecimal.
182
- FlagDebugUpperHex ,
172
+ pub enum Sign {
173
+ /// The `+` flag.
174
+ Plus ,
175
+ /// The `-` flag.
176
+ Minus ,
177
+ }
178
+
179
+ /// Enum for the debug hex flags.
180
+ #[ derive( Copy , Clone , Debug , PartialEq ) ]
181
+ pub enum DebugHex {
182
+ /// The `x` flag in `{:x?}`.
183
+ Lower ,
184
+ /// The `X` flag in `{:X?}`.
185
+ Upper ,
183
186
}
184
187
185
188
/// A count is used for the precision and width parameters of an integer, and
@@ -597,7 +600,10 @@ impl<'a> Parser<'a> {
597
600
let mut spec = FormatSpec {
598
601
fill : None ,
599
602
align : AlignUnknown ,
600
- flags : 0 ,
603
+ sign : None ,
604
+ alternate : false ,
605
+ zero_pad : false ,
606
+ debug_hex : None ,
601
607
precision : CountImplied ,
602
608
precision_span : None ,
603
609
width : CountImplied ,
@@ -626,13 +632,13 @@ impl<'a> Parser<'a> {
626
632
}
627
633
// Sign flags
628
634
if self . consume ( '+' ) {
629
- spec. flags |= 1 << ( FlagSignPlus as u32 ) ;
635
+ spec. sign = Some ( Sign :: Plus ) ;
630
636
} else if self . consume ( '-' ) {
631
- spec. flags |= 1 << ( FlagSignMinus as u32 ) ;
637
+ spec. sign = Some ( Sign :: Minus ) ;
632
638
}
633
639
// Alternate marker
634
640
if self . consume ( '#' ) {
635
- spec. flags |= 1 << ( FlagAlternate as u32 ) ;
641
+ spec. alternate = true ;
636
642
}
637
643
// Width and precision
638
644
let mut havewidth = false ;
@@ -647,7 +653,7 @@ impl<'a> Parser<'a> {
647
653
spec. width_span = Some ( self . span ( end - 1 , end + 1 ) ) ;
648
654
havewidth = true ;
649
655
} else {
650
- spec. flags |= 1 << ( FlagSignAwareZeroPad as u32 ) ;
656
+ spec. zero_pad = true ;
651
657
}
652
658
}
653
659
@@ -678,14 +684,14 @@ impl<'a> Parser<'a> {
678
684
// Optional radix followed by the actual format specifier
679
685
if self . consume ( 'x' ) {
680
686
if self . consume ( '?' ) {
681
- spec. flags |= 1 << ( FlagDebugLowerHex as u32 ) ;
687
+ spec. debug_hex = Some ( DebugHex :: Lower ) ;
682
688
spec. ty = "?" ;
683
689
} else {
684
690
spec. ty = "x" ;
685
691
}
686
692
} else if self . consume ( 'X' ) {
687
693
if self . consume ( '?' ) {
688
- spec. flags |= 1 << ( FlagDebugUpperHex as u32 ) ;
694
+ spec. debug_hex = Some ( DebugHex :: Upper ) ;
689
695
spec. ty = "?" ;
690
696
} else {
691
697
spec. ty = "X" ;
@@ -708,7 +714,10 @@ impl<'a> Parser<'a> {
708
714
let mut spec = FormatSpec {
709
715
fill : None ,
710
716
align : AlignUnknown ,
711
- flags : 0 ,
717
+ sign : None ,
718
+ alternate : false ,
719
+ zero_pad : false ,
720
+ debug_hex : None ,
712
721
precision : CountImplied ,
713
722
precision_span : None ,
714
723
width : CountImplied ,
0 commit comments