@@ -165,13 +165,76 @@ impl KdlEntry {
165165 pub fn autoformat ( & mut self ) {
166166 // TODO once MSRV allows:
167167 //self.format.take_if(|f| !f.autoformat_keep);
168+ let value_repr = self . format . as_ref ( ) . map ( |x| {
169+ match & self . value {
170+ KdlValue :: String ( val) => {
171+ // cleanup. I don't _think_ this should have any whitespace,
172+ // but just in case.
173+ let s = x. value_repr . trim ( ) ;
174+ // convert raw strings to new format
175+ let s = s. strip_prefix ( "r" ) . unwrap_or ( s) ;
176+ let s = if crate :: value:: is_plain_ident ( val) {
177+ val. to_string ( )
178+ } else if s
179+ . find ( |c| v2_parser:: NEWLINES . iter ( ) . any ( |nl| nl. contains ( c) ) )
180+ . is_some ( )
181+ {
182+ // Multiline string. Need triple quotes if they're not there already.
183+ if s. contains ( "\" \" \" " ) {
184+ // We're probably good. This could be more precise, but close enough.
185+ s. to_string ( )
186+ } else {
187+ // `"` -> `"""` but also extra newlines need to be
188+ // added because v2 strips the first and last ones.
189+ let s = s. replacen ( "\" " , "\" \" \" \n " , 1 ) ;
190+ s. chars ( )
191+ . rev ( )
192+ . collect :: < String > ( )
193+ . replacen ( "\" " , "\" \" \" \n " , 1 )
194+ . chars ( )
195+ . rev ( )
196+ . collect :: < String > ( )
197+ }
198+ } else if !s. starts_with ( "#" ) {
199+ // `/` is no longer an escaped char in v2.
200+ s. replace ( "\\ /" , "/" )
201+ } else {
202+ // We're all good! Let's move on.
203+ s. to_string ( )
204+ } ;
205+ s
206+ }
207+ // These have `#` prefixes now. The regular Display impl will
208+ // take care of that.
209+ KdlValue :: Bool ( _) | KdlValue :: Null => format ! ( "{}" , self . value) ,
210+ // These should be fine as-is?
211+ KdlValue :: Integer ( _) | KdlValue :: Float ( _) => x. value_repr . clone ( ) ,
212+ }
213+ } ) ;
214+
168215 if !self
169216 . format
170217 . as_ref ( )
171218 . map ( |f| f. autoformat_keep )
172219 . unwrap_or ( false )
173220 {
174- self . format = None
221+ self . format = None ;
222+ }
223+
224+ if let Some ( value_repr) = value_repr. as_ref ( ) {
225+ self . format = Some (
226+ self . format
227+ . clone ( )
228+ . map ( |mut x| {
229+ x. value_repr = value_repr. into ( ) ;
230+ x
231+ } )
232+ . unwrap_or_else ( || KdlEntryFormat {
233+ value_repr : value_repr. into ( ) ,
234+ leading : " " . into ( ) ,
235+ ..Default :: default ( )
236+ } ) ,
237+ )
175238 }
176239
177240 if let Some ( name) = & mut self . name {
@@ -455,4 +518,49 @@ mod test {
455518 let entry = KdlEntry :: new_prop ( "name" , KdlValue :: Integer ( 42 ) ) ;
456519 assert_eq ! ( format!( "{}" , entry) , "name=42" ) ;
457520 }
521+
522+ #[ cfg( feature = "v1" ) ]
523+ #[ test]
524+ fn v1_to_v2_format ( ) -> miette:: Result < ( ) > {
525+ let mut entry = KdlEntry :: parse_v1 ( r##"r#"hello, world!"#"## ) ?;
526+ entry. autoformat ( ) ;
527+ assert_eq ! ( format!( "{}" , entry) , r##" #"hello, world!"#"## ) ;
528+
529+ let mut entry = KdlEntry :: parse_v1 ( r#""hello, \" world!""# ) ?;
530+ entry. autoformat ( ) ;
531+ assert_eq ! ( format!( "{}" , entry) , r#" "hello, \" world!""# ) ;
532+
533+ let mut entry = KdlEntry :: parse_v1 ( "\" foo!`~.,<>\" " ) ?;
534+ entry. autoformat ( ) ;
535+ assert_eq ! ( format!( "{}" , entry) , " foo!`~.,<>" ) ;
536+
537+ let mut entry = KdlEntry :: parse_v1 ( "\" \n hello, world!\" " ) ?;
538+ entry. autoformat ( ) ;
539+ assert_eq ! ( format!( "{}" , entry) , " \" \" \" \n \n hello, world!\n \" \" \" " ) ;
540+
541+ let mut entry = KdlEntry :: parse_v1 ( "r#\" \n hello, world!\" #" ) ?;
542+ entry. autoformat ( ) ;
543+ assert_eq ! ( format!( "{}" , entry) , " #\" \" \" \n \n hello, world!\n \" \" \" #" ) ;
544+
545+ let mut entry = KdlEntry :: parse_v1 ( "true" ) ?;
546+ entry. autoformat ( ) ;
547+ assert_eq ! ( format!( "{}" , entry) , " #true" ) ;
548+
549+ let mut entry = KdlEntry :: parse_v1 ( "false" ) ?;
550+ entry. autoformat ( ) ;
551+ assert_eq ! ( format!( "{}" , entry) , " #false" ) ;
552+
553+ let mut entry = KdlEntry :: parse_v1 ( "null" ) ?;
554+ entry. autoformat ( ) ;
555+ assert_eq ! ( format!( "{}" , entry) , " #null" ) ;
556+
557+ let mut entry = KdlEntry :: parse_v1 ( "1_234_567" ) ?;
558+ entry. autoformat ( ) ;
559+ assert_eq ! ( format!( "{}" , entry) , " 1_234_567" ) ;
560+
561+ let mut entry = KdlEntry :: parse_v1 ( "1_234_567E-10" ) ?;
562+ entry. autoformat ( ) ;
563+ assert_eq ! ( format!( "{}" , entry) , " 1_234_567E-10" ) ;
564+ Ok ( ( ) )
565+ }
458566}
0 commit comments