mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:50:52 +00:00
register experimental app-server enum variants
Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
co-authored by
Codex
parent
1f150eda8b
commit
c9ef76fde6
@@ -13,6 +13,13 @@ use syn::LitStr;
|
||||
use syn::Type;
|
||||
use syn::parse_macro_input;
|
||||
|
||||
#[derive(Default)]
|
||||
struct EnumSerdeConfig {
|
||||
rename_all: Option<String>,
|
||||
tag: Option<String>,
|
||||
untagged: bool,
|
||||
}
|
||||
|
||||
#[proc_macro_derive(ExperimentalApi, attributes(experimental))]
|
||||
pub fn derive_experimental_api(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as DeriveInput);
|
||||
@@ -141,7 +148,10 @@ fn derive_for_struct(input: &DeriveInput, data: &DataStruct) -> TokenStream {
|
||||
|
||||
fn derive_for_enum(input: &DeriveInput, data: &DataEnum) -> TokenStream {
|
||||
let name = &input.ident;
|
||||
let type_name_lit = LitStr::new(&name.to_string(), Span::call_site());
|
||||
let serde_config = enum_serde_config(&input.attrs);
|
||||
let mut match_arms = Vec::new();
|
||||
let mut registrations = Vec::new();
|
||||
|
||||
for variant in &data.variants {
|
||||
let variant_name = &variant.ident;
|
||||
@@ -155,6 +165,14 @@ fn derive_for_enum(input: &DeriveInput, data: &DataEnum) -> TokenStream {
|
||||
match_arms.push(quote! {
|
||||
#pattern => Some(#reason),
|
||||
});
|
||||
if let Some(registration) = experimental_enum_variant_registration(
|
||||
&type_name_lit,
|
||||
&serde_config,
|
||||
variant,
|
||||
&reason,
|
||||
) {
|
||||
registrations.push(registration);
|
||||
}
|
||||
} else {
|
||||
match_arms.push(quote! {
|
||||
#pattern => None,
|
||||
@@ -163,6 +181,8 @@ fn derive_for_enum(input: &DeriveInput, data: &DataEnum) -> TokenStream {
|
||||
}
|
||||
|
||||
let expanded = quote! {
|
||||
#(#registrations)*
|
||||
|
||||
impl crate::experimental_api::ExperimentalApi for #name {
|
||||
fn experimental_reason(&self) -> Option<&'static str> {
|
||||
match self {
|
||||
@@ -181,12 +201,160 @@ fn experimental_reason(attrs: &[Attribute]) -> Option<LitStr> {
|
||||
attr.parse_args::<LitStr>().ok()
|
||||
}
|
||||
|
||||
fn enum_serde_config(attrs: &[Attribute]) -> EnumSerdeConfig {
|
||||
let mut config = EnumSerdeConfig::default();
|
||||
for attr in attrs.iter().filter(|attr| attr.path().is_ident("serde")) {
|
||||
let _ = attr.parse_nested_meta(|meta| {
|
||||
if meta.path.is_ident("rename_all") {
|
||||
config.rename_all = Some(meta.value()?.parse::<LitStr>()?.value());
|
||||
} else if meta.path.is_ident("tag") {
|
||||
config.tag = Some(meta.value()?.parse::<LitStr>()?.value());
|
||||
} else if meta.path.is_ident("untagged") {
|
||||
config.untagged = true;
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
config
|
||||
}
|
||||
|
||||
fn variant_serialized_name(variant: &syn::Variant, rename_all: Option<&str>) -> String {
|
||||
if let Some(rename) = serde_rename(&variant.attrs) {
|
||||
return rename;
|
||||
}
|
||||
apply_rename_all(&variant.ident.to_string(), rename_all)
|
||||
}
|
||||
|
||||
fn serde_rename(attrs: &[Attribute]) -> Option<String> {
|
||||
let mut rename = None;
|
||||
for attr in attrs.iter().filter(|attr| attr.path().is_ident("serde")) {
|
||||
let _ = attr.parse_nested_meta(|meta| {
|
||||
if meta.path.is_ident("rename") {
|
||||
rename = Some(meta.value()?.parse::<LitStr>()?.value());
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
rename
|
||||
}
|
||||
|
||||
fn experimental_enum_variant_registration(
|
||||
type_name_lit: &LitStr,
|
||||
serde_config: &EnumSerdeConfig,
|
||||
variant: &syn::Variant,
|
||||
reason: &LitStr,
|
||||
) -> Option<proc_macro2::TokenStream> {
|
||||
if serde_config.untagged {
|
||||
return None;
|
||||
}
|
||||
|
||||
let serialized_name = variant_serialized_name(variant, serde_config.rename_all.as_deref());
|
||||
let serialized_name_lit = LitStr::new(&serialized_name, Span::call_site());
|
||||
let encoding = if let Some(tag_name) = serde_config.tag.as_deref() {
|
||||
let tag_name_lit = LitStr::new(tag_name, Span::call_site());
|
||||
quote! {
|
||||
crate::experimental_api::ExperimentalEnumVariantEncoding::TaggedObject {
|
||||
tag_name: #tag_name_lit,
|
||||
}
|
||||
}
|
||||
} else if matches!(variant.fields, Fields::Unit) {
|
||||
quote! {
|
||||
crate::experimental_api::ExperimentalEnumVariantEncoding::StringLiteral
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
crate::experimental_api::ExperimentalEnumVariantEncoding::ExternallyTaggedObject
|
||||
}
|
||||
};
|
||||
|
||||
Some(quote! {
|
||||
::inventory::submit! {
|
||||
crate::experimental_api::ExperimentalEnumVariant {
|
||||
type_name: #type_name_lit,
|
||||
serialized_name: #serialized_name_lit,
|
||||
reason: #reason,
|
||||
encoding: #encoding,
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn field_serialized_name(field: &Field) -> Option<String> {
|
||||
let ident = field.ident.as_ref()?;
|
||||
let name = ident.to_string();
|
||||
Some(snake_to_camel(&name))
|
||||
}
|
||||
|
||||
fn apply_rename_all(name: &str, rename_all: Option<&str>) -> String {
|
||||
let words = split_words(name);
|
||||
match rename_all {
|
||||
Some("camelCase") => {
|
||||
let mut out = String::new();
|
||||
for (index, word) in words.iter().enumerate() {
|
||||
if index == 0 {
|
||||
out.push_str(word);
|
||||
} else {
|
||||
out.push_str(&capitalize(word));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
Some("snake_case") => words.join("_"),
|
||||
Some("kebab-case") => words.join("-"),
|
||||
Some("PascalCase") => words
|
||||
.iter()
|
||||
.map(|word| capitalize(word))
|
||||
.collect::<Vec<_>>()
|
||||
.join(""),
|
||||
Some("SCREAMING_SNAKE_CASE") => words.join("_").to_ascii_uppercase(),
|
||||
Some("UPPERCASE") => words.concat().to_ascii_uppercase(),
|
||||
Some("lowercase") => words.concat(),
|
||||
Some(_) | None => name.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn split_words(name: &str) -> Vec<String> {
|
||||
let mut words = Vec::new();
|
||||
let mut current = String::new();
|
||||
let chars: Vec<char> = name.chars().collect();
|
||||
for (index, ch) in chars.iter().copied().enumerate() {
|
||||
if ch == '_' || ch == '-' {
|
||||
if !current.is_empty() {
|
||||
words.push(std::mem::take(&mut current));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
let previous = chars.get(index.wrapping_sub(1)).copied();
|
||||
let next = chars.get(index + 1).copied();
|
||||
let boundary_before = previous.is_some_and(|previous| {
|
||||
(previous.is_ascii_lowercase() && ch.is_ascii_uppercase())
|
||||
|| (previous.is_ascii_uppercase()
|
||||
&& ch.is_ascii_uppercase()
|
||||
&& next.is_some_and(|next| next.is_ascii_lowercase()))
|
||||
});
|
||||
if boundary_before && !current.is_empty() {
|
||||
words.push(std::mem::take(&mut current));
|
||||
}
|
||||
current.push(ch.to_ascii_lowercase());
|
||||
}
|
||||
if !current.is_empty() {
|
||||
words.push(current);
|
||||
}
|
||||
words
|
||||
}
|
||||
|
||||
fn capitalize(word: &str) -> String {
|
||||
let mut chars = word.chars();
|
||||
let Some(first) = chars.next() else {
|
||||
return String::new();
|
||||
};
|
||||
let mut out = String::new();
|
||||
out.push(first.to_ascii_uppercase());
|
||||
out.extend(chars);
|
||||
out
|
||||
}
|
||||
|
||||
fn snake_to_camel(s: &str) -> String {
|
||||
let mut out = String::with_capacity(s.len());
|
||||
let mut upper = false;
|
||||
|
||||
Reference in New Issue
Block a user