Skip to content

Commit 1690a9d

Browse files
committed
[bevy_derive] Refactor modules for better error message. (#2059)
Problem: - When using the 'as_crate' attribute, if 'as_crate' was empty, the only error you would get is 'integer underflow'. Solution: - Provide an explicit check for the 'as_crate' attribute's token stream to ensure the formatting is correct. Note: - Also reworked 'get_meta' by not making it call 'Manifest::find' twice.
1 parent ce6dda2 commit 1690a9d

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

crates/bevy_derive/src/modules.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,18 @@ fn get_meta() -> Option<Modules> {
4646

4747
const AS_CRATE_ATTRIBUTE_NAME: &str = "as_crate";
4848

49+
fn validate_as_crate_attribute(tokens: &str) -> bool {
50+
tokens.len() > 2 && tokens.starts_with('(') && tokens.ends_with(')')
51+
}
52+
4953
pub fn get_modules(attributes: &[Attribute]) -> Modules {
5054
let mut modules = get_meta().unwrap_or_else(Modules::external);
5155
for attribute in attributes.iter() {
5256
if *attribute.path.get_ident().as_ref().unwrap() == AS_CRATE_ATTRIBUTE_NAME {
5357
let value = attribute.tokens.to_string();
54-
if value[1..value.len() - 1] == modules.bevy_render {
58+
if !validate_as_crate_attribute(&value) {
59+
panic!("The attribute `#[as_crate{}]` is invalid. It must follow the format `#[as_crate(<crate name>)]`", value);
60+
} else if value[1..value.len() - 1] == modules.bevy_render {
5561
modules.bevy_render = "crate".to_string();
5662
}
5763
}

0 commit comments

Comments
 (0)