Skip to content

Commit

Permalink
Improve error messages for multiple enums in a c_enum! block (#11)
Browse files Browse the repository at this point in the history
Version 0.2.0 removed the ability to declare multiple enums in a single
c_enum! block. However, if you have existing code that does this then
the error message doesn't really explain what went wrong. This commit
adds a custom compile_error! message in addition to the regular error.
  • Loading branch information
Phantomical authored Oct 24, 2023
1 parent 119b106 commit 7ef6f82
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Changed
- `c_enum!` will now emit a custom error message indicating that declaring
multiple enums in a single `c_enum!` block is not supported anymore.

## 0.2.0 - 2023-10-23
### Added
Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ macro_rules! c_enum {
$( #[$iattr:meta] )*
impl {}
)?

// Capture part of a potential next entry in order to emit a better error.
$( $evis:vis enum $($error:tt)+ )?
} => {
$crate::__c_enum_no_debug! {
$( #[$attr] )*
Expand Down Expand Up @@ -293,6 +296,16 @@ macro_rules! c_enum {
}
}
}

$(
$crate::__expects_no_input! {
$evis enum $($error)+
}

compile_error!(
"Declaring multiple enums using a single c_enum! macro block is no longer supported",
);
)?
};
}

Expand Down Expand Up @@ -402,6 +415,15 @@ macro_rules! __c_enum_impl {
}
}

/// Helper macro to emit a "no rules expected the token `...`" error message.
///
/// We use this mainly to emit proper error messages when the user provides
#[doc(hidden)]
#[macro_export]
macro_rules! __expects_no_input {
{} => {};
}

// This needs to be after all the macro definitions.
/// This module shows an example of code generated by the macro.
///
Expand Down

0 comments on commit 7ef6f82

Please sign in to comment.