@@ -18,7 +18,7 @@ impl FormatAlign {
18
18
'>' => Some ( FormatAlign :: Right ) ,
19
19
'=' => Some ( FormatAlign :: AfterSign ) ,
20
20
'^' => Some ( FormatAlign :: Center ) ,
21
- _ => None ,
21
+ _ => None ,
22
22
}
23
23
}
24
24
}
@@ -75,8 +75,7 @@ fn get_num_digits(text: &str) -> usize {
75
75
text. len ( )
76
76
}
77
77
78
- fn parse_align ( text : & str ) -> ( Option < FormatAlign > , & str ) {
79
-
78
+ fn parse_align ( text : & str ) -> ( Option < FormatAlign > , & str ) {
80
79
let mut chars = text. chars ( ) ;
81
80
let maybe_align = chars. next ( ) . and_then ( FormatAlign :: from_char) ;
82
81
if maybe_align. is_some ( ) {
@@ -105,21 +104,24 @@ fn parse_fill_and_align(text: &str) -> (Option<char>, Option<FormatAlign>, &str)
105
104
}
106
105
107
106
fn parse_number ( text : & str ) -> ( Option < usize > , & str ) {
108
- let num_digits: usize = get_num_digits ( text) ;
107
+ let num_digits: usize = get_num_digits ( text) ;
109
108
if num_digits == 0 {
110
- return ( None , text)
109
+ return ( None , text) ;
111
110
}
112
111
// This should never fail
113
- ( Some ( text[ ..num_digits] . parse :: < usize > ( ) . unwrap ( ) ) , & text[ num_digits..] )
112
+ (
113
+ Some ( text[ ..num_digits] . parse :: < usize > ( ) . unwrap ( ) ) ,
114
+ & text[ num_digits..] ,
115
+ )
114
116
}
115
117
116
- fn parse_sign ( text : & str ) -> ( Option < FormatSign > , & str ) {
118
+ fn parse_sign ( text : & str ) -> ( Option < FormatSign > , & str ) {
117
119
let mut chars = text. chars ( ) ;
118
120
match chars. next ( ) {
119
121
Some ( '-' ) => ( Some ( FormatSign :: Minus ) , chars. as_str ( ) ) ,
120
122
Some ( '+' ) => ( Some ( FormatSign :: Plus ) , chars. as_str ( ) ) ,
121
123
Some ( ' ' ) => ( Some ( FormatSign :: MinusOrSpace ) , chars. as_str ( ) ) ,
122
- _ => ( None , text)
124
+ _ => ( None , text) ,
123
125
}
124
126
}
125
127
@@ -149,7 +151,7 @@ fn parse_precision(text: &str) -> (Option<usize>, &str) {
149
151
} else {
150
152
( None , text)
151
153
}
152
- } ,
154
+ }
153
155
_ => ( None , text) ,
154
156
}
155
157
}
@@ -211,10 +213,16 @@ impl FormatSpec {
211
213
}
212
214
213
215
fn compute_fill_string ( fill_char : char , fill_chars_needed : i32 ) -> String {
214
- ( 0 ..fill_chars_needed) . map ( |_| fill_char) . collect :: < String > ( )
216
+ ( 0 ..fill_chars_needed)
217
+ . map ( |_| fill_char)
218
+ . collect :: < String > ( )
215
219
}
216
220
217
- fn add_magnitude_separators_for_char ( magnitude_string : String , interval : usize , separator : char ) -> String {
221
+ fn add_magnitude_separators_for_char (
222
+ magnitude_string : String ,
223
+ interval : usize ,
224
+ separator : char ,
225
+ ) -> String {
218
226
let mut result = String :: new ( ) ;
219
227
let mut remaining: usize = magnitude_string. len ( ) % interval;
220
228
if remaining == 0 {
@@ -245,13 +253,21 @@ impl FormatSpec {
245
253
246
254
fn add_magnitude_separators ( & self , magnitude_string : String ) -> String {
247
255
match self . grouping_option {
248
- Some ( FormatGrouping :: Comma ) => FormatSpec :: add_magnitude_separators_for_char ( magnitude_string, self . get_separator_interval ( ) , ',' ) ,
249
- Some ( FormatGrouping :: Underscore ) => FormatSpec :: add_magnitude_separators_for_char ( magnitude_string, self . get_separator_interval ( ) , '_' ) ,
250
- None => magnitude_string
256
+ Some ( FormatGrouping :: Comma ) => FormatSpec :: add_magnitude_separators_for_char (
257
+ magnitude_string,
258
+ self . get_separator_interval ( ) ,
259
+ ',' ,
260
+ ) ,
261
+ Some ( FormatGrouping :: Underscore ) => FormatSpec :: add_magnitude_separators_for_char (
262
+ magnitude_string,
263
+ self . get_separator_interval ( ) ,
264
+ '_' ,
265
+ ) ,
266
+ None => magnitude_string,
251
267
}
252
268
}
253
269
254
- pub fn format_int ( & self , num : & BigInt ) -> Result < String , & ' static str > {
270
+ pub fn format_int ( & self , num : & BigInt ) -> Result < String , & ' static str > {
255
271
let fill_char = self . fill . unwrap_or ( ' ' ) ;
256
272
let magnitude = num. abs ( ) ;
257
273
let prefix = if self . alternate_form {
@@ -262,7 +278,9 @@ impl FormatSpec {
262
278
Some ( FormatType :: HexUpper ) => "0x" ,
263
279
_ => "" ,
264
280
}
265
- } else { "" } ;
281
+ } else {
282
+ ""
283
+ } ;
266
284
let raw_magnitude_string_result: Result < String , & ' static str > = match self . format_type {
267
285
Some ( FormatType :: Binary ) => Ok ( magnitude. to_str_radix ( 2 ) ) ,
268
286
Some ( FormatType :: Decimal ) => Ok ( magnitude. to_str_radix ( 10 ) ) ,
@@ -276,18 +294,34 @@ impl FormatSpec {
276
294
Some ( FormatType :: Number ) => Ok ( magnitude. to_str_radix ( 10 ) ) ,
277
295
Some ( FormatType :: String ) => Err ( "Unknown format code 's' for object of type 'int'" ) ,
278
296
Some ( FormatType :: Character ) => Err ( "Unknown format code 'c' for object of type 'int'" ) ,
279
- Some ( FormatType :: GeneralFormatUpper ) => Err ( "Unknown format code 'G' for object of type 'int'" ) ,
280
- Some ( FormatType :: GeneralFormatLower ) => Err ( "Unknown format code 'g' for object of type 'int'" ) ,
281
- Some ( FormatType :: ExponentUpper ) => Err ( "Unknown format code 'E' for object of type 'int'" ) ,
282
- Some ( FormatType :: ExponentLower ) => Err ( "Unknown format code 'e' for object of type 'int'" ) ,
283
- Some ( FormatType :: FixedPointUpper ) => Err ( "Unknown format code 'F' for object of type 'int'" ) ,
284
- Some ( FormatType :: FixedPointLower ) => Err ( "Unknown format code 'f' for object of type 'int'" ) ,
297
+ Some ( FormatType :: GeneralFormatUpper ) => {
298
+ Err ( "Unknown format code 'G' for object of type 'int'" )
299
+ }
300
+ Some ( FormatType :: GeneralFormatLower ) => {
301
+ Err ( "Unknown format code 'g' for object of type 'int'" )
302
+ }
303
+ Some ( FormatType :: ExponentUpper ) => {
304
+ Err ( "Unknown format code 'E' for object of type 'int'" )
305
+ }
306
+ Some ( FormatType :: ExponentLower ) => {
307
+ Err ( "Unknown format code 'e' for object of type 'int'" )
308
+ }
309
+ Some ( FormatType :: FixedPointUpper ) => {
310
+ Err ( "Unknown format code 'F' for object of type 'int'" )
311
+ }
312
+ Some ( FormatType :: FixedPointLower ) => {
313
+ Err ( "Unknown format code 'f' for object of type 'int'" )
314
+ }
285
315
None => Ok ( magnitude. to_str_radix ( 10 ) ) ,
286
316
} ;
287
317
if !raw_magnitude_string_result. is_ok ( ) {
288
318
return raw_magnitude_string_result;
289
319
}
290
- let magnitude_string = format ! ( "{}{}" , prefix, self . add_magnitude_separators( raw_magnitude_string_result. unwrap( ) ) ) ;
320
+ let magnitude_string = format ! (
321
+ "{}{}" ,
322
+ prefix,
323
+ self . add_magnitude_separators( raw_magnitude_string_result. unwrap( ) )
324
+ ) ;
291
325
let align = self . align . unwrap_or ( FormatAlign :: Right ) ;
292
326
293
327
// Use the byte length as the string length since we're in ascii
@@ -300,21 +334,43 @@ impl FormatSpec {
300
334
FormatSign :: Plus => "+" ,
301
335
FormatSign :: Minus => "" ,
302
336
FormatSign :: MinusOrSpace => " " ,
303
- }
337
+ } ,
304
338
} ;
305
339
306
- let fill_chars_needed: i32 = self . width . map_or ( 0 , |w| cmp:: max ( 0 , ( w as i32 ) - ( num_chars as i32 ) - ( sign_str. len ( ) as i32 ) ) ) ;
340
+ let fill_chars_needed: i32 = self . width . map_or ( 0 , |w| {
341
+ cmp:: max ( 0 , ( w as i32 ) - ( num_chars as i32 ) - ( sign_str. len ( ) as i32 ) )
342
+ } ) ;
307
343
Ok ( match align {
308
- FormatAlign :: Left => format ! ( "{}{}{}" , sign_str, magnitude_string, FormatSpec :: compute_fill_string( fill_char, fill_chars_needed) ) ,
309
- FormatAlign :: Right => format ! ( "{}{}{}" , FormatSpec :: compute_fill_string( fill_char, fill_chars_needed) , sign_str, magnitude_string) ,
310
- FormatAlign :: AfterSign => format ! ( "{}{}{}" , sign_str, FormatSpec :: compute_fill_string( fill_char, fill_chars_needed) , magnitude_string) ,
344
+ FormatAlign :: Left => format ! (
345
+ "{}{}{}" ,
346
+ sign_str,
347
+ magnitude_string,
348
+ FormatSpec :: compute_fill_string( fill_char, fill_chars_needed)
349
+ ) ,
350
+ FormatAlign :: Right => format ! (
351
+ "{}{}{}" ,
352
+ FormatSpec :: compute_fill_string( fill_char, fill_chars_needed) ,
353
+ sign_str,
354
+ magnitude_string
355
+ ) ,
356
+ FormatAlign :: AfterSign => format ! (
357
+ "{}{}{}" ,
358
+ sign_str,
359
+ FormatSpec :: compute_fill_string( fill_char, fill_chars_needed) ,
360
+ magnitude_string
361
+ ) ,
311
362
FormatAlign :: Center => {
312
363
let left_fill_chars_needed = fill_chars_needed / 2 ;
313
364
let right_fill_chars_needed = fill_chars_needed - left_fill_chars_needed;
314
- let left_fill_string = FormatSpec :: compute_fill_string ( fill_char, left_fill_chars_needed) ;
315
- let right_fill_string = FormatSpec :: compute_fill_string ( fill_char, right_fill_chars_needed) ;
316
- format ! ( "{}{}{}{}" , left_fill_string, sign_str, magnitude_string, right_fill_string)
317
- } ,
365
+ let left_fill_string =
366
+ FormatSpec :: compute_fill_string ( fill_char, left_fill_chars_needed) ;
367
+ let right_fill_string =
368
+ FormatSpec :: compute_fill_string ( fill_char, right_fill_chars_needed) ;
369
+ format ! (
370
+ "{}{}{}{}" ,
371
+ left_fill_string, sign_str, magnitude_string, right_fill_string
372
+ )
373
+ }
318
374
} )
319
375
}
320
376
}
@@ -345,14 +401,14 @@ impl FormatPart {
345
401
pub fn is_auto ( & self ) -> bool {
346
402
match self {
347
403
FormatPart :: AutoSpec ( _) => true ,
348
- _ => false
404
+ _ => false ,
349
405
}
350
406
}
351
407
352
408
pub fn is_index ( & self ) -> bool {
353
409
match self {
354
410
FormatPart :: IndexSpec ( _, _) => true ,
355
- _ => false
411
+ _ => false ,
356
412
}
357
413
}
358
414
}
@@ -387,14 +443,14 @@ impl FormatString {
387
443
Ok ( ( next_char, remaining) ) => {
388
444
result_string. push ( next_char) ;
389
445
cur_text = remaining;
390
- } ,
446
+ }
391
447
Err ( err) => {
392
448
if result_string. len ( ) > 0 {
393
449
return Ok ( ( FormatPart :: Literal ( result_string. to_string ( ) ) , cur_text) ) ;
394
450
} else {
395
451
return Err ( err) ;
396
452
}
397
- } ,
453
+ }
398
454
}
399
455
}
400
456
Ok ( ( FormatPart :: Literal ( result_string) , "" ) )
@@ -413,7 +469,7 @@ impl FormatString {
413
469
414
470
if arg_part. len ( ) == 0 {
415
471
return Ok ( FormatPart :: AutoSpec ( format_spec) ) ;
416
- }
472
+ }
417
473
418
474
if let Ok ( index) = arg_part. parse :: < usize > ( ) {
419
475
Ok ( FormatPart :: IndexSpec ( index, format_spec) )
@@ -422,7 +478,6 @@ impl FormatString {
422
478
}
423
479
}
424
480
425
-
426
481
fn parse_spec ( text : & str ) -> Result < ( FormatPart , & str ) , FormatParseError > {
427
482
let mut chars = text. chars ( ) ;
428
483
if chars. next ( ) != Some ( '{' ) {
@@ -467,11 +522,26 @@ mod tests {
467
522
468
523
#[ test]
469
524
fn test_fill_and_align ( ) {
470
- assert_eq ! ( parse_fill_and_align( " <" ) , ( Some ( ' ' ) , Some ( FormatAlign :: Left ) , "" ) ) ;
471
- assert_eq ! ( parse_fill_and_align( " <22" ) , ( Some ( ' ' ) , Some ( FormatAlign :: Left ) , "22" ) ) ;
472
- assert_eq ! ( parse_fill_and_align( "<22" ) , ( None , Some ( FormatAlign :: Left ) , "22" ) ) ;
473
- assert_eq ! ( parse_fill_and_align( " ^^" ) , ( Some ( ' ' ) , Some ( FormatAlign :: Center ) , "^" ) ) ;
474
- assert_eq ! ( parse_fill_and_align( "===" ) , ( Some ( '=' ) , Some ( FormatAlign :: AfterSign ) , "=" ) ) ;
525
+ assert_eq ! (
526
+ parse_fill_and_align( " <" ) ,
527
+ ( Some ( ' ' ) , Some ( FormatAlign :: Left ) , "" )
528
+ ) ;
529
+ assert_eq ! (
530
+ parse_fill_and_align( " <22" ) ,
531
+ ( Some ( ' ' ) , Some ( FormatAlign :: Left ) , "22" )
532
+ ) ;
533
+ assert_eq ! (
534
+ parse_fill_and_align( "<22" ) ,
535
+ ( None , Some ( FormatAlign :: Left ) , "22" )
536
+ ) ;
537
+ assert_eq ! (
538
+ parse_fill_and_align( " ^^" ) ,
539
+ ( Some ( ' ' ) , Some ( FormatAlign :: Center ) , "^" )
540
+ ) ;
541
+ assert_eq ! (
542
+ parse_fill_and_align( "===" ) ,
543
+ ( Some ( '=' ) , Some ( FormatAlign :: AfterSign ) , "=" )
544
+ ) ;
475
545
}
476
546
477
547
#[ test]
@@ -521,42 +591,68 @@ mod tests {
521
591
522
592
#[ test]
523
593
fn test_format_int ( ) {
524
- assert_eq ! ( parse_format_spec( "d" ) . format_int( & BigInt :: from_bytes_be( Sign :: Plus , b"\x10 " ) ) , Ok ( "16" . to_string( ) ) ) ;
525
- assert_eq ! ( parse_format_spec( "x" ) . format_int( & BigInt :: from_bytes_be( Sign :: Plus , b"\x10 " ) ) , Ok ( "10" . to_string( ) ) ) ;
526
- assert_eq ! ( parse_format_spec( "b" ) . format_int( & BigInt :: from_bytes_be( Sign :: Plus , b"\x10 " ) ) , Ok ( "10000" . to_string( ) ) ) ;
527
- assert_eq ! ( parse_format_spec( "o" ) . format_int( & BigInt :: from_bytes_be( Sign :: Plus , b"\x10 " ) ) , Ok ( "20" . to_string( ) ) ) ;
528
- assert_eq ! ( parse_format_spec( "+d" ) . format_int( & BigInt :: from_bytes_be( Sign :: Plus , b"\x10 " ) ) , Ok ( "+16" . to_string( ) ) ) ;
529
- assert_eq ! ( parse_format_spec( "^ 5d" ) . format_int( & BigInt :: from_bytes_be( Sign :: Minus , b"\x10 " ) ) , Ok ( " -16 " . to_string( ) ) ) ;
530
- assert_eq ! ( parse_format_spec( "0>+#10x" ) . format_int( & BigInt :: from_bytes_be( Sign :: Plus , b"\x10 " ) ) , Ok ( "00000+0x10" . to_string( ) ) ) ;
594
+ assert_eq ! (
595
+ parse_format_spec( "d" ) . format_int( & BigInt :: from_bytes_be( Sign :: Plus , b"\x10 " ) ) ,
596
+ Ok ( "16" . to_string( ) )
597
+ ) ;
598
+ assert_eq ! (
599
+ parse_format_spec( "x" ) . format_int( & BigInt :: from_bytes_be( Sign :: Plus , b"\x10 " ) ) ,
600
+ Ok ( "10" . to_string( ) )
601
+ ) ;
602
+ assert_eq ! (
603
+ parse_format_spec( "b" ) . format_int( & BigInt :: from_bytes_be( Sign :: Plus , b"\x10 " ) ) ,
604
+ Ok ( "10000" . to_string( ) )
605
+ ) ;
606
+ assert_eq ! (
607
+ parse_format_spec( "o" ) . format_int( & BigInt :: from_bytes_be( Sign :: Plus , b"\x10 " ) ) ,
608
+ Ok ( "20" . to_string( ) )
609
+ ) ;
610
+ assert_eq ! (
611
+ parse_format_spec( "+d" ) . format_int( & BigInt :: from_bytes_be( Sign :: Plus , b"\x10 " ) ) ,
612
+ Ok ( "+16" . to_string( ) )
613
+ ) ;
614
+ assert_eq ! (
615
+ parse_format_spec( "^ 5d" ) . format_int( & BigInt :: from_bytes_be( Sign :: Minus , b"\x10 " ) ) ,
616
+ Ok ( " -16 " . to_string( ) )
617
+ ) ;
618
+ assert_eq ! (
619
+ parse_format_spec( "0>+#10x" ) . format_int( & BigInt :: from_bytes_be( Sign :: Plus , b"\x10 " ) ) ,
620
+ Ok ( "00000+0x10" . to_string( ) )
621
+ ) ;
531
622
}
532
623
533
624
#[ test]
534
625
fn test_format_parse ( ) {
535
626
let expected = Ok ( FormatString {
536
- format_parts : vec ! (
627
+ format_parts : vec ! [
537
628
FormatPart :: Literal ( "abcd" . to_string( ) ) ,
538
629
FormatPart :: IndexSpec ( 1 , String :: new( ) ) ,
539
630
FormatPart :: Literal ( ":" . to_string( ) ) ,
540
- FormatPart :: KeywordSpec ( "key" . to_string( ) , String :: new( ) ) )
631
+ FormatPart :: KeywordSpec ( "key" . to_string( ) , String :: new( ) ) ,
632
+ ] ,
541
633
} ) ;
542
634
543
635
assert_eq ! ( FormatString :: from_str( "abcd{1}:{key}" ) , expected) ;
544
636
}
545
637
546
638
#[ test]
547
639
fn test_format_parse_fail ( ) {
548
- assert_eq ! ( FormatString :: from_str( "{s" ) , Err ( FormatParseError :: UnmatchedBracket ) ) ;
640
+ assert_eq ! (
641
+ FormatString :: from_str( "{s" ) ,
642
+ Err ( FormatParseError :: UnmatchedBracket )
643
+ ) ;
549
644
}
550
645
551
646
#[ test]
552
647
fn test_format_parse_escape ( ) {
553
648
let expected = Ok ( FormatString {
554
- format_parts : vec ! (
649
+ format_parts : vec ! [
555
650
FormatPart :: Literal ( "{" . to_string( ) ) ,
556
651
FormatPart :: KeywordSpec ( "key" . to_string( ) , String :: new( ) ) ,
557
- FormatPart :: Literal ( "}ddfe" . to_string( ) ) )
652
+ FormatPart :: Literal ( "}ddfe" . to_string( ) ) ,
653
+ ] ,
558
654
} ) ;
559
655
560
656
assert_eq ! ( FormatString :: from_str( "{{{key}}}ddfe" ) , expected) ;
561
657
}
562
- }
658
+ }
0 commit comments