]> git.proxmox.com Git - proxmox.git/commitdiff
macro: refactoring
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Tue, 5 Jan 2021 12:49:32 +0000 (13:49 +0100)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Tue, 5 Jan 2021 12:50:55 +0000 (13:50 +0100)
purely non-functional changes

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
proxmox-api-macro/src/api/structs.rs

index e55378d5a37e106540a2304bfa13b3d1b87ecf00..bcadf05a442d4b469b695b491c9248e2de3ca70c 100644 (file)
@@ -246,63 +246,73 @@ fn handle_regular_struct(attribs: JSONObject, stru: syn::ItemStruct) -> Result<T
     if all_of_schemas.is_empty() {
         finish_schema(schema, &stru, &stru.ident)
     } else {
-        let name = &stru.ident;
-
-        // take out the inner object schema's description
-        let description = match schema.description.take().ok() {
-            Some(description) => description,
-            None => {
-                error!(schema.span, "missing description on api type struct");
-                syn::LitStr::new("<missing description>", schema.span)
-            }
-        };
-        // and replace it with a "dummy"
-        schema.description = Maybe::Derived(syn::LitStr::new(
-            &format!("<INNER: {}>", description.value()),
-            description.span(),
-        ));
-
-        // now check if it even has any fields
-        let has_fields = match &schema.item {
-            api::SchemaItem::Object(obj) => !obj.is_empty(),
-            _ => panic!("object schema is not an object schema?"),
-        };
+        finish_all_of_struct(stru, schema, all_of_schemas)
+    }
+}
 
-        let (inner_schema, inner_schema_ref) = if has_fields {
-            // if it does, we need to create an "inner" schema to merge into the AllOf schema
-            let obj_schema = {
-                let mut ts = TokenStream::new();
-                schema.to_schema(&mut ts)?;
-                ts
-            };
+/// If we have flattened fields the struct schema is not the "final" schema, but part of an AllOf
+/// schema containing it and all the flattened field schemas.
+fn finish_all_of_struct(
+    stru: syn::ItemStruct,
+    mut schema: Schema,
+    all_of_schemas: TokenStream,
+) -> Result<TokenStream, Error> {
+    let name = &stru.ident;
+
+    // take out the inner object schema's description
+    let description = match schema.description.take().ok() {
+        Some(description) => description,
+        None => {
+            error!(schema.span, "missing description on api type struct");
+            syn::LitStr::new("<missing description>", schema.span)
+        }
+    };
+    // and replace it with a "dummy"
+    schema.description = Maybe::Derived(syn::LitStr::new(
+        &format!("<INNER: {}>", description.value()),
+        description.span(),
+    ));
+
+    // now check if it even has any fields
+    let has_fields = match &schema.item {
+        api::SchemaItem::Object(obj) => !obj.is_empty(),
+        _ => panic!("object schema is not an object schema?"),
+    };
 
-            (
-                quote_spanned!(name.span() =>
-                    const INNER_API_SCHEMA: ::proxmox::api::schema::Schema = #obj_schema;
-                ),
-                quote_spanned!(name.span() => &Self::INNER_API_SCHEMA,),
-            )
-        } else {
-            // otherwise it stays empty
-            (TokenStream::new(), TokenStream::new())
+    let (inner_schema, inner_schema_ref) = if has_fields {
+        // if it does, we need to create an "inner" schema to merge into the AllOf schema
+        let obj_schema = {
+            let mut ts = TokenStream::new();
+            schema.to_schema(&mut ts)?;
+            ts
         };
 
-        Ok(quote_spanned!(name.span() =>
-            #stru
-            impl #name {
-                #inner_schema
-                pub const API_SCHEMA: ::proxmox::api::schema::Schema =
-                    ::proxmox::api::schema::AllOfSchema::new(
-                        #description,
-                        &[
-                            #inner_schema_ref
-                            #all_of_schemas
-                        ],
-                    )
-                    .schema();
-            }
-        ))
-    }
+        (
+            quote_spanned!(name.span() =>
+                const INNER_API_SCHEMA: ::proxmox::api::schema::Schema = #obj_schema;
+            ),
+            quote_spanned!(name.span() => &Self::INNER_API_SCHEMA,),
+        )
+    } else {
+        // otherwise it stays empty
+        (TokenStream::new(), TokenStream::new())
+    };
+
+    Ok(quote_spanned!(name.span() =>
+        #stru
+        impl #name {
+            #inner_schema
+            pub const API_SCHEMA: ::proxmox::api::schema::Schema =
+                ::proxmox::api::schema::AllOfSchema::new(
+                    #description,
+                    &[
+                        #inner_schema_ref
+                        #all_of_schemas
+                    ],
+                )
+                .schema();
+        }
+    ))
 }
 
 /// Field handling: