@@ -10,7 +10,7 @@ use crate::{Config, ErrorKind};
10
10
///
11
11
/// [the Style Guide]: https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/cargo.md
12
12
pub ( crate ) fn format_cargo_toml_inner ( content : & str , config : & Config ) -> Result < String , ErrorKind > {
13
- let mut doc = content. parse :: < toml_edit :: Document > ( ) ?;
13
+ let mut doc = content. parse :: < Document > ( ) ?;
14
14
let rules = [
15
15
& mut SortSection {
16
16
current_position : 0 ,
@@ -31,7 +31,7 @@ pub(crate) fn format_cargo_toml_inner(content: &str, config: &Config) -> Result<
31
31
for rule in rules. into_iter ( ) {
32
32
rule. visit_document_mut ( & mut doc) ;
33
33
}
34
- // Special handling for falliable rules.
34
+ // Special handling for fallible rules.
35
35
let mut rule = SortKey { error : None } ;
36
36
rule. visit_document_mut ( & mut doc) ;
37
37
if let Some ( e) = rule. error {
@@ -190,10 +190,10 @@ impl BlankLine {
190
190
}
191
191
192
192
fn trim_decor_blank_lines ( decor : & mut Decor ) {
193
- let prefix = decor. prefix ( ) . unwrap_or ( "" ) . to_owned ( ) ;
194
- let suffix = decor. suffix ( ) . unwrap_or ( "" ) . to_owned ( ) ;
195
- decor. set_prefix ( Self :: trim_blank_lines ( prefix. as_str ( ) ) ) ;
196
- decor. set_suffix ( Self :: trim_blank_lines ( suffix. as_str ( ) ) ) ;
193
+ let prefix = decor. prefix ( ) . cloned ( ) . unwrap_or_default ( ) ;
194
+ let suffix = decor. suffix ( ) . cloned ( ) . unwrap_or_default ( ) ;
195
+ decor. set_prefix ( Self :: trim_blank_lines ( prefix. as_str ( ) . unwrap_or_default ( ) ) ) ;
196
+ decor. set_suffix ( Self :: trim_blank_lines ( suffix. as_str ( ) . unwrap_or_default ( ) ) ) ;
197
197
}
198
198
}
199
199
@@ -221,8 +221,8 @@ impl VisitMut for BlankLine {
221
221
} ) ;
222
222
} else {
223
223
let decor = table. decor_mut ( ) ;
224
- let prefix = decor. prefix ( ) . unwrap_or ( "" ) . to_owned ( ) ;
225
- decor. set_prefix ( "\n " . to_owned ( ) + & prefix ) ;
224
+ let prefix = decor. prefix ( ) . cloned ( ) . unwrap_or_default ( ) ;
225
+ decor. set_prefix ( format ! ( "\n {}" , prefix . as_str ( ) . unwrap_or_default ( ) ) ) ;
226
226
}
227
227
}
228
228
}
@@ -237,7 +237,7 @@ impl KeyValue {
237
237
238
238
impl VisitMut for KeyValue {
239
239
fn visit_table_like_kv_mut ( & mut self , mut key : KeyMut < ' _ > , value : & mut Item ) {
240
- let prefix = key. decor ( ) . prefix ( ) . unwrap_or ( "" ) . to_owned ( ) ;
240
+ let prefix = key. decor ( ) . prefix ( ) . cloned ( ) . unwrap_or_default ( ) ;
241
241
if Self :: can_be_bare_key ( key. get ( ) ) {
242
242
// will remove decors and set the key to the bare key
243
243
key. fmt ( ) ;
@@ -246,8 +246,10 @@ impl VisitMut for KeyValue {
246
246
key. decor_mut ( ) . set_suffix ( " " ) ;
247
247
}
248
248
// start all key names at the start of a line, but preserve comments
249
- key. decor_mut ( )
250
- . set_prefix ( prefix. trim_end_matches ( |c : char | c. is_whitespace ( ) && c != '\n' ) ) ;
249
+ if let Some ( prefix) = prefix. as_str ( ) {
250
+ key. decor_mut ( )
251
+ . set_prefix ( prefix. trim_end_matches ( |c : char | c. is_whitespace ( ) && c != '\n' ) ) ;
252
+ }
251
253
252
254
if let Some ( v) = value. as_value_mut ( ) {
253
255
v. decor_mut ( ) . set_prefix ( " " ) ;
@@ -405,7 +407,14 @@ impl VisitMut for TrimSpaces {
405
407
let prefix = format ! (
406
408
"{}{}" ,
407
409
if i == 0 { "" } else { "\n " } ,
408
- Self :: trim_block( decor. prefix( ) . unwrap_or_default( ) )
410
+ Self :: trim_block(
411
+ decor
412
+ . prefix( )
413
+ . cloned( )
414
+ . unwrap_or_default( )
415
+ . as_str( )
416
+ . unwrap_or_default( )
417
+ )
409
418
) ;
410
419
decor. set_prefix ( prefix) ;
411
420
} ;
@@ -420,8 +429,9 @@ impl VisitMut for TrimSpaces {
420
429
}
421
430
}
422
431
423
- if !node. trailing ( ) . trim ( ) . is_empty ( ) {
424
- let trailing: String = Self :: trim_block ( node. trailing ( ) ) ;
432
+ let trailing = node. trailing ( ) . as_str ( ) . unwrap_or_default ( ) ;
433
+ if !trailing. trim ( ) . is_empty ( ) {
434
+ let trailing: String = Self :: trim_block ( trailing) ;
425
435
node. set_trailing ( & format ! ( "\n {trailing}" ) ) ;
426
436
} else {
427
437
node. set_trailing ( "" ) ;
@@ -431,28 +441,32 @@ impl VisitMut for TrimSpaces {
431
441
fn visit_table_mut ( & mut self , node : & mut Table ) {
432
442
let decor = node. decor_mut ( ) ;
433
443
if let Some ( prefix) = decor. prefix ( ) {
434
- decor. set_prefix ( format ! ( "\n {}" , Self :: trim_block( prefix) ) ) ;
435
- }
436
- if let Some ( suffix) = decor. suffix ( ) {
437
- decor. set_suffix ( Self :: trim_suffix ( suffix) ) ;
444
+ if let Some ( prefix) = prefix. as_str ( ) {
445
+ decor. set_prefix ( format ! ( "\n {}" , Self :: trim_block( prefix) ) ) ;
446
+ }
438
447
}
439
-
448
+ trim_suffix ( decor ) ;
440
449
self . visit_table_like_mut ( node) ;
441
450
}
442
451
443
452
fn visit_table_like_kv_mut ( & mut self , mut key : KeyMut < ' _ > , value : & mut Item ) {
444
453
let decor = key. decor_mut ( ) ;
445
454
if let Some ( prefix) = decor. prefix ( ) {
446
- decor. set_prefix ( format ! ( "{}" , Self :: trim_block( prefix) ) ) ;
455
+ if let Some ( prefix) = prefix. as_str ( ) {
456
+ decor. set_prefix ( format ! ( "{}" , Self :: trim_block( prefix) ) ) ;
457
+ }
447
458
}
448
-
449
459
if let Some ( value) = value. as_value_mut ( ) {
450
- let decor = value. decor_mut ( ) ;
451
- if let Some ( suffix) = decor. suffix ( ) {
452
- decor. set_suffix ( Self :: trim_suffix ( suffix) ) ;
453
- }
460
+ trim_suffix ( value. decor_mut ( ) ) ;
454
461
}
455
-
456
462
self . visit_item_mut ( value) ;
457
463
}
458
464
}
465
+
466
+ fn trim_suffix ( decor : & mut Decor ) {
467
+ if let Some ( suffix) = decor. suffix ( ) {
468
+ if let Some ( suffix) = suffix. as_str ( ) {
469
+ decor. set_suffix ( TrimSpaces :: trim_suffix ( suffix) ) ;
470
+ }
471
+ }
472
+ }
0 commit comments