@@ -516,14 +516,15 @@ impl FormatSpec {
516
516
}
517
517
518
518
#[ inline]
519
- fn format_int_radix ( & self , magnitude : BigInt , radix : u32 ) -> Result < String , & ' static str > {
519
+ fn format_int_radix ( & self , magnitude : BigInt , radix : u32 ) -> Result < String , String > {
520
520
match self . precision {
521
- Some ( _) => Err ( "Precision not allowed in integer format specifier" ) ,
521
+ Some ( _) => Err ( "Precision not allowed in integer format specifier" . to_owned ( ) ) ,
522
522
None => Ok ( magnitude. to_str_radix ( radix) ) ,
523
523
}
524
524
}
525
525
526
- pub fn format_int ( & self , num : & BigInt ) -> Result < String , & ' static str > {
526
+ pub fn format_int ( & self , num : & BigInt ) -> Result < String , String > {
527
+ self . validate_format ( FormatType :: Decimal ) ?;
527
528
let magnitude = num. abs ( ) ;
528
529
let prefix = if self . alternate_form {
529
530
match self . format_type {
@@ -536,30 +537,34 @@ impl FormatSpec {
536
537
} else {
537
538
""
538
539
} ;
539
- let raw_magnitude_str: Result < String , & ' static str > = match self . format_type {
540
+ let raw_magnitude_str: Result < String , String > = match self . format_type {
540
541
Some ( FormatType :: Binary ) => self . format_int_radix ( magnitude, 2 ) ,
541
542
Some ( FormatType :: Decimal ) => self . format_int_radix ( magnitude, 10 ) ,
542
543
Some ( FormatType :: Octal ) => self . format_int_radix ( magnitude, 8 ) ,
543
544
Some ( FormatType :: HexLower ) => self . format_int_radix ( magnitude, 16 ) ,
544
545
Some ( FormatType :: HexUpper ) => match self . precision {
545
- Some ( _) => Err ( "Precision not allowed in integer format specifier" ) ,
546
+ Some ( _) => Err ( "Precision not allowed in integer format specifier" . to_owned ( ) ) ,
546
547
None => {
547
548
let mut result = magnitude. to_str_radix ( 16 ) ;
548
549
result. make_ascii_uppercase ( ) ;
549
550
Ok ( result)
550
551
}
551
552
} ,
552
553
Some ( FormatType :: Number ) => self . format_int_radix ( magnitude, 10 ) ,
553
- Some ( FormatType :: String ) => Err ( "Unknown format code 's' for object of type 'int'" ) ,
554
+ Some ( FormatType :: String ) => {
555
+ Err ( "Unknown format code 's' for object of type 'int'" . to_owned ( ) )
556
+ }
554
557
Some ( FormatType :: Character ) => match ( self . sign , self . alternate_form ) {
555
- ( Some ( _) , _) => Err ( "Sign not allowed with integer format specifier 'c'" ) ,
556
- ( _, true ) => {
557
- Err ( "Alternate form (#) not allowed with integer format specifier 'c'" )
558
+ ( Some ( _) , _) => {
559
+ Err ( "Sign not allowed with integer format specifier 'c'" . to_owned ( ) )
558
560
}
561
+ ( _, true ) => Err (
562
+ "Alternate form (#) not allowed with integer format specifier 'c'" . to_owned ( ) ,
563
+ ) ,
559
564
( _, _) => match num. to_u32 ( ) {
560
565
Some ( n) if n <= 0x10ffff => Ok ( std:: char:: from_u32 ( n) . unwrap ( ) . to_string ( ) ) ,
561
566
// TODO: raise OverflowError
562
- Some ( _) | None => Err ( "%c arg not in range(0x110000)" ) ,
567
+ Some ( _) | None => Err ( "%c arg not in range(0x110000)" . to_owned ( ) ) ,
563
568
} ,
564
569
} ,
565
570
Some ( FormatType :: GeneralFormatUpper )
@@ -569,8 +574,8 @@ impl FormatSpec {
569
574
| Some ( FormatType :: ExponentUpper )
570
575
| Some ( FormatType :: ExponentLower )
571
576
| Some ( FormatType :: Percentage ) => match num. to_f64 ( ) {
572
- Some ( float) => return self . format_float ( float) ,
573
- _ => Err ( "Unable to convert int to float" ) ,
577
+ Some ( float) => return self . format_float ( float) . map_err ( |msg| msg . to_owned ( ) ) ,
578
+ _ => Err ( "Unable to convert int to float" . to_owned ( ) ) ,
574
579
} ,
575
580
None => self . format_int_radix ( magnitude, 10 ) ,
576
581
} ;
@@ -586,6 +591,7 @@ impl FormatSpec {
586
591
let sign_prefix = format ! ( "{}{}" , sign_str, prefix) ;
587
592
let magnitude_str = self . add_magnitude_separators ( raw_magnitude_str?, & sign_prefix) ;
588
593
self . format_sign_and_align ( & magnitude_str, & sign_prefix, FormatAlign :: Right )
594
+ . map_err ( |msg| msg. to_owned ( ) )
589
595
}
590
596
591
597
pub fn format_string ( & self , s : & str ) -> Result < String , & ' static str > {
0 commit comments