mirror of
https://git.proxmox.com/git/proxmox
synced 2025-08-14 12:52:55 +00:00
api-macro: support rename_all in enums
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
dd5cd3f311
commit
2e63bf8422
@ -8,7 +8,7 @@ use syn::punctuated::Punctuated;
|
|||||||
use syn::Token;
|
use syn::Token;
|
||||||
|
|
||||||
use super::Schema;
|
use super::Schema;
|
||||||
use crate::serde::SerdeAttrib;
|
use crate::serde::{self, SerdeAttrib};
|
||||||
use crate::util::{self, FieldName, JSONObject, JSONValue};
|
use crate::util::{self, FieldName, JSONObject, JSONValue};
|
||||||
|
|
||||||
/// Enums, provided they're simple enums, simply get an enum string schema attached to them.
|
/// Enums, provided they're simple enums, simply get an enum string schema attached to them.
|
||||||
@ -40,6 +40,8 @@ pub fn handle_enum(
|
|||||||
ts
|
ts
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let container_attrs = serde::ContainerAttrib::try_from(&enum_ty.attrs[..])?;
|
||||||
|
|
||||||
// with_capacity(enum_ty.variants.len());
|
// with_capacity(enum_ty.variants.len());
|
||||||
// doesn't exist O.o
|
// doesn't exist O.o
|
||||||
let mut variants = Punctuated::<syn::LitStr, Token![,]>::new();
|
let mut variants = Punctuated::<syn::LitStr, Token![,]>::new();
|
||||||
@ -52,6 +54,9 @@ pub fn handle_enum(
|
|||||||
let attrs = SerdeAttrib::try_from(&variant.attrs[..])?;
|
let attrs = SerdeAttrib::try_from(&variant.attrs[..])?;
|
||||||
if let Some(renamed) = attrs.rename {
|
if let Some(renamed) = attrs.rename {
|
||||||
variants.push(renamed.into_lit_str());
|
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 {
|
} else {
|
||||||
let name = &variant.ident;
|
let name = &variant.ident;
|
||||||
variants.push(syn::LitStr::new(&name.to_string(), name.span()));
|
variants.push(syn::LitStr::new(&name.to_string(), name.span()));
|
||||||
|
@ -37,12 +37,13 @@ impl OkString {
|
|||||||
|
|
||||||
#[api]
|
#[api]
|
||||||
#[derive(Deserialize)]
|
#[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 {
|
pub enum Selection {
|
||||||
#[serde(rename = "a")]
|
#[serde(rename = "onekind")]
|
||||||
A,
|
OneKind,
|
||||||
B,
|
AnotherKind,
|
||||||
C,
|
SelectionNumberThree,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initial test:
|
// Initial test:
|
||||||
|
Loading…
Reference in New Issue
Block a user