@@ -119,8 +119,10 @@ where
119
119
/// Characterizes the outermost value. Drives the worklist algorithm.
120
120
fn node ( self , config : & Config ) -> Node < Self > ;
121
121
122
- #[ allow( clippy:: ptr_arg) ]
123
- fn from_bytes ( v : & Vec < u8 > , config : & Config ) -> Self ;
122
+ fn from_bytes < T > ( v : T , config : & Config ) -> Self
123
+ where
124
+ T : AsRef < [ u8 ] > ;
125
+ fn from_string ( v : String , config : & Config ) -> Self ;
124
126
fn from_list_dir ( files : Vec < Self > , config : & Config ) -> Self ;
125
127
fn from_named_dir ( files : HashMap < String , Self > , config : & Config ) -> Self ;
126
128
}
@@ -238,7 +240,15 @@ where
238
240
V : Nodelike ,
239
241
{
240
242
match & fs. get ( inum) . unwrap ( ) . entry {
241
- Entry :: File ( contents) => V :: from_bytes ( contents, & fs. config ) ,
243
+ Entry :: File ( contents) => match String :: from_utf8 ( contents. clone ( ) ) {
244
+ Ok ( mut contents) => {
245
+ if fs. config . add_newlines && contents. ends_with ( '\n' ) {
246
+ contents. truncate ( contents. len ( ) - 1 ) ;
247
+ }
248
+ V :: from_string ( contents, & fs. config )
249
+ }
250
+ Err ( _) => V :: from_bytes ( contents, & fs. config ) ,
251
+ } ,
242
252
Entry :: Directory ( DirType :: List , files) => {
243
253
let mut entries = Vec :: with_capacity ( files. len ( ) ) ;
244
254
@@ -294,25 +304,20 @@ mod json {
294
304
Value :: Bool ( b) => Node :: Bytes ( format ! ( "{}{}" , b, nl) . into_bytes ( ) ) ,
295
305
Value :: Number ( n) => Node :: Bytes ( format ! ( "{}{}" , n, nl) . into_bytes ( ) ) ,
296
306
Value :: String ( s) => {
297
- let contents = if s. ends_with ( '\n' ) { s } else { s + nl } ;
298
- Node :: Bytes ( contents. into_bytes ( ) )
307
+ if config. try_decode_base64 {
308
+ if let Ok ( bytes) = base64:: decode_config ( & s, config. base64 ) {
309
+ return Node :: Bytes ( bytes) ;
310
+ }
311
+ }
312
+
313
+ Node :: String ( if s. ends_with ( '\n' ) { s } else { s + nl } )
299
314
}
300
315
Value :: Array ( vs) => Node :: List ( vs) ,
301
316
Value :: Object ( fvs) => Node :: Map ( fvs. into_iter ( ) . collect ( ) ) ,
302
317
}
303
318
}
304
319
305
- fn from_bytes ( contents : & Vec < u8 > , config : & Config ) -> Self {
306
- let contents = match String :: from_utf8 ( contents. clone ( ) ) {
307
- Ok ( mut contents) => {
308
- if config. add_newlines && contents. ends_with ( '\n' ) {
309
- contents. truncate ( contents. len ( ) - 1 ) ;
310
- }
311
- contents
312
- }
313
- Err ( _) => unimplemented ! ( "binary data JSON serialization" ) ,
314
- } ;
315
-
320
+ fn from_string ( contents : String , _config : & Config ) -> Self {
316
321
if contents. is_empty ( ) {
317
322
Value :: Null
318
323
} else if contents == "true" {
@@ -326,6 +331,13 @@ mod json {
326
331
}
327
332
}
328
333
334
+ fn from_bytes < T > ( contents : T , config : & Config ) -> Self
335
+ where
336
+ T : AsRef < [ u8 ] > ,
337
+ {
338
+ Value :: String ( base64:: encode_config ( contents, config. base64 ) )
339
+ }
340
+
329
341
fn from_list_dir ( files : Vec < Self > , _config : & Config ) -> Self {
330
342
Value :: Array ( files)
331
343
}
@@ -340,7 +352,6 @@ mod toml {
340
352
use super :: * ;
341
353
342
354
use serde_toml:: Value ;
343
-
344
355
#[ derive( Debug ) ]
345
356
pub enum Error < E > {
346
357
Io ( std:: io:: Error ) ,
@@ -392,25 +403,20 @@ mod toml {
392
403
Value :: Float ( n) => Node :: Bytes ( format ! ( "{}{}" , n, nl) . into_bytes ( ) ) ,
393
404
Value :: Integer ( n) => Node :: Bytes ( format ! ( "{}{}" , n, nl) . into_bytes ( ) ) ,
394
405
Value :: String ( s) => {
395
- let contents = if s. ends_with ( '\n' ) { s } else { s + nl } ;
396
- Node :: Bytes ( contents. into_bytes ( ) )
406
+ if config. try_decode_base64 {
407
+ if let Ok ( bytes) = base64:: decode_config ( & s, config. base64 ) {
408
+ return Node :: Bytes ( bytes) ;
409
+ }
410
+ }
411
+
412
+ Node :: String ( if s. ends_with ( '\n' ) { s } else { s + nl } )
397
413
}
398
414
Value :: Array ( vs) => Node :: List ( vs) ,
399
415
Value :: Table ( fvs) => Node :: Map ( fvs. into_iter ( ) . collect ( ) ) ,
400
416
}
401
417
}
402
418
403
- fn from_bytes ( contents : & Vec < u8 > , config : & Config ) -> Self {
404
- let contents = match String :: from_utf8 ( contents. clone ( ) ) {
405
- Ok ( mut contents) => {
406
- if config. add_newlines && contents. ends_with ( '\n' ) {
407
- contents. truncate ( contents. len ( ) - 1 ) ;
408
- }
409
- contents
410
- }
411
- Err ( _) => unimplemented ! ( "binary data TOML serialization" ) ,
412
- } ;
413
-
419
+ fn from_string ( contents : String , _config : & Config ) -> Self {
414
420
if contents == "true" {
415
421
Value :: Boolean ( true )
416
422
} else if contents == "false" {
@@ -424,6 +430,13 @@ mod toml {
424
430
}
425
431
}
426
432
433
+ fn from_bytes < T > ( contents : T , config : & Config ) -> Self
434
+ where
435
+ T : AsRef < [ u8 ] > ,
436
+ {
437
+ Value :: String ( base64:: encode_config ( contents, config. base64 ) )
438
+ }
439
+
427
440
fn from_list_dir ( files : Vec < Self > , _config : & Config ) -> Self {
428
441
Value :: Array ( files)
429
442
}
0 commit comments