More user-friendly error message about missing value for `name` attribute in module macro

Update the `[lua_module]` doc
This commit is contained in:
Alex Orlenko 2023-03-28 14:41:21 +01:00
parent 0f937b0a03
commit e182d474e0
No known key found for this signature in database
GPG Key ID: 4C150C250863B96D
2 changed files with 19 additions and 1 deletions

View File

@ -18,7 +18,14 @@ struct ModuleAttributes {
impl ModuleAttributes {
fn parse(&mut self, meta: ParseNestedMeta) -> Result<()> {
if meta.path.is_ident("name") {
self.name = Some(meta.value()?.parse::<LitStr>()?.parse()?);
match meta.value() {
Ok(value) => {
self.name = Some(value.parse::<LitStr>()?.parse()?);
}
Err(_) => {
return Err(meta.error(format!("`name` attribute must have a value")));
}
}
} else {
return Err(meta.error("unsupported module attribute"));
}

View File

@ -230,6 +230,17 @@ pub use mlua_derive::chunk;
///
/// Internally in the code above the compiler defines C function `luaopen_my_module`.
///
/// You can also pass options to the attribute:
///
/// name - name of the module, defaults to the name of the function
///
/// ```ignore
/// #[mlua::lua_module(name = "alt_module")]
/// fn my_module(lua: &Lua) -> Result<Table> {
/// ...
/// }
/// ```
///
#[cfg(any(feature = "module", docsrs))]
#[cfg_attr(docsrs, doc(cfg(feature = "module")))]
pub use mlua_derive::lua_module;