-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
WIP: Implement auto-documenting routes #1624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -296,6 +296,25 @@ fn sentinels_expr(route: &Route) -> TokenStream { | |
quote!(::std::vec![#(#sentinel),*]) | ||
} | ||
|
||
fn schemas_expr(route: &Route) -> TokenStream { | ||
let ret_ty = match route.handler.sig.output { | ||
syn::ReturnType::Default => None, | ||
syn::ReturnType::Type(_, ref ty) => Some(ty.with_stripped_lifetimes()) | ||
}; | ||
|
||
let eligible_types = route.guards() | ||
.map(|guard| &guard.ty) | ||
.chain(ret_ty.as_ref().into_iter()); | ||
|
||
let schema = eligible_types.map(|ty| { | ||
define_spanned_export!(ty.span() => _doc); | ||
|
||
quote_spanned!(ty.span() => #_doc::resolve_doc!(#ty)) | ||
}); | ||
|
||
quote!(::std::vec![#(#schema),*]) | ||
} | ||
|
||
fn codegen_route(route: Route) -> Result<TokenStream> { | ||
use crate::exports::*; | ||
|
||
|
@@ -305,8 +324,9 @@ fn codegen_route(route: Route) -> Result<TokenStream> { | |
let query_guards = query_decls(&route); | ||
let data_guard = route.data_guard.as_ref().map(data_guard_decl); | ||
|
||
// Extract the sentinels from the route. | ||
// Extract the sentinels and schemas from the route. | ||
let sentinels = sentinels_expr(&route); | ||
let schemas = schemas_expr(&route); | ||
|
||
// Gather info about the function. | ||
let (vis, handler_fn) = (&route.handler.vis, &route.handler); | ||
|
@@ -319,6 +339,9 @@ fn codegen_route(route: Route) -> Result<TokenStream> { | |
let rank = Optional(route.attr.rank); | ||
let format = Optional(route.attr.format.as_ref()); | ||
|
||
// Get the doc comment | ||
let docstring = &route.docstring; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to separate this. Group it with the rest. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean add it to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I just need style wise. Basically to just remove the whitespace and comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right okay |
||
|
||
Ok(quote! { | ||
#handler_fn | ||
|
||
|
@@ -353,6 +376,8 @@ fn codegen_route(route: Route) -> Result<TokenStream> { | |
format: #format, | ||
rank: #rank, | ||
sentinels: #sentinels, | ||
schemas: #schemas, | ||
docstring: #docstring, | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ pub struct Route { | |
pub handler: syn::ItemFn, | ||
/// The parsed arguments to the user's function. | ||
pub arguments: Arguments, | ||
/// The doc comment describing this route | ||
pub docstring: String, | ||
} | ||
|
||
type ArgumentMap = IndexMap<Name, (syn::Ident, syn::Type)>; | ||
|
@@ -209,9 +211,11 @@ impl Route { | |
}) | ||
.collect(); | ||
|
||
let docstring = String::from_attrs("doc", &handler.attrs)?.join("\n"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you determine if we need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe we need it, because Rust comments are markdown, which is also supported for OpenApi and RAML. Markdown is a newline sensitive format: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My question is really whether rustdoc already inserts a new line or whether it infers one based on having multiple attributes. You should be able to easily check. We only need the new line here if it is inferred. |
||
|
||
diags.head_err_or(Route { | ||
attr, path_params, query_params, data_guard, request_guards, | ||
handler, arguments, | ||
handler, arguments, docstring | ||
}) | ||
} | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.