@@ -1536,36 +1536,20 @@ impl Serialize for CacherRequest {
1536
1536
S : Serializer ,
1537
1537
{
1538
1538
match self {
1539
- CacherRequest :: GetManifest => {
1540
- let mut map = serializer. serialize_map ( Some ( 1 ) ) ?;
1541
- map. serialize_entry ( "GetManifest" , & ( ) ) ?;
1542
- map. end ( )
1543
- }
1539
+ CacherRequest :: GetManifest => serializer. serialize_str ( "GetManifest" ) ,
1544
1540
CacherRequest :: GetLogCacheContent ( path) => {
1545
1541
let mut map = serializer. serialize_map ( Some ( 1 ) ) ?;
1546
1542
map. serialize_entry ( "GetLogCacheContent" , path) ?;
1547
1543
map. end ( )
1548
1544
}
1549
- CacherRequest :: GetStatus => {
1550
- let mut map = serializer. serialize_map ( Some ( 1 ) ) ?;
1551
- map. serialize_entry ( "GetStatus" , & ( ) ) ?;
1552
- map. end ( )
1553
- }
1545
+ CacherRequest :: GetStatus => serializer. serialize_str ( "GetStatus" ) ,
1554
1546
CacherRequest :: GetLogsByRange ( request) => {
1555
1547
let mut map = serializer. serialize_map ( Some ( 1 ) ) ?;
1556
1548
map. serialize_entry ( "GetLogsByRange" , request) ?;
1557
1549
map. end ( )
1558
1550
}
1559
- CacherRequest :: StartProviding => {
1560
- let mut map = serializer. serialize_map ( Some ( 1 ) ) ?;
1561
- map. serialize_entry ( "StartProviding" , & ( ) ) ?;
1562
- map. end ( )
1563
- }
1564
- CacherRequest :: StopProviding => {
1565
- let mut map = serializer. serialize_map ( Some ( 1 ) ) ?;
1566
- map. serialize_entry ( "StopProviding" , & ( ) ) ?;
1567
- map. end ( )
1568
- }
1551
+ CacherRequest :: StartProviding => serializer. serialize_str ( "StartProviding" ) ,
1552
+ CacherRequest :: StopProviding => serializer. serialize_str ( "StopProviding" ) ,
1569
1553
CacherRequest :: SetNodes ( nodes) => {
1570
1554
let mut map = serializer. serialize_map ( Some ( 1 ) ) ?;
1571
1555
map. serialize_entry ( "SetNodes" , nodes) ?;
@@ -1591,8 +1575,32 @@ impl<'de> Deserialize<'de> for CacherRequest {
1591
1575
type Value = CacherRequest ;
1592
1576
1593
1577
fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
1594
- formatter
1595
- . write_str ( "a map with a single key representing the CacherRequest variant" )
1578
+ formatter. write_str ( "a string for unit variants or a map for other variants" )
1579
+ }
1580
+
1581
+ fn visit_str < E > ( self , value : & str ) -> Result < Self :: Value , E >
1582
+ where
1583
+ E : de:: Error ,
1584
+ {
1585
+ match value {
1586
+ "GetManifest" => Ok ( CacherRequest :: GetManifest ) ,
1587
+ "GetStatus" => Ok ( CacherRequest :: GetStatus ) ,
1588
+ "StartProviding" => Ok ( CacherRequest :: StartProviding ) ,
1589
+ "StopProviding" => Ok ( CacherRequest :: StopProviding ) ,
1590
+ _ => Err ( de:: Error :: unknown_variant (
1591
+ value,
1592
+ & [
1593
+ "GetManifest" ,
1594
+ "GetLogCacheContent" ,
1595
+ "GetStatus" ,
1596
+ "GetLogsByRange" ,
1597
+ "StartProviding" ,
1598
+ "StopProviding" ,
1599
+ "SetNodes" ,
1600
+ "Reset" ,
1601
+ ] ,
1602
+ ) ) ,
1603
+ }
1596
1604
}
1597
1605
1598
1606
fn visit_map < A > ( self , mut map : A ) -> Result < Self :: Value , A :: Error >
@@ -1603,19 +1611,20 @@ impl<'de> Deserialize<'de> for CacherRequest {
1603
1611
. next_entry :: < String , serde_json:: Value > ( ) ?
1604
1612
. ok_or_else ( || de:: Error :: invalid_length ( 0 , & self ) ) ?;
1605
1613
1614
+ // Ensure there are no extra entries
1615
+ if map. next_entry :: < String , serde_json:: Value > ( ) ?. is_some ( ) {
1616
+ return Err ( de:: Error :: custom ( "unexpected extra entries in map" ) ) ;
1617
+ }
1618
+
1606
1619
match variant. as_str ( ) {
1607
- "GetManifest" => Ok ( CacherRequest :: GetManifest ) ,
1608
1620
"GetLogCacheContent" => {
1609
1621
let path = serde_json:: from_value ( value) . map_err ( de:: Error :: custom) ?;
1610
1622
Ok ( CacherRequest :: GetLogCacheContent ( path) )
1611
1623
}
1612
- "GetStatus" => Ok ( CacherRequest :: GetStatus ) ,
1613
1624
"GetLogsByRange" => {
1614
1625
let request = serde_json:: from_value ( value) . map_err ( de:: Error :: custom) ?;
1615
1626
Ok ( CacherRequest :: GetLogsByRange ( request) )
1616
1627
}
1617
- "StartProviding" => Ok ( CacherRequest :: StartProviding ) ,
1618
- "StopProviding" => Ok ( CacherRequest :: StopProviding ) ,
1619
1628
"SetNodes" => {
1620
1629
let nodes = serde_json:: from_value ( value) . map_err ( de:: Error :: custom) ?;
1621
1630
Ok ( CacherRequest :: SetNodes ( nodes) )
@@ -1641,7 +1650,7 @@ impl<'de> Deserialize<'de> for CacherRequest {
1641
1650
}
1642
1651
}
1643
1652
1644
- deserializer. deserialize_map ( CacherRequestVisitor )
1653
+ deserializer. deserialize_any ( CacherRequestVisitor )
1645
1654
}
1646
1655
}
1647
1656
@@ -1681,16 +1690,8 @@ impl Serialize for CacherResponse {
1681
1690
map. serialize_entry ( "StopProviding" , result) ?;
1682
1691
map. end ( )
1683
1692
}
1684
- CacherResponse :: Rejected => {
1685
- let mut map = serializer. serialize_map ( Some ( 1 ) ) ?;
1686
- map. serialize_entry ( "Rejected" , & ( ) ) ?;
1687
- map. end ( )
1688
- }
1689
- CacherResponse :: IsStarting => {
1690
- let mut map = serializer. serialize_map ( Some ( 1 ) ) ?;
1691
- map. serialize_entry ( "IsStarting" , & ( ) ) ?;
1692
- map. end ( )
1693
- }
1693
+ CacherResponse :: Rejected => serializer. serialize_str ( "Rejected" ) ,
1694
+ CacherResponse :: IsStarting => serializer. serialize_str ( "IsStarting" ) ,
1694
1695
CacherResponse :: SetNodes ( result) => {
1695
1696
let mut map = serializer. serialize_map ( Some ( 1 ) ) ?;
1696
1697
map. serialize_entry ( "SetNodes" , result) ?;
@@ -1716,8 +1717,32 @@ impl<'de> Deserialize<'de> for CacherResponse {
1716
1717
type Value = CacherResponse ;
1717
1718
1718
1719
fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
1719
- formatter
1720
- . write_str ( "a map with a single key representing the CacherResponse variant" )
1720
+ formatter. write_str ( "a string for unit variants or a map for other variants" )
1721
+ }
1722
+
1723
+ fn visit_str < E > ( self , value : & str ) -> Result < Self :: Value , E >
1724
+ where
1725
+ E : de:: Error ,
1726
+ {
1727
+ match value {
1728
+ "Rejected" => Ok ( CacherResponse :: Rejected ) ,
1729
+ "IsStarting" => Ok ( CacherResponse :: IsStarting ) ,
1730
+ _ => Err ( de:: Error :: unknown_variant (
1731
+ value,
1732
+ & [
1733
+ "GetManifest" ,
1734
+ "GetLogCacheContent" ,
1735
+ "GetStatus" ,
1736
+ "GetLogsByRange" ,
1737
+ "StartProviding" ,
1738
+ "StopProviding" ,
1739
+ "Rejected" ,
1740
+ "IsStarting" ,
1741
+ "SetNodes" ,
1742
+ "Reset" ,
1743
+ ] ,
1744
+ ) ) ,
1745
+ }
1721
1746
}
1722
1747
1723
1748
fn visit_map < A > ( self , mut map : A ) -> Result < Self :: Value , A :: Error >
@@ -1728,6 +1753,11 @@ impl<'de> Deserialize<'de> for CacherResponse {
1728
1753
. next_entry :: < String , serde_json:: Value > ( ) ?
1729
1754
. ok_or_else ( || de:: Error :: invalid_length ( 0 , & self ) ) ?;
1730
1755
1756
+ // Ensure there are no extra entries
1757
+ if map. next_entry :: < String , serde_json:: Value > ( ) ?. is_some ( ) {
1758
+ return Err ( de:: Error :: custom ( "unexpected extra entries in map" ) ) ;
1759
+ }
1760
+
1731
1761
match variant. as_str ( ) {
1732
1762
"GetManifest" => {
1733
1763
let manifest = serde_json:: from_value ( value) . map_err ( de:: Error :: custom) ?;
@@ -1753,8 +1783,6 @@ impl<'de> Deserialize<'de> for CacherResponse {
1753
1783
let result = serde_json:: from_value ( value) . map_err ( de:: Error :: custom) ?;
1754
1784
Ok ( CacherResponse :: StopProviding ( result) )
1755
1785
}
1756
- "Rejected" => Ok ( CacherResponse :: Rejected ) ,
1757
- "IsStarting" => Ok ( CacherResponse :: IsStarting ) ,
1758
1786
"SetNodes" => {
1759
1787
let result = serde_json:: from_value ( value) . map_err ( de:: Error :: custom) ?;
1760
1788
Ok ( CacherResponse :: SetNodes ( result) )
@@ -1782,7 +1810,7 @@ impl<'de> Deserialize<'de> for CacherResponse {
1782
1810
}
1783
1811
}
1784
1812
1785
- deserializer. deserialize_map ( CacherResponseVisitor )
1813
+ deserializer. deserialize_any ( CacherResponseVisitor )
1786
1814
}
1787
1815
}
1788
1816
0 commit comments