1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-20 00:21:07 +00:00

Port over doc comments in route macros. (#2022)

Co-authored-by: Jonas Platte <jplatte@users.noreply.github.com>
Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Erik Johnston 2021-02-24 12:26:56 +00:00 committed by GitHub
parent f6393728c7
commit 42711c23d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 0 deletions

View file

@ -1,6 +1,9 @@
# Changes # Changes
## Unreleased - 2021-xx-xx ## 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 ## 0.5.0-beta.1 - 2021-02-10

View file

@ -176,6 +176,9 @@ pub struct Route {
args: Args, args: Args,
ast: syn::ItemFn, ast: syn::ItemFn,
resource_type: ResourceType, 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 { fn guess_resource_type(typ: &syn::Type) -> ResourceType {
@ -221,6 +224,18 @@ impl Route {
let ast: syn::ItemFn = syn::parse(input)?; let ast: syn::ItemFn = syn::parse(input)?;
let name = ast.sig.ident.clone(); 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)?; let args = Args::new(args, method)?;
if args.methods.is_empty() { if args.methods.is_empty() {
return Err(syn::Error::new( return Err(syn::Error::new(
@ -248,6 +263,7 @@ impl Route {
args, args,
ast, ast,
resource_type, resource_type,
doc_attributes,
}) })
} }
} }
@ -265,6 +281,7 @@ impl ToTokens for Route {
methods, methods,
}, },
resource_type, resource_type,
doc_attributes,
} = self; } = self;
let resource_name = name.to_string(); let resource_name = name.to_string();
let method_guards = { let method_guards = {
@ -287,6 +304,7 @@ impl ToTokens for Route {
}; };
let stream = quote! { let stream = quote! {
#(#doc_attributes)*
#[allow(non_camel_case_types, missing_docs)] #[allow(non_camel_case_types, missing_docs)]
pub struct #name; pub struct #name;

View file

@ -9,6 +9,8 @@ fn compile_macros() {
t.compile_fail("tests/trybuild/route-missing-method-fail.rs"); 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-duplicate-method-fail.rs");
t.compile_fail("tests/trybuild/route-unexpected-method-fail.rs"); t.compile_fail("tests/trybuild/route-unexpected-method-fail.rs");
t.pass("tests/trybuild/docstring-ok.rs");
} }
// #[rustversion::not(nightly)] // #[rustversion::not(nightly)]

View file

@ -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());
}