From 08e1cf3c2fd66868e71279fb765cbdc16dc39d6c Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 8 Jan 2020 10:09:41 +0100 Subject: [PATCH] api-macro: support empty api macro on structs The description comes from the doc comment, the field types and description from field doc comments and types. Previously we needed to add at least the hint that the schema is an object schema. Now we support an empty #[api] attribute as well. Signed-off-by: Wolfgang Bumiller --- proxmox-api-macro/src/api.rs | 15 +++++++++++++++ proxmox-api-macro/src/api/structs.rs | 8 ++++++-- proxmox-api-macro/src/util.rs | 4 ++++ proxmox-api-macro/tests/types.rs | 4 +--- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/proxmox-api-macro/src/api.rs b/proxmox-api-macro/src/api.rs index ef3e6102..5f0acb6b 100644 --- a/proxmox-api-macro/src/api.rs +++ b/proxmox-api-macro/src/api.rs @@ -109,6 +109,15 @@ impl Schema { } } + fn empty_object(span: Span) -> Self { + Self { + span, + description: None, + item: SchemaItem::Object(SchemaObject::new()), + properties: Vec::new(), + } + } + fn to_typed_schema(&self, ts: &mut TokenStream) -> Result<(), Error> { self.item.to_schema( ts, @@ -331,6 +340,12 @@ pub struct SchemaObject { } impl SchemaObject { + pub fn new() -> Self { + Self { + properties: Vec::new(), + } + } + fn try_extract_from(obj: &mut JSONObject) -> Result { Ok(Self { properties: obj diff --git a/proxmox-api-macro/src/api/structs.rs b/proxmox-api-macro/src/api/structs.rs index 254ca93b..c8926dc8 100644 --- a/proxmox-api-macro/src/api/structs.rs +++ b/proxmox-api-macro/src/api/structs.rs @@ -3,7 +3,7 @@ use std::convert::{TryFrom, TryInto}; use failure::Error; -use proc_macro2::{Ident, TokenStream}; +use proc_macro2::{Ident, Span, TokenStream}; use quote::quote_spanned; use super::Schema; @@ -12,7 +12,11 @@ use crate::serde; use crate::util::{self, FieldName, JSONObject}; pub fn handle_struct(attribs: JSONObject, mut stru: syn::ItemStruct) -> Result { - let mut schema: Schema = attribs.try_into()?; + let mut schema: Schema = if attribs.is_empty() { + Schema::empty_object(Span::call_site()) + } else { + attribs.try_into()? + }; if schema.description.is_none() { let (doc_comment, doc_span) = util::get_doc_comments(&stru.attrs)?; diff --git a/proxmox-api-macro/src/util.rs b/proxmox-api-macro/src/util.rs index a73f297e..0c88ce91 100644 --- a/proxmox-api-macro/src/util.rs +++ b/proxmox-api-macro/src/util.rs @@ -298,6 +298,10 @@ pub struct JSONObject { } impl JSONObject { + pub fn is_empty(&self) -> bool { + self.elements.is_empty() + } + fn parse_elements(input: ParseStream) -> syn::Result> { let map_elems: Punctuated = input.parse_terminated(JSONMapEntry::parse)?; diff --git a/proxmox-api-macro/tests/types.rs b/proxmox-api-macro/tests/types.rs index cf3fd9e9..c6c83cce 100644 --- a/proxmox-api-macro/tests/types.rs +++ b/proxmox-api-macro/tests/types.rs @@ -16,9 +16,7 @@ use serde_json::Value; //#[derive(Clone, Debug, Deserialize, Serialize)] pub struct OkString(String); -#[api( - properties: {} -)] +#[api] /// A Foo. pub struct Foo { /// A test string.