@@ -422,10 +422,11 @@ impl FormatSpec {
422
422
}
423
423
}
424
424
425
- pub fn format_float ( & self , num : f64 ) -> Result < String , & ' static str > {
425
+ pub fn format_float ( & self , num : f64 ) -> Result < String , String > {
426
+ self . validate_format ( FormatType :: FixedPointLower ) ?;
426
427
let precision = self . precision . unwrap_or ( 6 ) ;
427
428
let magnitude = num. abs ( ) ;
428
- let raw_magnitude_str: Result < String , & ' static str > = match self . format_type {
429
+ let raw_magnitude_str: Result < String , String > = match self . format_type {
429
430
Some ( FormatType :: FixedPointUpper ) => Ok ( float_ops:: format_fixed (
430
431
precision,
431
432
magnitude,
@@ -436,17 +437,21 @@ impl FormatSpec {
436
437
magnitude,
437
438
float_ops:: Case :: Lower ,
438
439
) ) ,
439
- Some ( FormatType :: Decimal ) => Err ( "Unknown format code 'd' for object of type 'float'" ) ,
440
- Some ( FormatType :: Binary ) => Err ( "Unknown format code 'b' for object of type 'float'" ) ,
441
- Some ( FormatType :: Octal ) => Err ( "Unknown format code 'o' for object of type 'float'" ) ,
442
- Some ( FormatType :: HexLower ) => Err ( "Unknown format code 'x' for object of type 'float'" ) ,
443
- Some ( FormatType :: HexUpper ) => Err ( "Unknown format code 'X' for object of type 'float'" ) ,
444
- Some ( FormatType :: String ) => Err ( "Unknown format code 's' for object of type 'float'" ) ,
445
- Some ( FormatType :: Character ) => {
446
- Err ( "Unknown format code 'c' for object of type 'float'" )
440
+ Some ( FormatType :: Decimal )
441
+ | Some ( FormatType :: Binary )
442
+ | Some ( FormatType :: Octal )
443
+ | Some ( FormatType :: HexLower )
444
+ | Some ( FormatType :: HexUpper )
445
+ | Some ( FormatType :: String )
446
+ | Some ( FormatType :: Character ) => {
447
+ let ch = char:: from ( self . format_type . as_ref ( ) . unwrap ( ) ) ;
448
+ Err ( format ! (
449
+ "Unknown format code '{}' for object of type 'float'" ,
450
+ ch
451
+ ) )
447
452
}
448
453
Some ( FormatType :: Number ) => {
449
- Err ( "Format code 'n' for object of type 'float' not implemented yet" )
454
+ Err ( "Format code 'n' for object of type 'float' not implemented yet" . to_owned ( ) )
450
455
}
451
456
Some ( FormatType :: GeneralFormatUpper ) => {
452
457
let precision = if precision == 0 { 1 } else { precision } ;
@@ -513,6 +518,7 @@ impl FormatSpec {
513
518
} ;
514
519
let magnitude_str = self . add_magnitude_separators ( raw_magnitude_str?, sign_str) ;
515
520
self . format_sign_and_align ( & magnitude_str, sign_str, FormatAlign :: Right )
521
+ . map_err ( |msg| msg. to_owned ( ) )
516
522
}
517
523
518
524
#[ inline]
@@ -574,7 +580,7 @@ impl FormatSpec {
574
580
| Some ( FormatType :: ExponentUpper )
575
581
| Some ( FormatType :: ExponentLower )
576
582
| Some ( FormatType :: Percentage ) => match num. to_f64 ( ) {
577
- Some ( float) => return self . format_float ( float) . map_err ( |msg| msg . to_owned ( ) ) ,
583
+ Some ( float) => return self . format_float ( float) ,
578
584
_ => Err ( "Unable to convert int to float" . to_owned ( ) ) ,
579
585
} ,
580
586
None => self . format_int_radix ( magnitude, 10 ) ,
0 commit comments