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,6 @@ 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 ( ) ;
241
245
if Self :: can_be_bare_key ( key. get ( ) ) {
242
246
// will remove decors and set the key to the bare key
243
247
key. fmt ( ) ;
@@ -246,7 +250,8 @@ impl VisitMut for KeyValue {
246
250
key. decor_mut ( ) . set_suffix ( " " ) ;
247
251
}
248
252
// start all key names at the start of a line, but preserve comments
249
- if let Some ( prefix) = prefix. as_str ( ) {
253
+ if let Some ( prefix) = key. decor ( ) . prefix ( ) . map ( raw_string_as_str) {
254
+ let prefix = prefix. to_string ( ) ;
250
255
key. decor_mut ( )
251
256
. set_prefix ( prefix. trim_end_matches ( |c : char | c. is_whitespace ( ) && c != '\n' ) ) ;
252
257
}
@@ -404,19 +409,14 @@ impl VisitMut for TrimSpaces {
404
409
self . visit_table_mut ( node) ;
405
410
406
411
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) ;
412
+ if let Some ( prefix) = decor. prefix ( ) . map ( raw_string_as_str) {
413
+ let prefix = format ! (
414
+ "{}{}" ,
415
+ if i == 0 { "" } else { "\n " } ,
416
+ Self :: trim_block( prefix)
417
+ ) ;
418
+ decor. set_prefix ( prefix) ;
419
+ }
420
420
} ;
421
421
let table = node. as_table_mut ( ) ;
422
422
for ( i, ( _, item) ) in table. iter_mut ( ) . enumerate ( ) {
@@ -429,9 +429,9 @@ impl VisitMut for TrimSpaces {
429
429
}
430
430
}
431
431
432
- let trailing = node. trailing ( ) . as_str ( ) . unwrap_or_default ( ) ;
432
+ let trailing = raw_string_as_str ( node. trailing ( ) ) ;
433
433
if !trailing. trim ( ) . is_empty ( ) {
434
- let trailing: String = Self :: trim_block ( trailing) ;
434
+ let trailing = Self :: trim_block ( trailing) ;
435
435
node. set_trailing ( & format ! ( "\n {trailing}" ) ) ;
436
436
} else {
437
437
node. set_trailing ( "" ) ;
@@ -440,33 +440,33 @@ impl VisitMut for TrimSpaces {
440
440
441
441
fn visit_table_mut ( & mut self , node : & mut Table ) {
442
442
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
- }
443
+ if let Some ( prefix) = decor. prefix ( ) . map ( raw_string_as_str) {
444
+ decor. set_prefix ( format ! ( "\n {}" , Self :: trim_block( prefix) ) ) ;
445
+ }
446
+ if let Some ( suffix) = decor. suffix ( ) . map ( raw_string_as_str) {
447
+ decor. set_suffix ( Self :: trim_suffix ( suffix) ) ;
447
448
}
448
- trim_suffix ( decor) ;
449
449
self . visit_table_like_mut ( node) ;
450
450
}
451
451
452
452
fn visit_table_like_kv_mut ( & mut self , mut key : KeyMut < ' _ > , value : & mut Item ) {
453
453
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
- }
454
+ if let Some ( prefix) = decor. prefix ( ) . map ( raw_string_as_str) {
455
+ decor. set_prefix ( format ! ( "{}" , Self :: trim_block( prefix) ) ) ;
458
456
}
457
+
459
458
if let Some ( value) = value. as_value_mut ( ) {
460
- trim_suffix ( value. decor_mut ( ) ) ;
459
+ let decor = value. decor_mut ( ) ;
460
+ if let Some ( suffix) = decor. suffix ( ) . map ( raw_string_as_str) {
461
+ decor. set_suffix ( Self :: trim_suffix ( suffix) ) ;
462
+ }
461
463
}
462
464
self . visit_item_mut ( value) ;
463
465
}
464
466
}
465
467
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
- }
468
+ /// Note: in `Document::from_str`, the document is despanned, so we can safely unwrap `as_str`
469
+ /// when handling `RawString`.
470
+ fn raw_string_as_str ( raw_string : & RawString ) -> & str {
471
+ raw_string. as_str ( ) . expect ( "should already be despanded" )
472
472
}
0 commit comments