Skip to content

Commit dbf0152

Browse files
nicholasbishopphip1611
authored andcommitted
xtask: Improve error message for enums in uefi-raw
Instead of a generic "forbidden type of item" error, recommend using the `newtype_enum!` macro.
1 parent 87ab019 commit dbf0152

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

xtask/src/check_raw.rs

+38-5
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,28 @@ use syn::{
2323
};
2424
use walkdir::WalkDir;
2525

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+
2642
/// All possible validation error kinds.
2743
#[derive(Debug, Eq, PartialEq)]
2844
enum ErrorKind {
2945
ForbiddenAbi,
3046
ForbiddenAttr,
31-
ForbiddenItemKind,
47+
ForbiddenItemKind(ItemKind),
3248
ForbiddenRepr,
3349
ForbiddenType,
3450
MalformedAttrs,
@@ -47,7 +63,9 @@ impl Display for ErrorKind {
4763
match self {
4864
Self::ForbiddenAbi => "forbidden ABI",
4965
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",
5169
Self::ForbiddenRepr => "forbidden repr",
5270
Self::ForbiddenType => "forbidden type",
5371
Self::MalformedAttrs => "malformed attribute contents",
@@ -364,7 +382,11 @@ fn check_item(item: &Item, src: &Path) -> Result<(), Error> {
364382
// Allow.
365383
}
366384
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+
));
368390
}
369391
}
370392

@@ -424,15 +446,26 @@ mod tests {
424446
}
425447

426448
#[test]
427-
fn test_invalid_item() {
449+
fn test_invalid_item_enum() {
428450
// Rust enums are not allowed.
429451
check_item_err(
430452
parse_quote! {
431453
pub enum E {
432454
A
433455
}
434456
},
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),
436469
);
437470
}
438471

0 commit comments

Comments
 (0)