From 2e63bf84220a978d866a09d47909cec213cb2609 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Tue, 7 Jan 2020 15:29:56 +0100 Subject: [PATCH] api-macro: support rename_all in enums Signed-off-by: Wolfgang Bumiller --- proxmox-api-macro/src/api/enums.rs | 7 ++++++- proxmox-api-macro/tests/types.rs | 11 ++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/proxmox-api-macro/src/api/enums.rs b/proxmox-api-macro/src/api/enums.rs index e6c506b4..946dc6c3 100644 --- a/proxmox-api-macro/src/api/enums.rs +++ b/proxmox-api-macro/src/api/enums.rs @@ -8,7 +8,7 @@ use syn::punctuated::Punctuated; use syn::Token; use super::Schema; -use crate::serde::SerdeAttrib; +use crate::serde::{self, SerdeAttrib}; use crate::util::{self, FieldName, JSONObject, JSONValue}; /// Enums, provided they're simple enums, simply get an enum string schema attached to them. @@ -40,6 +40,8 @@ pub fn handle_enum( ts }; + let container_attrs = serde::ContainerAttrib::try_from(&enum_ty.attrs[..])?; + // with_capacity(enum_ty.variants.len()); // doesn't exist O.o let mut variants = Punctuated::::new(); @@ -52,6 +54,9 @@ pub fn handle_enum( let attrs = SerdeAttrib::try_from(&variant.attrs[..])?; if let Some(renamed) = attrs.rename { variants.push(renamed.into_lit_str()); + } else if let Some(rename_all) = container_attrs.rename_all { + let name = rename_all.apply_to_variant(&variant.ident.to_string()); + variants.push(syn::LitStr::new(&name, variant.ident.span())); } else { let name = &variant.ident; variants.push(syn::LitStr::new(&name.to_string(), name.span())); diff --git a/proxmox-api-macro/tests/types.rs b/proxmox-api-macro/tests/types.rs index a491a49a..cf3fd9e9 100644 --- a/proxmox-api-macro/tests/types.rs +++ b/proxmox-api-macro/tests/types.rs @@ -37,12 +37,13 @@ impl OkString { #[api] #[derive(Deserialize)] -/// A selection of either a, B or C +#[serde(rename_all = "kebab-case")] +/// A selection of either 'onekind', 'another-kind' or 'selection-number-three'. pub enum Selection { - #[serde(rename = "a")] - A, - B, - C, + #[serde(rename = "onekind")] + OneKind, + AnotherKind, + SelectionNumberThree, } // Initial test: