From 7609cf2bd4580fd1ddbe16c717b65d23837c107e Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 4 Dec 2019 11:47:27 +0100 Subject: [PATCH] api-macro: move doc-comment reading to util The function attribute reader doesn't read any other attributes anymore, so make it reusable! Signed-off-by: Wolfgang Bumiller --- proxmox-api-macro/src/api/method.rs | 41 ++++++----------------------- proxmox-api-macro/src/util.rs | 22 ++++++++++++++++ 2 files changed, 30 insertions(+), 33 deletions(-) diff --git a/proxmox-api-macro/src/api/method.rs b/proxmox-api-macro/src/api/method.rs index a9d9c2d1..a0a64903 100644 --- a/proxmox-api-macro/src/api/method.rs +++ b/proxmox-api-macro/src/api/method.rs @@ -1,5 +1,4 @@ use std::convert::{TryFrom, TryInto}; -use std::mem; use failure::Error; @@ -9,7 +8,7 @@ use syn::spanned::Spanned; use syn::Ident; use super::{PropertySchema, Schema, SchemaItem}; -use crate::util::{BareAssignment, JSONObject, FieldName}; +use crate::util::{self, FieldName, JSONObject}; /// Parse `input`, `returns` and `protected` attributes out of an function annotated /// with an `#[api]` attribute and produce a `const ApiMethod` named after the function. @@ -39,7 +38,13 @@ pub fn handle_method(mut attribs: JSONObject, mut func: syn::ItemFn) -> Result Result, - attrs: &mut Vec, -) -> Result<(), Error> { - let mut doc_comment = String::new(); - let doc_span = Span::call_site(); // FIXME: set to first doc comment - - for attr in mem::replace(attrs, Vec::new()) { - // don't mess with #![...] - if let syn::AttrStyle::Inner(_) = &attr.style { - attrs.push(attr); - continue; - } - - if attr.path.is_ident("doc") { - let doc: BareAssignment = syn::parse2(attr.tokens.clone())?; - if !doc_comment.is_empty() { - doc_comment.push_str("\n"); - } - doc_comment.push_str(doc.content.value().trim()); - attrs.push(attr); - } else { - attrs.push(attr); - } - } - - derive_descriptions(input_schema, returns_schema, &doc_comment, doc_span) -} - fn derive_descriptions( input_schema: &mut Schema, returns_schema: &mut Option, diff --git a/proxmox-api-macro/src/util.rs b/proxmox-api-macro/src/util.rs index eba4fd5f..d00d8d01 100644 --- a/proxmox-api-macro/src/util.rs +++ b/proxmox-api-macro/src/util.rs @@ -375,3 +375,25 @@ impl Parse for BareAssignment { }) } } + +pub fn get_doc_comments(attributes: &[syn::Attribute]) -> Result<(String, Span), syn::Error> { + let mut doc_comment = String::new(); + let doc_span = Span::call_site(); // FIXME: set to first doc comment + + for attr in attributes { + // skip #![...] + if let syn::AttrStyle::Inner(_) = &attr.style { + continue; + } + + if attr.path.is_ident("doc") { + let doc: BareAssignment = syn::parse2(attr.tokens.clone())?; + if !doc_comment.is_empty() { + doc_comment.push_str("\n"); + } + doc_comment.push_str(doc.content.value().trim()); + } + } + + Ok((doc_comment, doc_span)) +}