use std::collections::HashMap;
use crate::generated::descriptor::EnumDescriptorProto;
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote};
use crate::context::CodeGenContext;
use crate::features::ResolvedFeatures;
use crate::CodeGenError;
fn generate_enum_serde(name_ident: &Ident) -> TokenStream {
quote! {
impl ::serde::Serialize for #name_ident {
fn serialize<__S: ::serde::Serializer>(&self, s: __S) -> ::core::result::Result<__S::Ok, __S::Error> {
s.serialize_str(::buffa::Enumeration::proto_name(self))
}
}
impl<'de> ::serde::Deserialize<'de> for #name_ident {
fn deserialize<__D: ::serde::Deserializer<'de>>(d: __D) -> ::core::result::Result<Self, __D::Error> {
::buffa::json_helpers::closed_enum::deserialize_strict(d)
}
}
impl ::buffa::json_helpers::ProtoElemJson for #name_ident {
fn serialize_proto_json<__S: ::serde::Serializer>(
v: &Self,
s: __S,
) -> ::core::result::Result<__S::Ok, __S::Error> {
::serde::Serialize::serialize(v, s)
}
fn deserialize_proto_json<'de, __D: ::serde::Deserializer<'de>>(
d: __D,
) -> ::core::result::Result<Self, __D::Error> {
<Self as ::serde::Deserialize>::deserialize(d)
}
}
}
}
fn is_deprecated_value(value: &crate::generated::descriptor::EnumValueDescriptorProto) -> bool {
value
.options
.as_option()
.and_then(|o| o.deprecated)
.unwrap_or(false)
}
pub(crate) fn deprecated_items(enum_desc: &EnumDescriptorProto) -> Vec<bool> {
let mut variant_deprecated: HashMap<i32, bool> = HashMap::new();
enum_desc
.value
.iter()
.map(|v| {
let own = is_deprecated_value(v);
let inherited = v
.number
.is_some_and(|number| *variant_deprecated.entry(number).or_insert(own));
own || inherited
})
.collect()
}
pub fn generate_enum(
ctx: &CodeGenContext,
enum_desc: &EnumDescriptorProto,
rust_name: &str,
proto_fqn: &str,
features: &ResolvedFeatures,
_resolver: &crate::imports::ImportResolver,
) -> Result<TokenStream, CodeGenError> {
let name_ident = format_ident!("{}", rust_name);
let enum_features =
crate::features::resolve_child(features, crate::features::enum_features(enum_desc));
let mut seen: HashMap<i32, &str> = HashMap::new();
let mut variants = Vec::new();
let mut alias_consts = Vec::new();
let mut from_i32_arms = Vec::new();
let mut from_proto_name_arms: Vec<TokenStream> = Vec::new();
let mut proto_name_arms = Vec::new();
let mut value_idents: Vec<Ident> = Vec::new();
let mut first_variant: Option<Ident> = None;
let mut value_records: Vec<(String, Ident, String)> = Vec::new();
let item_deprecated = deprecated_items(enum_desc);
let mut has_deprecated_variant = false;
let mut deprecated_idents: std::collections::HashSet<String> = std::collections::HashSet::new();
for (v, &deprecated) in enum_desc.value.iter().zip(&item_deprecated) {
let value_name = v
.name
.as_deref()
.ok_or(CodeGenError::MissingField("enum_value.name"))?;
let number = v
.number
.ok_or(CodeGenError::MissingField("enum_value.number"))?;
let variant_ident = crate::message::make_field_ident(value_name);
let value_fqn = format!("{}.{}", proto_fqn, value_name);
let variant_doc =
crate::comments::doc_attrs_resolved(ctx.comment(&value_fqn), proto_fqn, &ctx.type_map);
let deprecated_attr = deprecated.then(|| quote! { #[deprecated] });
if deprecated {
deprecated_idents.insert(variant_ident.to_string());
}
if let Some(&primary_name) = seen.get(&number) {
let primary_ident = crate::message::make_field_ident(primary_name);
alias_consts.push(quote! {
#variant_doc
#deprecated_attr
#[allow(non_upper_case_globals)]
pub const #variant_ident: Self = Self::#primary_ident;
});
from_proto_name_arms.push(quote! {
#value_name => ::core::option::Option::Some(Self::#primary_ident)
});
if ctx.config.idiomatic_enum_aliases {
value_records.push((
value_name.to_string(),
primary_ident,
variant_ident.to_string(),
));
}
} else {
seen.insert(number, value_name);
has_deprecated_variant |= deprecated;
if first_variant.is_none() {
first_variant = Some(variant_ident.clone());
}
variants.push(quote! { #variant_doc #deprecated_attr #variant_ident = #number });
from_i32_arms.push(quote! {
#number => ::core::option::Option::Some(Self::#variant_ident)
});
from_proto_name_arms.push(quote! {
#value_name => ::core::option::Option::Some(Self::#variant_ident)
});
proto_name_arms.push(quote! {
Self::#variant_ident => #value_name
});
if ctx.config.idiomatic_enum_aliases {
value_records.push((
value_name.to_string(),
variant_ident.clone(),
variant_ident.to_string(),
));
}
value_idents.push(variant_ident);
}
}
let enum_simple_name = enum_desc.name.as_deref().unwrap_or(rust_name);
let (idiomatic_consts, idiomatic_doc_note) = idiomatic_aliases(
ctx,
rust_name,
enum_simple_name,
value_records,
&deprecated_idents,
);
let deprecated_value_allow = has_deprecated_variant.then(|| quote! { #[allow(deprecated)] });
let alias_block = if alias_consts.is_empty() && idiomatic_consts.is_empty() {
quote! {}
} else {
quote! {
#deprecated_value_allow
impl #name_ident {
#(#alias_consts)*
#(#idiomatic_consts)*
}
}
};
let default_variant = first_variant;
let first_variant_deprecated = default_variant
.as_ref()
.is_some_and(|v| deprecated_idents.contains(&v.to_string()))
.then(|| quote! { #[allow(deprecated)] });
let default_block = match default_variant {
Some(v) => quote! {
#first_variant_deprecated
impl ::core::default::Default for #name_ident {
fn default() -> Self {
Self::#v
}
}
},
None => quote! {},
};
let serde_impls = if ctx.config.generate_json {
crate::feature_gates::cfg_const_block(
generate_enum_serde(&name_ident),
ctx.config.feature_gates().json,
)
} else {
quote! {}
};
let (arbitrary_derive, arbitrary_impl) =
match (ctx.config.generate_arbitrary, has_deprecated_variant) {
(false, _) => (quote! {}, quote! {}),
(true, false) => (
quote! { #[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))] },
quote! {},
),
(true, true) => (
quote! {},
quote! {
#[cfg(feature = "arbitrary")]
impl<'a> ::arbitrary::Arbitrary<'a> for #name_ident {
fn arbitrary(
u: &mut ::arbitrary::Unstructured<'a>,
) -> ::arbitrary::Result<Self> {
let values = <Self as ::buffa::Enumeration>::values();
let draw = <u32 as ::arbitrary::Arbitrary>::arbitrary(u)?;
let index = (u64::from(draw) * values.len() as u64) >> 32;
::core::result::Result::Ok(values[index as usize])
}
fn size_hint(depth: usize) -> (usize, ::core::option::Option<usize>) {
<u32 as ::arbitrary::Arbitrary>::size_hint(depth)
}
}
},
),
};
let reflect_element_impl = if ctx.config.generate_reflection
&& ctx.config.generate_reflection_vtable
&& crate::message::is_closed_enum(&enum_features)
{
crate::feature_gates::cfg_block(
quote! {
impl ::buffa_descriptor::reflect::ReflectElement for #name_ident {
fn as_value_ref(&self) -> ::buffa_descriptor::reflect::ValueRef<'_> {
::buffa_descriptor::reflect::ValueRef::EnumNumber(
::buffa::Enumeration::to_i32(self),
)
}
}
},
ctx.config.feature_gates().reflect,
)
} else {
quote! {}
};
let enum_doc = {
let base =
crate::comments::doc_attrs_resolved(ctx.comment(proto_fqn), proto_fqn, &ctx.type_map);
quote! { #base #idiomatic_doc_note }
};
let custom_type_attrs = crate::context::CodeGenContext::matching_attributes(
&ctx.config.type_attributes,
proto_fqn,
)?;
let custom_enum_attrs = crate::context::CodeGenContext::matching_attributes(
&ctx.config.enum_attributes,
proto_fqn,
)?;
let derives = if ctx.skip_debug_enum(proto_fqn) {
quote! { #[derive(Clone, Copy, PartialEq, Eq, Hash)] }
} else {
quote! { #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] }
};
let zeroize_impl = if ctx.config.generate_zeroize || !ctx.config.zeroize_messages.is_empty() {
quote! { impl ::buffa::sensitive::Zeroize for #name_ident {
fn zeroize(&mut self) { ::buffa::sensitive::reset_copy(self); }
} }
} else {
quote! {}
};
let inspect_impl = if ctx.config.generate_reflection
&& (ctx.config.generate_zeroize || !ctx.config.zeroize_messages.is_empty())
&& crate::message::is_closed_enum(&enum_features)
{
crate::feature_gates::cfg_block(
quote! {
impl ::buffa_descriptor::reflect::InspectElement for #name_ident {
fn as_inspect_value(&self) -> ::buffa_descriptor::reflect::InspectValueRef<'_> {
::buffa_descriptor::reflect::InspectValueRef::EnumNumber(
::buffa::Enumeration::to_i32(self),
)
}
}
},
ctx.config.feature_gates().reflect,
)
} else {
quote! {}
};
Ok(quote! {
#enum_doc
#[allow(non_camel_case_types)]
#derives
#arbitrary_derive
#custom_type_attrs
#custom_enum_attrs
#[repr(i32)]
pub enum #name_ident {
#(#variants,)*
}
#alias_block
#default_block
#zeroize_impl
#inspect_impl
#serde_impls
#deprecated_value_allow
impl ::buffa::Enumeration for #name_ident {
fn from_i32(value: i32) -> ::core::option::Option<Self> {
match value {
#(#from_i32_arms,)*
_ => ::core::option::Option::None,
}
}
fn to_i32(&self) -> i32 {
*self as i32
}
fn proto_name(&self) -> &'static str {
match self {
#(#proto_name_arms,)*
}
}
fn from_proto_name(name: &str) -> ::core::option::Option<Self> {
match name {
#(#from_proto_name_arms,)*
_ => ::core::option::Option::None,
}
}
fn values() -> &'static [Self] {
&[#(Self::#value_idents),*]
}
}
#reflect_element_impl
#arbitrary_impl
})
}
fn idiomatic_aliases(
ctx: &CodeGenContext,
rust_name: &str,
enum_simple_name: &str,
records: Vec<(String, Ident, String)>,
deprecated_idents: &std::collections::HashSet<String>,
) -> (Vec<TokenStream>, TokenStream) {
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fmt::Write;
if records.is_empty() {
return (Vec::new(), quote! {});
}
let is_valid = |c: &str| !c.is_empty() && !c.starts_with(|ch: char| ch.is_ascii_digit());
let prefix = format!("{}_", crate::idents::to_shouty_snake_case(enum_simple_name));
let strip = records.iter().all(|(name, ..)| {
name.strip_prefix(&prefix)
.is_some_and(|base| is_valid(&crate::idents::to_upper_camel_case(base)))
});
let camel = |name: &str| {
let base = if strip {
name.strip_prefix(&prefix).unwrap_or(name)
} else {
name
};
crate::idents::to_upper_camel_case(base)
};
let existing: HashSet<String> = records.iter().map(|(_, _, own)| own.clone()).collect();
let mut buckets: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
let mut invalid: BTreeSet<String> = BTreeSet::new();
{
let owner: HashMap<&str, &str> = records
.iter()
.map(|(name, _, own)| (own.as_str(), name.as_str()))
.collect();
for (name, _, _) in &records {
let candidate = camel(name);
if !is_valid(&candidate) {
invalid.insert(name.clone());
continue;
}
let escaped = crate::idents::make_field_ident(&candidate).to_string();
if let Some(&existing_owner) = owner.get(escaped.as_str()) {
buckets
.entry(escaped.clone())
.or_default()
.insert(existing_owner.to_string());
}
buckets.entry(escaped).or_default().insert(name.clone());
}
}
let conflicts: Vec<(&String, &BTreeSet<String>)> = buckets
.iter()
.filter(|(_, claimants)| claimants.len() > 1)
.collect();
if conflicts.is_empty() && invalid.is_empty() {
let consts = records
.into_iter()
.filter_map(|(name, target, own)| {
let escaped = crate::idents::make_field_ident(&camel(&name));
if existing.contains(&escaped.to_string()) {
return None;
}
let target_name = target.to_string();
let deprecated_attr = deprecated_idents
.contains(&own)
.then(|| quote! { #[deprecated] });
let alias_doc = if let Some(stripped) = target_name.strip_prefix("r#") {
format!("Idiomatic alias for `{stripped}`; `Debug` prints the variant name.")
} else {
format!(
"Idiomatic alias for [`Self::{target_name}`]; `Debug` prints the variant name."
)
};
Some(quote! {
#[doc = #alias_doc]
#deprecated_attr
#[allow(non_upper_case_globals)]
pub const #escaped: Self = Self::#target;
})
})
.collect();
return (consts, quote! {});
}
let conflict_data: Vec<crate::AliasConflict> = conflicts
.iter()
.map(|(camel_ident, claimants)| crate::AliasConflict {
camel_target: (*camel_ident).clone(),
proto_values: claimants.iter().cloned().collect(),
})
.collect();
let invalid_data: Vec<String> = invalid.into_iter().collect();
let mut note = String::from(
"Idiomatic CamelCase aliases are not generated for this enum: two or more proto values \
collide after conversion (or would be invalid identifiers). Use the `SHOUTY_SNAKE_CASE` \
variants directly. Collisions:\n",
);
for conflict in &conflict_data {
let joined = conflict
.proto_values
.iter()
.map(|n| format!("`{n}`"))
.collect::<Vec<_>>()
.join(", ");
let _ = writeln!(note, "- {joined} → `{}`", conflict.camel_target);
}
for name in &invalid_data {
let _ = writeln!(note, "- `{name}` produces an invalid identifier");
}
ctx.warn(crate::CodeGenWarning::IdiomaticAliasesSuppressed {
enum_name: rust_name.to_string(),
conflicts: conflict_data,
invalid: invalid_data,
});
(Vec::new(), quote! { #[doc = #note] })
}