diff --git a/proxmox-api-macro/src/api_macro.rs b/proxmox-api-macro/src/api_macro.rs index 25098910..585f58cc 100644 --- a/proxmox-api-macro/src/api_macro.rs +++ b/proxmox-api-macro/src/api_macro.rs @@ -17,9 +17,10 @@ pub fn api_macro(attr: TokenStream, item: TokenStream) -> Result group.stream(), - _ => cbail!(definition.span() => "expected api definition in braces"), + _ => c_bail!(definition.span() => "expected api definition in braces"), }; + let def_span = definition.span(); let definition = parse_object(definition)?; // Now parse the item, based on which we decide whether this is an API method which needs a @@ -33,17 +34,18 @@ pub fn api_macro(attr: TokenStream, item: TokenStream) -> Result handle_function(definition, func), - _ => cbail!(item.span() => "api macro currently only applies to structs and functions"), + syn::Item::Fn(func) => handle_function(def_span, definition, func), + _ => c_bail!(item.span() => "api macro currently only applies to structs and functions"), } } fn handle_function( + def_span: Span, mut definition: HashMap, mut item: syn::ItemFn, ) -> Result { if item.decl.generics.lt_token.is_some() { - cbail!( + c_bail!( item.decl.generics.span(), "cannot use generic functions for api macros currently", ); @@ -56,7 +58,7 @@ fn handle_function( let fn_api_description = definition .remove("description") - .ok_or_else(|| format_err!("missing 'description' in method definition"))? + .ok_or_else(|| c_format_err!(def_span, "missing 'description' in method definition"))? .expect_lit_str()?; let fn_api_protected = definition diff --git a/proxmox-api-macro/src/error.rs b/proxmox-api-macro/src/error.rs index f239aee0..e1f298f7 100644 --- a/proxmox-api-macro/src/error.rs +++ b/proxmox-api-macro/src/error.rs @@ -14,11 +14,18 @@ impl std::fmt::Display for CompileError { impl std::error::Error for CompileError {} -macro_rules! cbail { +macro_rules! c_format_err { ($span:expr => $($msg:tt)*) => { - return Err(::failure::Error::from(crate::error::CompileError { + crate::error::CompileError { tokens: ::quote::quote_spanned! { $span => compile_error!($($msg)*); }.into() - })) + } }; - ($span:expr, $($msg:tt)*) => { cbail!($span => $($msg)*) } + ($span:expr, $($msg:tt)*) => { c_format_err!($span => $($msg)*) } +} + +macro_rules! c_bail { + ($span:expr => $($msg:tt)*) => { + return Err(c_format_err!($span => $($msg)*).into()); + }; + ($span:expr, $($msg:tt)*) => { c_bail!($span => $($msg)*) } }