1212
1313use self :: Param :: * ;
1414use self :: States :: * ;
15- use self :: FormatState :: * ;
16- use self :: FormatOp :: * ;
1715
1816use std:: iter:: repeat;
1917
@@ -36,9 +34,9 @@ enum States {
3634
3735#[ derive( Copy , PartialEq , Clone ) ]
3836enum FormatState {
39- FormatStateFlags ,
40- FormatStateWidth ,
41- FormatStatePrecision ,
37+ Flags ,
38+ Width ,
39+ Precision ,
4240}
4341
4442/// Types of parameters a capability can use
@@ -210,22 +208,22 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) -> Result<Vec<
210208 if let Some ( arg) = stack. pop ( ) {
211209 let flags = Flags :: new ( ) ;
212210 let res = format ( arg, FormatOp :: from_char ( cur) , flags) ?;
213- output. extend ( res. iter ( ) . map ( |x| * x ) ) ;
211+ output. extend ( res. iter ( ) . cloned ( ) ) ;
214212 } else {
215213 return Err ( "stack is empty" . to_string ( ) ) ;
216214 }
217215 }
218216 ':' | '#' | ' ' | '.' | '0' ..='9' => {
219217 let mut flags = Flags :: new ( ) ;
220- let mut fstate = FormatStateFlags ;
218+ let mut fstate = FormatState :: Flags ;
221219 match cur {
222220 ':' => ( ) ,
223221 '#' => flags. alternate = true ,
224222 ' ' => flags. space = true ,
225- '.' => fstate = FormatStatePrecision ,
223+ '.' => fstate = FormatState :: Precision ,
226224 '0' ..='9' => {
227225 flags. width = cur as usize - '0' as usize ;
228- fstate = FormatStateWidth ;
226+ fstate = FormatState :: Width ;
229227 }
230228 _ => unreachable ! ( ) ,
231229 }
@@ -318,43 +316,43 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables) -> Result<Vec<
318316 ( _, 'd' ) | ( _, 'o' ) | ( _, 'x' ) | ( _, 'X' ) | ( _, 's' ) => {
319317 if let Some ( arg) = stack. pop ( ) {
320318 let res = format ( arg, FormatOp :: from_char ( cur) , * flags) ?;
321- output. extend ( res. iter ( ) . map ( |x| * x ) ) ;
319+ output. extend ( res. iter ( ) . cloned ( ) ) ;
322320 // will cause state to go to Nothing
323321 old_state = FormatPattern ( * flags, * fstate) ;
324322 } else {
325323 return Err ( "stack is empty" . to_string ( ) ) ;
326324 }
327325 }
328- ( FormatStateFlags , '#' ) => {
326+ ( FormatState :: Flags , '#' ) => {
329327 flags. alternate = true ;
330328 }
331- ( FormatStateFlags , '-' ) => {
329+ ( FormatState :: Flags , '-' ) => {
332330 flags. left = true ;
333331 }
334- ( FormatStateFlags , '+' ) => {
332+ ( FormatState :: Flags , '+' ) => {
335333 flags. sign = true ;
336334 }
337- ( FormatStateFlags , ' ' ) => {
335+ ( FormatState :: Flags , ' ' ) => {
338336 flags. space = true ;
339337 }
340- ( FormatStateFlags , '0' ..='9' ) => {
338+ ( FormatState :: Flags , '0' ..='9' ) => {
341339 flags. width = cur as usize - '0' as usize ;
342- * fstate = FormatStateWidth ;
340+ * fstate = FormatState :: Width ;
343341 }
344- ( FormatStateFlags , '.' ) => {
345- * fstate = FormatStatePrecision ;
342+ ( FormatState :: Flags , '.' ) => {
343+ * fstate = FormatState :: Precision ;
346344 }
347- ( FormatStateWidth , '0' ..='9' ) => {
345+ ( FormatState :: Width , '0' ..='9' ) => {
348346 let old = flags. width ;
349347 flags. width = flags. width * 10 + ( cur as usize - '0' as usize ) ;
350348 if flags. width < old {
351349 return Err ( "format width overflow" . to_string ( ) ) ;
352350 }
353351 }
354- ( FormatStateWidth , '.' ) => {
355- * fstate = FormatStatePrecision ;
352+ ( FormatState :: Width , '.' ) => {
353+ * fstate = FormatState :: Precision ;
356354 }
357- ( FormatStatePrecision , '0' ..='9' ) => {
355+ ( FormatState :: Precision , '0' ..='9' ) => {
358356 let old = flags. precision ;
359357 flags. precision = flags. precision * 10 + ( cur as usize - '0' as usize ) ;
360358 if flags. precision < old {
@@ -437,31 +435,31 @@ impl Flags {
437435
438436#[ derive( Copy , Clone ) ]
439437enum FormatOp {
440- FormatDigit ,
441- FormatOctal ,
442- FormatHex ,
443- FormatHEX ,
444- FormatString ,
438+ Digit ,
439+ Octal ,
440+ LowerHex ,
441+ UpperHex ,
442+ String ,
445443}
446444
447445impl FormatOp {
448446 fn from_char ( c : char ) -> FormatOp {
449447 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 ,
455453 _ => panic ! ( "bad FormatOp char" ) ,
456454 }
457455 }
458456 fn to_char ( self ) -> char {
459457 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' ,
465463 }
466464 }
467465}
@@ -470,7 +468,7 @@ fn format(val: Param, op: FormatOp, flags: Flags) -> Result<Vec<u8>, String> {
470468 let mut s = match val {
471469 Number ( d) => {
472470 match op {
473- FormatDigit => {
471+ FormatOp :: Digit => {
474472 if flags. sign {
475473 format ! ( "{:+01$}" , d, flags. precision)
476474 } else if d < 0 {
@@ -482,35 +480,35 @@ fn format(val: Param, op: FormatOp, flags: Flags) -> Result<Vec<u8>, String> {
482480 format ! ( "{:01$}" , d, flags. precision)
483481 }
484482 }
485- FormatOctal => {
483+ FormatOp :: Octal => {
486484 if flags. alternate {
487485 // Leading octal zero counts against precision.
488486 format ! ( "0{:01$o}" , d, flags. precision. saturating_sub( 1 ) )
489487 } else {
490488 format ! ( "{:01$o}" , d, flags. precision)
491489 }
492490 }
493- FormatHex => {
491+ FormatOp :: LowerHex => {
494492 if flags. alternate && d != 0 {
495493 format ! ( "0x{:01$x}" , d, flags. precision)
496494 } else {
497495 format ! ( "{:01$x}" , d, flags. precision)
498496 }
499497 }
500- FormatHEX => {
498+ FormatOp :: UpperHex => {
501499 if flags. alternate && d != 0 {
502500 format ! ( "0X{:01$X}" , d, flags. precision)
503501 } else {
504502 format ! ( "{:01$X}" , d, flags. precision)
505503 }
506504 }
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 ( ) ) ,
508506 }
509507 . into_bytes ( )
510508 }
511509 Words ( s) => {
512510 match op {
513- FormatString => {
511+ FormatOp :: String => {
514512 let mut s = s. into_bytes ( ) ;
515513 if flags. precision > 0 && flags. precision < s. len ( ) {
516514 s. truncate ( flags. precision ) ;
0 commit comments