Skip to content

Commit

Permalink
Port over doc comments in route macros. (actix#2022)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Platte <[email protected]>
Co-authored-by: Rob Ede <[email protected]>
  • Loading branch information
3 people authored Feb 24, 2021
1 parent f639372 commit 42711c2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions actix-web-codegen/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changes

## Unreleased - 2021-xx-xx
* Preserve doc comments when using route macros. [#2022]

[#2022]: https://github.com/actix/actix-web/pull/2022


## 0.5.0-beta.1 - 2021-02-10
Expand Down
18 changes: 18 additions & 0 deletions actix-web-codegen/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ pub struct Route {
args: Args,
ast: syn::ItemFn,
resource_type: ResourceType,

/// The doc comment attributes to copy to generated struct, if any.
doc_attributes: Vec<syn::Attribute>,
}

fn guess_resource_type(typ: &syn::Type) -> ResourceType {
Expand Down Expand Up @@ -221,6 +224,18 @@ impl Route {
let ast: syn::ItemFn = syn::parse(input)?;
let name = ast.sig.ident.clone();

// Try and pull out the doc comments so that we can reapply them to the
// generated struct.
//
// Note that multi line doc comments are converted to multiple doc
// attributes.
let doc_attributes = ast
.attrs
.iter()
.filter(|attr| attr.path.is_ident("doc"))
.cloned()
.collect();

let args = Args::new(args, method)?;
if args.methods.is_empty() {
return Err(syn::Error::new(
Expand Down Expand Up @@ -248,6 +263,7 @@ impl Route {
args,
ast,
resource_type,
doc_attributes,
})
}
}
Expand All @@ -265,6 +281,7 @@ impl ToTokens for Route {
methods,
},
resource_type,
doc_attributes,
} = self;
let resource_name = name.to_string();
let method_guards = {
Expand All @@ -287,6 +304,7 @@ impl ToTokens for Route {
};

let stream = quote! {
#(#doc_attributes)*
#[allow(non_camel_case_types, missing_docs)]
pub struct #name;

Expand Down
2 changes: 2 additions & 0 deletions actix-web-codegen/tests/trybuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ fn compile_macros() {
t.compile_fail("tests/trybuild/route-missing-method-fail.rs");
t.compile_fail("tests/trybuild/route-duplicate-method-fail.rs");
t.compile_fail("tests/trybuild/route-unexpected-method-fail.rs");

t.pass("tests/trybuild/docstring-ok.rs");
}

// #[rustversion::not(nightly)]
Expand Down
17 changes: 17 additions & 0 deletions actix-web-codegen/tests/trybuild/docstring-ok.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use actix_web::{Responder, HttpResponse, App, test};
use actix_web_codegen::*;

/// Docstrings shouldn't break anything.
#[get("/")]
async fn index() -> impl Responder {
HttpResponse::Ok()
}

#[actix_web::main]
async fn main() {
let srv = test::start(|| App::new().service(index));

let request = srv.get("/");
let response = request.send().await.unwrap();
assert!(response.status().is_success());
}

0 comments on commit 42711c2

Please sign in to comment.