@@ -23,12 +23,28 @@ use syn::{
23
23
} ;
24
24
use walkdir:: WalkDir ;
25
25
26
+ /// Type of an `Item`.
27
+ #[ derive( Debug , Eq , PartialEq ) ]
28
+ enum ItemKind {
29
+ Enum ,
30
+ Other ,
31
+ }
32
+
33
+ impl From < & Item > for ItemKind {
34
+ fn from ( item : & Item ) -> Self {
35
+ match item {
36
+ Item :: Enum ( _) => Self :: Enum ,
37
+ _ => Self :: Other ,
38
+ }
39
+ }
40
+ }
41
+
26
42
/// All possible validation error kinds.
27
43
#[ derive( Debug , Eq , PartialEq ) ]
28
44
enum ErrorKind {
29
45
ForbiddenAbi ,
30
46
ForbiddenAttr ,
31
- ForbiddenItemKind ,
47
+ ForbiddenItemKind ( ItemKind ) ,
32
48
ForbiddenRepr ,
33
49
ForbiddenType ,
34
50
MalformedAttrs ,
@@ -47,7 +63,9 @@ impl Display for ErrorKind {
47
63
match self {
48
64
Self :: ForbiddenAbi => "forbidden ABI" ,
49
65
Self :: ForbiddenAttr => "forbidden attribute" ,
50
- Self :: ForbiddenItemKind => "forbidden type of item" ,
66
+ Self :: ForbiddenItemKind ( ItemKind :: Enum ) =>
67
+ "forbidden use of enum; use the `newtype_enum!` macro instead" ,
68
+ Self :: ForbiddenItemKind ( _) => "forbidden type of item" ,
51
69
Self :: ForbiddenRepr => "forbidden repr" ,
52
70
Self :: ForbiddenType => "forbidden type" ,
53
71
Self :: MalformedAttrs => "malformed attribute contents" ,
@@ -364,7 +382,11 @@ fn check_item(item: &Item, src: &Path) -> Result<(), Error> {
364
382
// Allow.
365
383
}
366
384
item => {
367
- return Err ( Error :: new ( ErrorKind :: ForbiddenItemKind , src, item) ) ;
385
+ return Err ( Error :: new (
386
+ ErrorKind :: ForbiddenItemKind ( item. into ( ) ) ,
387
+ src,
388
+ item,
389
+ ) ) ;
368
390
}
369
391
}
370
392
@@ -424,15 +446,26 @@ mod tests {
424
446
}
425
447
426
448
#[ test]
427
- fn test_invalid_item ( ) {
449
+ fn test_invalid_item_enum ( ) {
428
450
// Rust enums are not allowed.
429
451
check_item_err (
430
452
parse_quote ! {
431
453
pub enum E {
432
454
A
433
455
}
434
456
} ,
435
- ErrorKind :: ForbiddenItemKind ,
457
+ ErrorKind :: ForbiddenItemKind ( ItemKind :: Enum ) ,
458
+ ) ;
459
+ }
460
+
461
+ #[ test]
462
+ fn test_invalid_item_other ( ) {
463
+ // Top-level functions are not allowed.
464
+ check_item_err (
465
+ parse_quote ! {
466
+ pub fn x( ) { }
467
+ } ,
468
+ ErrorKind :: ForbiddenItemKind ( ItemKind :: Other ) ,
436
469
) ;
437
470
}
438
471
0 commit comments