Skip to content

Commit

Permalink
Preserve span info when using #[openapi]
Browse files Browse the repository at this point in the history
(issue #12)
  • Loading branch information
GREsau committed Feb 17, 2020
1 parent 342056a commit a6ef948
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions rocket-okapi-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use syn::Ident;

#[proc_macro_attribute]
pub fn openapi(args: TokenStream, mut input: TokenStream) -> TokenStream {
input = preserve_span_information(input);

// We don't need to modify/replace the input TokenStream,
// we just need to append to it.
input.extend(openapi_attr::parse(args, input.clone()));
Expand All @@ -24,6 +26,16 @@ pub fn routes_with_openapi(input: TokenStream) -> TokenStream {
routes_with_openapi::parse(input)
}

fn preserve_span_information(input: TokenStream) -> TokenStream {
// Outputting the input unmodified would cause its span information to be
// lost when being consumed by other macros. But parsing it in then quoting
// it back out causes span information to be preserved.
// See https://github.com/GREsau/okapi/issues/12
// and https://github.com/rust-lang/rust/issues/43081
let parsed_input: syn::Item = syn::parse(input).unwrap();
quote!(#parsed_input).into()
}

fn get_add_operation_fn_name(route_fn_name: &Ident) -> Ident {
Ident::new(
&format!("okapi_add_operation_for_{}_", route_fn_name),
Expand Down

0 comments on commit a6ef948

Please sign in to comment.