11use itertools:: Itertools ;
22use std:: cmp:: Ordering ;
33use 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 ,
56} ;
67
78use crate :: { Config , ErrorKind } ;
@@ -190,10 +191,12 @@ impl BlankLine {
190191 }
191192
192193 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+ }
197200 }
198201}
199202
@@ -221,8 +224,10 @@ impl VisitMut for BlankLine {
221224 } ) ;
222225 } else {
223226 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+ ) ) ;
226231 }
227232 }
228233}
@@ -237,7 +242,6 @@ impl KeyValue {
237242
238243impl VisitMut for KeyValue {
239244 fn visit_table_like_kv_mut ( & mut self , mut key : KeyMut < ' _ > , value : & mut Item ) {
240- let prefix = key. decor ( ) . prefix ( ) . cloned ( ) . unwrap_or_default ( ) ;
241245 if Self :: can_be_bare_key ( key. get ( ) ) {
242246 // will remove decors and set the key to the bare key
243247 key. fmt ( ) ;
@@ -246,7 +250,8 @@ impl VisitMut for KeyValue {
246250 key. decor_mut ( ) . set_suffix ( " " ) ;
247251 }
248252 // 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 ( ) ;
250255 key. decor_mut ( )
251256 . set_prefix ( prefix. trim_end_matches ( |c : char | c. is_whitespace ( ) && c != '\n' ) ) ;
252257 }
@@ -404,19 +409,14 @@ impl VisitMut for TrimSpaces {
404409 self . visit_table_mut ( node) ;
405410
406411 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+ }
420420 } ;
421421 let table = node. as_table_mut ( ) ;
422422 for ( i, ( _, item) ) in table. iter_mut ( ) . enumerate ( ) {
@@ -429,9 +429,9 @@ impl VisitMut for TrimSpaces {
429429 }
430430 }
431431
432- let trailing = node. trailing ( ) . as_str ( ) . unwrap_or_default ( ) ;
432+ let trailing = raw_string_as_str ( node. trailing ( ) ) ;
433433 if !trailing. trim ( ) . is_empty ( ) {
434- let trailing: String = Self :: trim_block ( trailing) ;
434+ let trailing = Self :: trim_block ( trailing) ;
435435 node. set_trailing ( & format ! ( "\n {trailing}" ) ) ;
436436 } else {
437437 node. set_trailing ( "" ) ;
@@ -440,33 +440,33 @@ impl VisitMut for TrimSpaces {
440440
441441 fn visit_table_mut ( & mut self , node : & mut Table ) {
442442 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) ) ;
447448 }
448- trim_suffix ( decor) ;
449449 self . visit_table_like_mut ( node) ;
450450 }
451451
452452 fn visit_table_like_kv_mut ( & mut self , mut key : KeyMut < ' _ > , value : & mut Item ) {
453453 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) ) ) ;
458456 }
457+
459458 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+ }
461463 }
462464 self . visit_item_mut ( value) ;
463465 }
464466}
465467
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" )
472472}
0 commit comments