1
1
use itertools:: Itertools ;
2
2
use std:: cmp:: Ordering ;
3
3
use toml_edit:: {
4
- visit_mut:: * , Decor , Document , Formatted , Item , KeyMut , Table , TableLike , TomlError , Value ,
4
+ visit_mut:: * , Decor , Document , Formatted , Item , KeyMut , RawString , Table , TableLike , TomlError ,
5
+ Value ,
5
6
} ;
6
7
7
8
use crate :: { Config , ErrorKind } ;
@@ -190,10 +191,12 @@ impl BlankLine {
190
191
}
191
192
192
193
fn trim_decor_blank_lines ( decor : & mut Decor ) {
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 ( ) ) ) ;
194
+ if let Some ( prefix) = decor. prefix ( ) . map ( raw_string_as_str) {
195
+ decor. set_prefix ( Self :: trim_blank_lines ( prefix) ) ;
196
+ }
197
+ if let Some ( suffix) = decor. suffix ( ) . map ( raw_string_as_str) {
198
+ decor. set_suffix ( Self :: trim_blank_lines ( suffix) ) ;
199
+ }
197
200
}
198
201
}
199
202
@@ -221,8 +224,10 @@ impl VisitMut for BlankLine {
221
224
} ) ;
222
225
} else {
223
226
let decor = table. decor_mut ( ) ;
224
- let prefix = decor. prefix ( ) . cloned ( ) . unwrap_or_default ( ) ;
225
- decor. set_prefix ( format ! ( "\n {}" , prefix. as_str( ) . unwrap_or_default( ) ) ) ;
227
+ decor. set_prefix ( format ! (
228
+ "\n {}" ,
229
+ decor. prefix( ) . map( raw_string_as_str) . unwrap_or_default( )
230
+ ) ) ;
226
231
}
227
232
}
228
233
}
@@ -237,7 +242,11 @@ impl KeyValue {
237
242
238
243
impl VisitMut for KeyValue {
239
244
fn visit_table_like_kv_mut ( & mut self , mut key : KeyMut < ' _ > , value : & mut Item ) {
240
- let prefix = key. decor ( ) . prefix ( ) . cloned ( ) . unwrap_or_default ( ) ;
245
+ let original_prefix = key
246
+ . decor ( )
247
+ . prefix ( )
248
+ . map ( raw_string_as_str)
249
+ . map ( String :: from) ;
241
250
if Self :: can_be_bare_key ( key. get ( ) ) {
242
251
// will remove decors and set the key to the bare key
243
252
key. fmt ( ) ;
@@ -246,7 +255,7 @@ impl VisitMut for KeyValue {
246
255
key. decor_mut ( ) . set_suffix ( " " ) ;
247
256
}
248
257
// start all key names at the start of a line, but preserve comments
249
- if let Some ( prefix) = prefix . as_str ( ) {
258
+ if let Some ( prefix) = original_prefix {
250
259
key. decor_mut ( )
251
260
. set_prefix ( prefix. trim_end_matches ( |c : char | c. is_whitespace ( ) && c != '\n' ) ) ;
252
261
}
@@ -404,19 +413,14 @@ impl VisitMut for TrimSpaces {
404
413
self . visit_table_mut ( node) ;
405
414
406
415
let set_prefix = |decor : & mut Decor , i : usize | {
407
- let prefix = format ! (
408
- "{}{}" ,
409
- if i == 0 { "" } else { "\n " } ,
410
- Self :: trim_block(
411
- decor
412
- . prefix( )
413
- . cloned( )
414
- . unwrap_or_default( )
415
- . as_str( )
416
- . unwrap_or_default( )
417
- )
418
- ) ;
419
- decor. set_prefix ( prefix) ;
416
+ if let Some ( prefix) = decor. prefix ( ) . map ( raw_string_as_str) {
417
+ let prefix = format ! (
418
+ "{}{}" ,
419
+ if i == 0 { "" } else { "\n " } ,
420
+ Self :: trim_block( prefix)
421
+ ) ;
422
+ decor. set_prefix ( prefix) ;
423
+ }
420
424
} ;
421
425
let table = node. as_table_mut ( ) ;
422
426
for ( i, ( _, item) ) in table. iter_mut ( ) . enumerate ( ) {
@@ -429,9 +433,9 @@ impl VisitMut for TrimSpaces {
429
433
}
430
434
}
431
435
432
- let trailing = node. trailing ( ) . as_str ( ) . unwrap_or_default ( ) ;
436
+ let trailing = raw_string_as_str ( node. trailing ( ) ) ;
433
437
if !trailing. trim ( ) . is_empty ( ) {
434
- let trailing: String = Self :: trim_block ( trailing) ;
438
+ let trailing = Self :: trim_block ( trailing) ;
435
439
node. set_trailing ( & format ! ( "\n {trailing}" ) ) ;
436
440
} else {
437
441
node. set_trailing ( "" ) ;
@@ -440,33 +444,33 @@ impl VisitMut for TrimSpaces {
440
444
441
445
fn visit_table_mut ( & mut self , node : & mut Table ) {
442
446
let decor = node. decor_mut ( ) ;
443
- if let Some ( prefix) = decor. prefix ( ) {
444
- if let Some ( prefix) = prefix. as_str ( ) {
445
- decor. set_prefix ( format ! ( "\n {}" , Self :: trim_block( prefix) ) ) ;
446
- }
447
+ if let Some ( prefix) = decor. prefix ( ) . map ( raw_string_as_str) {
448
+ decor. set_prefix ( format ! ( "\n {}" , Self :: trim_block( prefix) ) ) ;
449
+ }
450
+ if let Some ( suffix) = decor. suffix ( ) . map ( raw_string_as_str) {
451
+ decor. set_suffix ( Self :: trim_suffix ( suffix) ) ;
447
452
}
448
- trim_suffix ( decor) ;
449
453
self . visit_table_like_mut ( node) ;
450
454
}
451
455
452
456
fn visit_table_like_kv_mut ( & mut self , mut key : KeyMut < ' _ > , value : & mut Item ) {
453
457
let decor = key. decor_mut ( ) ;
454
- if let Some ( prefix) = decor. prefix ( ) {
455
- if let Some ( prefix) = prefix. as_str ( ) {
456
- decor. set_prefix ( format ! ( "{}" , Self :: trim_block( prefix) ) ) ;
457
- }
458
+ if let Some ( prefix) = decor. prefix ( ) . map ( raw_string_as_str) {
459
+ decor. set_prefix ( format ! ( "{}" , Self :: trim_block( prefix) ) ) ;
458
460
}
461
+
459
462
if let Some ( value) = value. as_value_mut ( ) {
460
- trim_suffix ( value. decor_mut ( ) ) ;
463
+ let decor = value. decor_mut ( ) ;
464
+ if let Some ( suffix) = decor. suffix ( ) . map ( raw_string_as_str) {
465
+ decor. set_suffix ( Self :: trim_suffix ( suffix) ) ;
466
+ }
461
467
}
462
468
self . visit_item_mut ( value) ;
463
469
}
464
470
}
465
471
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
+ /// Note: in `Document::from_str`, the document is despanned, so we can safely unwrap `as_str`
473
+ /// when handling `RawString`.
474
+ fn raw_string_as_str ( raw_string : & RawString ) -> & str {
475
+ raw_string. as_str ( ) . expect ( "should already be despanded" )
472
476
}
0 commit comments