@@ -410,11 +410,7 @@ impl<'de> de::Deserialize<'de> for InheritableBtreeMap {
410
410
if let Ok ( w) = TomlInheritedField :: deserialize (
411
411
serde_value:: ValueDeserializer :: < D :: Error > :: new ( value. clone ( ) ) ,
412
412
) {
413
- return if w. workspace {
414
- Ok ( InheritableField :: Inherit ( w) )
415
- } else {
416
- Err ( de:: Error :: custom ( "`workspace` cannot be false" ) )
417
- } ;
413
+ return Ok ( InheritableField :: Inherit ( w) ) ;
418
414
}
419
415
BTreeMap :: deserialize ( serde_value:: ValueDeserializer :: < D :: Error > :: new ( value) )
420
416
. map ( InheritableField :: Value )
@@ -424,13 +420,14 @@ impl<'de> de::Deserialize<'de> for InheritableBtreeMap {
424
420
#[ derive( Deserialize , Serialize , Copy , Clone , Debug ) ]
425
421
#[ serde( rename_all = "kebab-case" ) ]
426
422
pub struct TomlInheritedField {
427
- #[ serde( deserialize_with = "bool_no_false" ) ]
428
- workspace : bool ,
423
+ workspace : WorkspaceValue ,
429
424
}
430
425
431
426
impl TomlInheritedField {
432
427
pub fn new ( ) -> Self {
433
- TomlInheritedField { workspace : true }
428
+ TomlInheritedField {
429
+ workspace : WorkspaceValue ,
430
+ }
434
431
}
435
432
}
436
433
@@ -440,12 +437,25 @@ impl Default for TomlInheritedField {
440
437
}
441
438
}
442
439
443
- fn bool_no_false < ' de , D : de:: Deserializer < ' de > > ( deserializer : D ) -> Result < bool , D :: Error > {
444
- let b: bool = Deserialize :: deserialize ( deserializer) ?;
445
- if b {
446
- Ok ( b)
447
- } else {
448
- Err ( de:: Error :: custom ( "`workspace` cannot be false" ) )
440
+ #[ derive( Deserialize , Serialize , Copy , Clone , Debug ) ]
441
+ #[ serde( try_from = "bool" ) ]
442
+ #[ serde( into = "bool" ) ]
443
+ struct WorkspaceValue ;
444
+
445
+ impl TryFrom < bool > for WorkspaceValue {
446
+ type Error = String ;
447
+ fn try_from ( other : bool ) -> Result < WorkspaceValue , Self :: Error > {
448
+ if other {
449
+ Ok ( WorkspaceValue )
450
+ } else {
451
+ Err ( "`workspace` cannot be false" . to_owned ( ) )
452
+ }
453
+ }
454
+ }
455
+
456
+ impl From < WorkspaceValue > for bool {
457
+ fn from ( _: WorkspaceValue ) -> bool {
458
+ true
449
459
}
450
460
}
451
461
@@ -1250,12 +1260,9 @@ impl TomlPlatform {
1250
1260
}
1251
1261
}
1252
1262
1253
- #[ derive( Deserialize , Serialize , Debug , Clone ) ]
1254
- #[ serde( expecting = "a lints table" ) ]
1255
- #[ serde( rename_all = "kebab-case" ) ]
1263
+ #[ derive( Serialize , Debug , Clone ) ]
1256
1264
pub struct InheritableLints {
1257
1265
#[ serde( skip_serializing_if = "is_false" ) ]
1258
- #[ serde( deserialize_with = "bool_no_false" , default ) ]
1259
1266
pub workspace : bool ,
1260
1267
#[ serde( flatten) ]
1261
1268
pub lints : TomlLints ,
@@ -1265,6 +1272,54 @@ fn is_false(b: &bool) -> bool {
1265
1272
!b
1266
1273
}
1267
1274
1275
+ impl < ' de > Deserialize < ' de > for InheritableLints {
1276
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
1277
+ where
1278
+ D : de:: Deserializer < ' de > ,
1279
+ {
1280
+ struct InheritableLintsVisitor ;
1281
+
1282
+ impl < ' de > de:: Visitor < ' de > for InheritableLintsVisitor {
1283
+ // The type that our Visitor is going to produce.
1284
+ type Value = InheritableLints ;
1285
+
1286
+ // Format a message stating what data this Visitor expects to receive.
1287
+ fn expecting ( & self , formatter : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
1288
+ formatter. write_str ( "a lints table" )
1289
+ }
1290
+
1291
+ // Deserialize MyMap from an abstract "map" provided by the
1292
+ // Deserializer. The MapAccess input is a callback provided by
1293
+ // the Deserializer to let us see each entry in the map.
1294
+ fn visit_map < M > ( self , mut access : M ) -> Result < Self :: Value , M :: Error >
1295
+ where
1296
+ M : de:: MapAccess < ' de > ,
1297
+ {
1298
+ let mut lints = TomlLints :: new ( ) ;
1299
+ let mut workspace = false ;
1300
+
1301
+ // While there are entries remaining in the input, add them
1302
+ // into our map.
1303
+ while let Some ( key) = access. next_key ( ) ? {
1304
+ if key == "workspace" {
1305
+ workspace = match access. next_value ( ) ? {
1306
+ Some ( WorkspaceValue ) => true ,
1307
+ None => false ,
1308
+ } ;
1309
+ } else {
1310
+ let value = access. next_value ( ) ?;
1311
+ lints. insert ( key, value) ;
1312
+ }
1313
+ }
1314
+
1315
+ Ok ( InheritableLints { workspace, lints } )
1316
+ }
1317
+ }
1318
+
1319
+ deserializer. deserialize_map ( InheritableLintsVisitor )
1320
+ }
1321
+ }
1322
+
1268
1323
pub type TomlLints = BTreeMap < String , TomlToolLints > ;
1269
1324
1270
1325
pub type TomlToolLints = BTreeMap < String , TomlLint > ;
0 commit comments