12
12
13
13
use self :: Param :: * ;
14
14
use self :: States :: * ;
15
- use self :: FormatState :: * ;
16
- use self :: FormatOp :: * ;
17
15
18
16
use std:: iter:: repeat;
19
17
@@ -36,9 +34,9 @@ enum States {
36
34
37
35
#[ derive( Copy , PartialEq , Clone ) ]
38
36
enum FormatState {
39
- FormatStateFlags ,
40
- FormatStateWidth ,
41
- FormatStatePrecision ,
37
+ Flags ,
38
+ Width ,
39
+ Precision ,
42
40
}
43
41
44
42
/// Types of parameters a capability can use
@@ -210,22 +208,22 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) -> Result<Vec<
210
208
if let Some ( arg) = stack. pop ( ) {
211
209
let flags = Flags :: new ( ) ;
212
210
let res = format ( arg, FormatOp :: from_char ( cur) , flags) ?;
213
- output. extend ( res. iter ( ) . map ( |x| * x ) ) ;
211
+ output. extend ( res. iter ( ) . cloned ( ) ) ;
214
212
} else {
215
213
return Err ( "stack is empty" . to_string ( ) ) ;
216
214
}
217
215
}
218
216
':' | '#' | ' ' | '.' | '0' ..='9' => {
219
217
let mut flags = Flags :: new ( ) ;
220
- let mut fstate = FormatStateFlags ;
218
+ let mut fstate = FormatState :: Flags ;
221
219
match cur {
222
220
':' => ( ) ,
223
221
'#' => flags. alternate = true ,
224
222
' ' => flags. space = true ,
225
- '.' => fstate = FormatStatePrecision ,
223
+ '.' => fstate = FormatState :: Precision ,
226
224
'0' ..='9' => {
227
225
flags. width = cur as usize - '0' as usize ;
228
- fstate = FormatStateWidth ;
226
+ fstate = FormatState :: Width ;
229
227
}
230
228
_ => unreachable ! ( ) ,
231
229
}
@@ -318,43 +316,43 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) -> Result<Vec<
318
316
( _, 'd' ) | ( _, 'o' ) | ( _, 'x' ) | ( _, 'X' ) | ( _, 's' ) => {
319
317
if let Some ( arg) = stack. pop ( ) {
320
318
let res = format ( arg, FormatOp :: from_char ( cur) , * flags) ?;
321
- output. extend ( res. iter ( ) . map ( |x| * x ) ) ;
319
+ output. extend ( res. iter ( ) . cloned ( ) ) ;
322
320
// will cause state to go to Nothing
323
321
old_state = FormatPattern ( * flags, * fstate) ;
324
322
} else {
325
323
return Err ( "stack is empty" . to_string ( ) ) ;
326
324
}
327
325
}
328
- ( FormatStateFlags , '#' ) => {
326
+ ( FormatState :: Flags , '#' ) => {
329
327
flags. alternate = true ;
330
328
}
331
- ( FormatStateFlags , '-' ) => {
329
+ ( FormatState :: Flags , '-' ) => {
332
330
flags. left = true ;
333
331
}
334
- ( FormatStateFlags , '+' ) => {
332
+ ( FormatState :: Flags , '+' ) => {
335
333
flags. sign = true ;
336
334
}
337
- ( FormatStateFlags , ' ' ) => {
335
+ ( FormatState :: Flags , ' ' ) => {
338
336
flags. space = true ;
339
337
}
340
- ( FormatStateFlags , '0' ..='9' ) => {
338
+ ( FormatState :: Flags , '0' ..='9' ) => {
341
339
flags. width = cur as usize - '0' as usize ;
342
- * fstate = FormatStateWidth ;
340
+ * fstate = FormatState :: Width ;
343
341
}
344
- ( FormatStateFlags , '.' ) => {
345
- * fstate = FormatStatePrecision ;
342
+ ( FormatState :: Flags , '.' ) => {
343
+ * fstate = FormatState :: Precision ;
346
344
}
347
- ( FormatStateWidth , '0' ..='9' ) => {
345
+ ( FormatState :: Width , '0' ..='9' ) => {
348
346
let old = flags. width ;
349
347
flags. width = flags. width * 10 + ( cur as usize - '0' as usize ) ;
350
348
if flags. width < old {
351
349
return Err ( "format width overflow" . to_string ( ) ) ;
352
350
}
353
351
}
354
- ( FormatStateWidth , '.' ) => {
355
- * fstate = FormatStatePrecision ;
352
+ ( FormatState :: Width , '.' ) => {
353
+ * fstate = FormatState :: Precision ;
356
354
}
357
- ( FormatStatePrecision , '0' ..='9' ) => {
355
+ ( FormatState :: Precision , '0' ..='9' ) => {
358
356
let old = flags. precision ;
359
357
flags. precision = flags. precision * 10 + ( cur as usize - '0' as usize ) ;
360
358
if flags. precision < old {
@@ -437,31 +435,31 @@ impl Flags {
437
435
438
436
#[ derive( Copy , Clone ) ]
439
437
enum FormatOp {
440
- FormatDigit ,
441
- FormatOctal ,
442
- FormatHex ,
443
- FormatHEX ,
444
- FormatString ,
438
+ Digit ,
439
+ Octal ,
440
+ LowerHex ,
441
+ UpperHex ,
442
+ String ,
445
443
}
446
444
447
445
impl FormatOp {
448
446
fn from_char ( c : char ) -> FormatOp {
449
447
match c {
450
- 'd' => FormatDigit ,
451
- 'o' => FormatOctal ,
452
- 'x' => FormatHex ,
453
- 'X' => FormatHEX ,
454
- 's' => FormatString ,
448
+ 'd' => FormatOp :: Digit ,
449
+ 'o' => FormatOp :: Octal ,
450
+ 'x' => FormatOp :: LowerHex ,
451
+ 'X' => FormatOp :: UpperHex ,
452
+ 's' => FormatOp :: String ,
455
453
_ => panic ! ( "bad FormatOp char" ) ,
456
454
}
457
455
}
458
456
fn to_char ( self ) -> char {
459
457
match self {
460
- FormatDigit => 'd' ,
461
- FormatOctal => 'o' ,
462
- FormatHex => 'x' ,
463
- FormatHEX => 'X' ,
464
- FormatString => 's' ,
458
+ FormatOp :: Digit => 'd' ,
459
+ FormatOp :: Octal => 'o' ,
460
+ FormatOp :: LowerHex => 'x' ,
461
+ FormatOp :: UpperHex => 'X' ,
462
+ FormatOp :: String => 's' ,
465
463
}
466
464
}
467
465
}
@@ -470,7 +468,7 @@ fn format(val: Param, op: FormatOp, flags: Flags) -> Result<Vec<u8>, String> {
470
468
let mut s = match val {
471
469
Number ( d) => {
472
470
match op {
473
- FormatDigit => {
471
+ FormatOp :: Digit => {
474
472
if flags. sign {
475
473
format ! ( "{:+01$}" , d, flags. precision)
476
474
} else if d < 0 {
@@ -482,35 +480,35 @@ fn format(val: Param, op: FormatOp, flags: Flags) -> Result<Vec<u8>, String> {
482
480
format ! ( "{:01$}" , d, flags. precision)
483
481
}
484
482
}
485
- FormatOctal => {
483
+ FormatOp :: Octal => {
486
484
if flags. alternate {
487
485
// Leading octal zero counts against precision.
488
486
format ! ( "0{:01$o}" , d, flags. precision. saturating_sub( 1 ) )
489
487
} else {
490
488
format ! ( "{:01$o}" , d, flags. precision)
491
489
}
492
490
}
493
- FormatHex => {
491
+ FormatOp :: LowerHex => {
494
492
if flags. alternate && d != 0 {
495
493
format ! ( "0x{:01$x}" , d, flags. precision)
496
494
} else {
497
495
format ! ( "{:01$x}" , d, flags. precision)
498
496
}
499
497
}
500
- FormatHEX => {
498
+ FormatOp :: UpperHex => {
501
499
if flags. alternate && d != 0 {
502
500
format ! ( "0X{:01$X}" , d, flags. precision)
503
501
} else {
504
502
format ! ( "{:01$X}" , d, flags. precision)
505
503
}
506
504
}
507
- FormatString => return Err ( "non-number on stack with %s" . to_string ( ) ) ,
505
+ FormatOp :: String => return Err ( "non-number on stack with %s" . to_string ( ) ) ,
508
506
}
509
507
. into_bytes ( )
510
508
}
511
509
Words ( s) => {
512
510
match op {
513
- FormatString => {
511
+ FormatOp :: String => {
514
512
let mut s = s. into_bytes ( ) ;
515
513
if flags. precision > 0 && flags. precision < s. len ( ) {
516
514
s. truncate ( flags. precision ) ;
0 commit comments