mirror of
https://git.proxmox.com/git/proxmox
synced 2025-08-03 06:29:45 +00:00
api-macro: split struct handling into newtype, regular, unit, ...
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
4c77a7fece
commit
b9769de6b6
@ -2,13 +2,13 @@ use std::convert::TryInto;
|
||||
|
||||
use failure::Error;
|
||||
|
||||
use proc_macro2::TokenStream;
|
||||
use proc_macro2::{Ident, TokenStream};
|
||||
use quote::quote_spanned;
|
||||
|
||||
use super::Schema;
|
||||
use crate::util::{self, JSONObject};
|
||||
|
||||
pub fn handle_struct(attribs: JSONObject, stru: syn::ItemStruct) -> Result<TokenStream, Error> {
|
||||
pub fn handle_struct(attribs: JSONObject, mut stru: syn::ItemStruct) -> Result<TokenStream, Error> {
|
||||
let mut schema: Schema = attribs.try_into()?;
|
||||
|
||||
if schema.description.is_none() {
|
||||
@ -16,14 +16,31 @@ pub fn handle_struct(attribs: JSONObject, stru: syn::ItemStruct) -> Result<Token
|
||||
util::derive_descriptions(&mut schema, &mut None, &doc_comment, doc_span)?;
|
||||
}
|
||||
|
||||
match &stru.fields {
|
||||
// unit structs, not sure about these?
|
||||
syn::Fields::Unit => finish_schema(schema, &stru, &stru.ident),
|
||||
syn::Fields::Unnamed(fields) if fields.unnamed.len() == 1 => {
|
||||
handle_newtype_struct(schema, &mut stru)
|
||||
}
|
||||
syn::Fields::Unnamed(fields) => bail!(
|
||||
fields.paren_token.span,
|
||||
"api macro does not support tuple structs"
|
||||
),
|
||||
syn::Fields::Named(fields) => handle_regular_struct(schema, &mut stru),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn finish_schema(
|
||||
schema: Schema,
|
||||
stru: &syn::ItemStruct,
|
||||
name: &Ident,
|
||||
) -> Result<TokenStream, Error> {
|
||||
let schema = {
|
||||
let mut ts = TokenStream::new();
|
||||
schema.to_schema(&mut ts)?;
|
||||
ts
|
||||
};
|
||||
|
||||
let name = &stru.ident;
|
||||
|
||||
Ok(quote_spanned! { name.span() =>
|
||||
#stru
|
||||
impl #name {
|
||||
@ -31,3 +48,17 @@ pub fn handle_struct(attribs: JSONObject, stru: syn::ItemStruct) -> Result<Token
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn handle_newtype_struct(
|
||||
schema: Schema,
|
||||
stru: &mut syn::ItemStruct,
|
||||
) -> Result<TokenStream, Error> {
|
||||
finish_schema(schema, &stru, &stru.ident)
|
||||
}
|
||||
|
||||
pub fn handle_regular_struct(
|
||||
schema: Schema,
|
||||
stru: &mut syn::ItemStruct,
|
||||
) -> Result<TokenStream, Error> {
|
||||
todo!();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user