@@ -431,7 +431,21 @@ impl<'de> de::Deserialize<'de> for CompressionLevel {
431
431
where
432
432
E : de:: Error ,
433
433
{
434
- Ok ( CompressionLevel :: Val ( v as u32 ) )
434
+ u32:: try_from ( v) . map ( CompressionLevel :: Val ) . map_err ( |err| {
435
+ de:: Error :: custom ( format ! (
436
+ "unsigned integer could not be converted to u32: {}" ,
437
+ err
438
+ ) )
439
+ } )
440
+ }
441
+
442
+ fn visit_i64 < E > ( self , v : i64 ) -> Result < Self :: Value , E >
443
+ where
444
+ E : de:: Error ,
445
+ {
446
+ u32:: try_from ( v) . map ( CompressionLevel :: Val ) . map_err ( |err| {
447
+ de:: Error :: custom ( format ! ( "integer could not be converted to u32: {}" , err) )
448
+ } )
435
449
}
436
450
}
437
451
@@ -490,7 +504,7 @@ mod test {
490
504
use super :: { Compression , CompressionLevel } ;
491
505
492
506
#[ test]
493
- fn deserialization ( ) {
507
+ fn deserialization_json ( ) {
494
508
let fixtures_valid = [
495
509
( r#""none""# , Compression :: None ) ,
496
510
( r#""gzip""# , Compression :: Gzip ( CompressionLevel :: default ( ) ) ) ,
@@ -545,7 +559,7 @@ mod test {
545
559
) ,
546
560
(
547
561
r#"{"algorithm": "gzip", "level": -1}"# ,
548
- r#"invalid type: integer `-1`, expected unsigned number or string at line 1 column 33"# ,
562
+ r#"integer could not be converted to u32: out of range integral type conversion attempted at line 1 column 33"# ,
549
563
) ,
550
564
(
551
565
r#"{"algorithm": "gzip", "level": "good"}"# ,
@@ -575,6 +589,22 @@ mod test {
575
589
}
576
590
}
577
591
592
+ #[ test]
593
+ fn deserialization_toml ( ) {
594
+ let fixtures_valid = [
595
+ // TOML differs from YAML and JSON by always parsing integers as signed
596
+ (
597
+ r#"algorithm = "gzip"
598
+ level = 8"# ,
599
+ Compression :: Gzip ( CompressionLevel :: Val ( 8 ) ) ,
600
+ ) ,
601
+ ] ;
602
+ for ( sources, result) in fixtures_valid. iter ( ) {
603
+ let deserialized: Result < Compression , _ > = toml:: from_str ( sources) ;
604
+ assert_eq ! ( deserialized. expect( "valid source" ) , * result) ;
605
+ }
606
+ }
607
+
578
608
#[ test]
579
609
fn from_and_to_value ( ) {
580
610
let fixtures_valid = [
0 commit comments