Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error messages for multiple enums in a c_enum! block #11

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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