@@ -191,13 +191,42 @@ type TomlExampleTarget = TomlTarget;
191
191
type TomlTestTarget = TomlTarget ;
192
192
type TomlBenchTarget = TomlTarget ;
193
193
194
- #[ derive( Deserialize ) ]
195
- #[ serde( untagged) ]
196
194
pub enum TomlDependency {
197
195
Simple ( String ) ,
198
196
Detailed ( DetailedTomlDependency )
199
197
}
200
198
199
+ impl de:: Deserialize for TomlDependency {
200
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
201
+ where D : de:: Deserializer
202
+ {
203
+ struct TomlDependencyVisitor ;
204
+
205
+ impl de:: Visitor for TomlDependencyVisitor {
206
+ type Value = TomlDependency ;
207
+
208
+ fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
209
+ formatter. write_str ( "a version string like \" 0.9.8\" or a \
210
+ detailed dependency like { version = \" 0.9.8\" }")
211
+ }
212
+
213
+ fn visit_str < E > ( self , s : & str ) -> Result < Self :: Value , E >
214
+ where E : de:: Error
215
+ {
216
+ Ok ( TomlDependency :: Simple ( s. to_owned ( ) ) )
217
+ }
218
+
219
+ fn visit_map < V > ( self , map : V ) -> Result < Self :: Value , V :: Error >
220
+ where V : de:: MapVisitor
221
+ {
222
+ let mvd = de:: value:: MapVisitorDeserializer :: new ( map) ;
223
+ DetailedTomlDependency :: deserialize ( mvd) . map ( TomlDependency :: Detailed )
224
+ }
225
+ }
226
+
227
+ deserializer. deserialize ( TomlDependencyVisitor )
228
+ }
229
+ }
201
230
202
231
#[ derive( Deserialize , Clone , Default ) ]
203
232
pub struct DetailedTomlDependency {
@@ -288,13 +317,48 @@ impl de::Deserialize for TomlOptLevel {
288
317
}
289
318
}
290
319
291
- #[ derive( Deserialize , Clone ) ]
292
- #[ serde( untagged) ]
320
+ #[ derive( Clone ) ]
293
321
pub enum U32OrBool {
294
322
U32 ( u32 ) ,
295
323
Bool ( bool ) ,
296
324
}
297
325
326
+ impl de:: Deserialize for U32OrBool {
327
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
328
+ where D : de:: Deserializer
329
+ {
330
+ struct Visitor ;
331
+
332
+ impl de:: Visitor for Visitor {
333
+ type Value = U32OrBool ;
334
+
335
+ fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
336
+ formatter. write_str ( "a boolean or an integer" )
337
+ }
338
+
339
+ fn visit_i64 < E > ( self , u : i64 ) -> Result < Self :: Value , E >
340
+ where E : de:: Error ,
341
+ {
342
+ Ok ( U32OrBool :: U32 ( u as u32 ) )
343
+ }
344
+
345
+ fn visit_u64 < E > ( self , u : u64 ) -> Result < Self :: Value , E >
346
+ where E : de:: Error ,
347
+ {
348
+ Ok ( U32OrBool :: U32 ( u as u32 ) )
349
+ }
350
+
351
+ fn visit_bool < E > ( self , b : bool ) -> Result < Self :: Value , E >
352
+ where E : de:: Error ,
353
+ {
354
+ Ok ( U32OrBool :: Bool ( b) )
355
+ }
356
+ }
357
+
358
+ deserializer. deserialize ( Visitor )
359
+ }
360
+ }
361
+
298
362
#[ derive( Deserialize , Clone , Default ) ]
299
363
pub struct TomlProfile {
300
364
#[ serde( rename = "opt-level" ) ]
@@ -309,13 +373,42 @@ pub struct TomlProfile {
309
373
panic : Option < String > ,
310
374
}
311
375
312
- #[ derive( Deserialize , Clone , Debug ) ]
313
- #[ serde( untagged) ]
376
+ #[ derive( Clone , Debug ) ]
314
377
pub enum StringOrBool {
315
378
String ( String ) ,
316
379
Bool ( bool ) ,
317
380
}
318
381
382
+ impl de:: Deserialize for StringOrBool {
383
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
384
+ where D : de:: Deserializer
385
+ {
386
+ struct Visitor ;
387
+
388
+ impl de:: Visitor for Visitor {
389
+ type Value = StringOrBool ;
390
+
391
+ fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
392
+ formatter. write_str ( "a boolean or a string" )
393
+ }
394
+
395
+ fn visit_str < E > ( self , s : & str ) -> Result < Self :: Value , E >
396
+ where E : de:: Error ,
397
+ {
398
+ Ok ( StringOrBool :: String ( s. to_string ( ) ) )
399
+ }
400
+
401
+ fn visit_bool < E > ( self , b : bool ) -> Result < Self :: Value , E >
402
+ where E : de:: Error ,
403
+ {
404
+ Ok ( StringOrBool :: Bool ( b) )
405
+ }
406
+ }
407
+
408
+ deserializer. deserialize ( Visitor )
409
+ }
410
+ }
411
+
319
412
#[ derive( Deserialize ) ]
320
413
pub struct TomlProject {
321
414
name : String ,
@@ -405,7 +498,7 @@ fn inferred_lib_target(name: &str, layout: &Layout) -> Option<TomlTarget> {
405
498
layout. lib . as_ref ( ) . map ( |lib| {
406
499
TomlTarget {
407
500
name : Some ( name. to_string ( ) ) ,
408
- path : Some ( PathValue :: Path ( lib. clone ( ) ) ) ,
501
+ path : Some ( PathValue ( lib. clone ( ) ) ) ,
409
502
.. TomlTarget :: new ( )
410
503
}
411
504
} )
@@ -423,7 +516,7 @@ fn inferred_bin_targets(name: &str, layout: &Layout) -> Vec<TomlTarget> {
423
516
name. map ( |name| {
424
517
TomlTarget {
425
518
name : Some ( name) ,
426
- path : Some ( PathValue :: Path ( bin. clone ( ) ) ) ,
519
+ path : Some ( PathValue ( bin. clone ( ) ) ) ,
427
520
.. TomlTarget :: new ( )
428
521
}
429
522
} )
@@ -435,7 +528,7 @@ fn inferred_example_targets(layout: &Layout) -> Vec<TomlTarget> {
435
528
ex. file_stem ( ) . and_then ( |s| s. to_str ( ) ) . map ( |name| {
436
529
TomlTarget {
437
530
name : Some ( name. to_string ( ) ) ,
438
- path : Some ( PathValue :: Path ( ex. clone ( ) ) ) ,
531
+ path : Some ( PathValue ( ex. clone ( ) ) ) ,
439
532
.. TomlTarget :: new ( )
440
533
}
441
534
} )
@@ -447,7 +540,7 @@ fn inferred_test_targets(layout: &Layout) -> Vec<TomlTarget> {
447
540
ex. file_stem ( ) . and_then ( |s| s. to_str ( ) ) . map ( |name| {
448
541
TomlTarget {
449
542
name : Some ( name. to_string ( ) ) ,
450
- path : Some ( PathValue :: Path ( ex. clone ( ) ) ) ,
543
+ path : Some ( PathValue ( ex. clone ( ) ) ) ,
451
544
.. TomlTarget :: new ( )
452
545
}
453
546
} )
@@ -459,7 +552,7 @@ fn inferred_bench_targets(layout: &Layout) -> Vec<TomlTarget> {
459
552
ex. file_stem ( ) . and_then ( |s| s. to_str ( ) ) . map ( |name| {
460
553
TomlTarget {
461
554
name : Some ( name. to_string ( ) ) ,
462
- path : Some ( PathValue :: Path ( ex. clone ( ) ) ) ,
555
+ path : Some ( PathValue ( ex. clone ( ) ) ) ,
463
556
.. TomlTarget :: new ( )
464
557
}
465
558
} )
@@ -498,7 +591,7 @@ impl TomlManifest {
498
591
TomlTarget {
499
592
name : lib. name . clone ( ) . or ( Some ( project. name . clone ( ) ) ) ,
500
593
path : lib. path . clone ( ) . or_else (
501
- || layout. lib . as_ref ( ) . map ( |p| PathValue :: Path ( p. clone ( ) ) )
594
+ || layout. lib . as_ref ( ) . map ( |p| PathValue ( p. clone ( ) ) )
502
595
) ,
503
596
..lib. clone ( )
504
597
}
@@ -995,11 +1088,15 @@ struct TomlTarget {
995
1088
required_features : Option < Vec < String > > ,
996
1089
}
997
1090
998
- #[ derive( Deserialize , Clone ) ]
999
- #[ serde( untagged) ]
1000
- enum PathValue {
1001
- String ( String ) ,
1002
- Path ( PathBuf ) ,
1091
+ #[ derive( Clone ) ]
1092
+ struct PathValue ( PathBuf ) ;
1093
+
1094
+ impl de:: Deserialize for PathValue {
1095
+ fn deserialize < D > ( deserializer : D ) -> Result < Self , D :: Error >
1096
+ where D : de:: Deserializer
1097
+ {
1098
+ Ok ( PathValue ( String :: deserialize ( deserializer) ?. into ( ) ) )
1099
+ }
1003
1100
}
1004
1101
1005
1102
/// Corresponds to a `target` entry, but `TomlTarget` is already used.
@@ -1118,21 +1215,9 @@ impl TomlTarget {
1118
1215
}
1119
1216
}
1120
1217
1121
- impl PathValue {
1122
- fn to_path ( & self ) -> PathBuf {
1123
- match * self {
1124
- PathValue :: String ( ref s) => PathBuf :: from ( s) ,
1125
- PathValue :: Path ( ref p) => p. clone ( ) ,
1126
- }
1127
- }
1128
- }
1129
-
1130
1218
impl fmt:: Debug for PathValue {
1131
1219
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
1132
- match * self {
1133
- PathValue :: String ( ref s) => s. fmt ( f) ,
1134
- PathValue :: Path ( ref p) => p. display ( ) . fmt ( f) ,
1135
- }
1220
+ self . 0 . fmt ( f)
1136
1221
}
1137
1222
}
1138
1223
@@ -1159,7 +1244,7 @@ fn normalize(package_root: &Path,
1159
1244
1160
1245
let lib_target = |dst : & mut Vec < Target > , l : & TomlLibTarget | {
1161
1246
let path = l. path . clone ( ) . unwrap_or_else (
1162
- || PathValue :: Path ( Path :: new ( "src" ) . join ( & format ! ( "{}.rs" , l. name( ) ) ) )
1247
+ || PathValue ( Path :: new ( "src" ) . join ( & format ! ( "{}.rs" , l. name( ) ) ) )
1163
1248
) ;
1164
1249
let crate_types = l. crate_type . as_ref ( ) . or ( l. crate_type2 . as_ref ( ) ) ;
1165
1250
let crate_types = match crate_types {
@@ -1172,7 +1257,7 @@ fn normalize(package_root: &Path,
1172
1257
} ;
1173
1258
1174
1259
let mut target = Target :: lib_target ( & l. name ( ) , crate_types,
1175
- package_root. join ( path. to_path ( ) ) ) ;
1260
+ package_root. join ( & path. 0 ) ) ;
1176
1261
configure ( l, & mut target) ;
1177
1262
dst. push ( target) ;
1178
1263
} ;
@@ -1181,14 +1266,14 @@ fn normalize(package_root: &Path,
1181
1266
default : & mut FnMut ( & TomlBinTarget ) -> PathBuf | {
1182
1267
for bin in bins. iter ( ) {
1183
1268
let path = bin. path . clone ( ) . unwrap_or_else ( || {
1184
- let default_bin_path = PathValue :: Path ( default ( bin) ) ;
1185
- if package_root. join ( default_bin_path. to_path ( ) ) . exists ( ) {
1269
+ let default_bin_path = PathValue ( default ( bin) ) ;
1270
+ if package_root. join ( & default_bin_path. 0 ) . exists ( ) {
1186
1271
default_bin_path // inferred from bin's name
1187
1272
} else {
1188
- PathValue :: Path ( Path :: new ( "src" ) . join ( "main.rs" ) )
1273
+ PathValue ( Path :: new ( "src" ) . join ( "main.rs" ) )
1189
1274
}
1190
1275
} ) ;
1191
- let mut target = Target :: bin_target ( & bin. name ( ) , package_root. join ( path. to_path ( ) ) ,
1276
+ let mut target = Target :: bin_target ( & bin. name ( ) , package_root. join ( & path. 0 ) ,
1192
1277
bin. required_features . clone ( ) ) ;
1193
1278
configure ( bin, & mut target) ;
1194
1279
dst. push ( target) ;
@@ -1207,7 +1292,7 @@ fn normalize(package_root: &Path,
1207
1292
default : & mut FnMut ( & TomlExampleTarget ) -> PathBuf | {
1208
1293
for ex in examples. iter ( ) {
1209
1294
let path = ex. path . clone ( ) . unwrap_or_else ( || {
1210
- PathValue :: Path ( default ( ex) )
1295
+ PathValue ( default ( ex) )
1211
1296
} ) ;
1212
1297
1213
1298
let crate_types = ex. crate_type . as_ref ( ) . or ( ex. crate_type2 . as_ref ( ) ) ;
@@ -1219,7 +1304,7 @@ fn normalize(package_root: &Path,
1219
1304
let mut target = Target :: example_target (
1220
1305
& ex. name ( ) ,
1221
1306
crate_types,
1222
- package_root. join ( path. to_path ( ) ) ,
1307
+ package_root. join ( & path. 0 ) ,
1223
1308
ex. required_features . clone ( )
1224
1309
) ;
1225
1310
configure ( ex, & mut target) ;
@@ -1232,10 +1317,10 @@ fn normalize(package_root: &Path,
1232
1317
default : & mut FnMut ( & TomlTestTarget ) -> PathBuf | {
1233
1318
for test in tests. iter ( ) {
1234
1319
let path = test. path . clone ( ) . unwrap_or_else ( || {
1235
- PathValue :: Path ( default ( test) )
1320
+ PathValue ( default ( test) )
1236
1321
} ) ;
1237
1322
1238
- let mut target = Target :: test_target ( & test. name ( ) , package_root. join ( path. to_path ( ) ) ,
1323
+ let mut target = Target :: test_target ( & test. name ( ) , package_root. join ( & path. 0 ) ,
1239
1324
test. required_features . clone ( ) ) ;
1240
1325
configure ( test, & mut target) ;
1241
1326
dst. push ( target) ;
@@ -1247,10 +1332,10 @@ fn normalize(package_root: &Path,
1247
1332
default : & mut FnMut ( & TomlBenchTarget ) -> PathBuf | {
1248
1333
for bench in benches. iter ( ) {
1249
1334
let path = bench. path . clone ( ) . unwrap_or_else ( || {
1250
- PathValue :: Path ( default ( bench) )
1335
+ PathValue ( default ( bench) )
1251
1336
} ) ;
1252
1337
1253
- let mut target = Target :: bench_target ( & bench. name ( ) , package_root. join ( path. to_path ( ) ) ,
1338
+ let mut target = Target :: bench_target ( & bench. name ( ) , package_root. join ( & path. 0 ) ,
1254
1339
bench. required_features . clone ( ) ) ;
1255
1340
configure ( bench, & mut target) ;
1256
1341
dst. push ( target) ;
0 commit comments