use std::collections::{HashMap, HashSet};
use crate::context::CodeGenContext;
use crate::features::ResolvedFeatures;
use crate::generated::descriptor::field_descriptor_proto::{Label, Type};
use crate::generated::descriptor::{DescriptorProto, FieldDescriptorProto, FileDescriptorProto};
use crate::impl_message::{
effective_type, effective_type_in_map_entry, field_bytes_repr, find_map_entry_fields,
is_explicit_presence_scalar, is_field_packed, is_real_oneof_member, is_required_field,
map_string_repr, map_value_bytes_repr,
};
use crate::message::{find_map_entry, is_closed_enum, map_entry_key_type, map_entry_value_type};
use crate::{CodeGenError, CodeGenWarning, CodecStrategy, TableCodecFallbackReason};
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Card {
Implicit,
Required,
Optional,
Repeated,
Packed,
}
impl Card {
fn name(self) -> &'static str {
match self {
Card::Implicit => "Implicit",
Card::Required => "Required",
Card::Optional => "Optional",
Card::Repeated => "Repeated",
Card::Packed => "Packed",
}
}
}
pub(crate) struct TableField<'a> {
pub(crate) field: &'a FieldDescriptorProto,
pub(crate) number: u32,
pub(crate) shape: Shape<'a>,
}
pub(crate) enum Shape<'a> {
Map(MapField<'a>),
Plain(PlainField<'a>),
}
pub(crate) struct PlainField<'a> {
pub(crate) ty: Type,
pub(crate) card: Card,
pub(crate) kind: String,
pub(crate) closed_enum: bool,
pub(crate) oneof: Option<OneofMembership<'a>>,
}
pub(crate) struct MapField<'a> {
pub(crate) key_ty: Type,
pub(crate) val_ty: Type,
pub(crate) val_field: &'a FieldDescriptorProto,
pub(crate) closed_enum: bool,
}
pub(crate) struct OneofMembership<'a> {
pub(crate) index: usize,
pub(crate) name: &'a str,
}
pub(crate) fn type_stem(ty: Type, card: Card) -> Option<&'static str> {
Some(match ty {
Type::TYPE_INT32 => "Int32",
Type::TYPE_INT64 => "Int64",
Type::TYPE_UINT32 => "Uint32",
Type::TYPE_UINT64 => "Uint64",
Type::TYPE_SINT32 => "Sint32",
Type::TYPE_SINT64 => "Sint64",
Type::TYPE_BOOL => "Bool",
Type::TYPE_FIXED32 => "Fixed32",
Type::TYPE_FIXED64 => "Fixed64",
Type::TYPE_SFIXED32 => "Sfixed32",
Type::TYPE_SFIXED64 => "Sfixed64",
Type::TYPE_FLOAT => "Float",
Type::TYPE_DOUBLE => "Double",
Type::TYPE_STRING => "Str",
Type::TYPE_BYTES => "Bytes",
Type::TYPE_ENUM => "Enum",
Type::TYPE_MESSAGE if card == Card::Repeated => return Some("MsgRepeated"),
Type::TYPE_MESSAGE => return Some("MsgSingular"),
Type::TYPE_GROUP => return None,
})
}
#[derive(Clone, Debug)]
pub(crate) struct Ineligible {
pub(crate) reason: String,
pub(crate) detail: String,
}
fn same(reason: &str) -> Ineligible {
ineligible(reason, format!("it {reason}"))
}
pub(crate) const CUSTOM_TYPE_REASON: &str =
"has a field with a custom string, bytes or collection type";
fn map_entry_fields<'a>(
msg: &'a DescriptorProto,
f: &FieldDescriptorProto,
) -> Option<Result<(&'a FieldDescriptorProto, &'a FieldDescriptorProto), CodeGenError>> {
find_map_entry(msg, f).map(|_| find_map_entry_fields(msg, f))
}
fn ineligible(reason: impl Into<String>, detail: impl Into<String>) -> Ineligible {
Ineligible {
reason: reason.into(),
detail: detail.into(),
}
}
pub(crate) fn table_fields<'a>(
ctx: &CodeGenContext,
msg: &'a DescriptorProto,
fqn: &str,
features: &ResolvedFeatures,
) -> Result<Vec<TableField<'a>>, Ineligible> {
if ctx.zeroizes_message(fqn) {
return Err(same("requires generated recursive cleanup"));
}
if msg
.options
.as_option()
.and_then(|o| o.message_set_wire_format)
.unwrap_or(false)
{
return Err(same("uses the MessageSet wire format"));
}
if ctx.config.generate_json
&& !msg.extension_range.is_empty()
&& ctx.preserve_unknown_fields(fqn)
{
return Err(same("has extension ranges and JSON code is generated"));
}
let mut fields = Vec::with_capacity(msg.field.len());
for f in &msg.field {
let name = f.name.as_deref().unwrap_or("");
let oneof = if is_real_oneof_member(f) {
let index = f.oneof_index.and_then(|i| usize::try_from(i).ok());
let decl = index.and_then(|i| msg.oneof_decl.get(i));
let Some((index, decl)) = index.zip(decl) else {
return Err(ineligible(
"has a field in a oneof that does not exist",
format!("field `{name}` names a oneof that the message does not declare"),
));
};
Some((index, decl.name.as_deref().unwrap_or("")))
} else {
None
};
if let Some(entry) = map_entry_fields(msg, f) {
let entry =
entry.map_err(|e| ineligible("has a malformed map field", e.to_string()))?;
fields.push(map_field(ctx, f, entry, fqn, features)?);
continue;
}
let ty = effective_type(ctx, f, features);
let field_fqn = format!("{fqn}.{name}");
let repeated = f.label.unwrap_or_default() == Label::LABEL_REPEATED;
let custom = match ty {
Type::TYPE_STRING => !ctx.string_repr(&field_fqn).is_default(),
Type::TYPE_BYTES => !ctx.bytes_repr(&field_fqn).is_default(),
_ => false,
} || (repeated && !ctx.repeated_repr(&field_fqn).is_default());
if custom {
return Err(ineligible(
CUSTOM_TYPE_REASON,
format!("field `{name}` has a custom string, bytes or collection type"),
));
}
let number = crate::impl_message::validated_field_number(f)
.map_err(|e| ineligible("has an invalid field number", e.to_string()))?;
let card = if oneof.is_some() {
Card::Required
} else if repeated {
if is_field_packed(f, features) {
Card::Packed
} else {
Card::Repeated
}
} else if is_explicit_presence_scalar(f, ty, features) {
Card::Optional
} else if is_required_field(f, features) {
Card::Required
} else {
Card::Implicit
};
let closed_enum = ty == Type::TYPE_ENUM
&& is_closed_enum(&crate::features::resolve_field(ctx, f, features));
let Some(stem) = type_stem(ty, card) else {
return Err(ineligible(
"has a group field",
format!("field `{name}` is a group"),
));
};
let kind = if ty == Type::TYPE_MESSAGE {
stem.to_string()
} else {
format!("{stem}{}", card.name())
};
fields.push(TableField {
field: f,
number,
shape: Shape::Plain(PlainField {
ty,
card,
kind,
closed_enum,
oneof: oneof.map(|(index, name)| OneofMembership { index, name }),
}),
});
}
fields.sort_by_key(|f| f.number);
debug_assert!(fields.windows(2).all(|w| w[0].number < w[1].number));
Ok(fields)
}
fn map_field<'a>(
ctx: &CodeGenContext,
f: &'a FieldDescriptorProto,
(key_field, val_field): (&FieldDescriptorProto, &'a FieldDescriptorProto),
fqn: &str,
features: &ResolvedFeatures,
) -> Result<TableField<'a>, Ineligible> {
let name = f.name.as_deref().unwrap_or("");
if ctx.config.map_entries_omit_defaults {
return Err(ineligible(
"has a map field and map_entries_omit_defaults is set",
format!("map_entries_omit_defaults is set and it has map field `{name}`"),
));
}
let field_fqn = format!("{fqn}.{name}");
if matches!(ctx.map_repr(&field_fqn), crate::MapRepr::Custom(_)) {
return Err(ineligible(
CUSTOM_TYPE_REASON,
format!("map field `{name}` has a custom collection type"),
));
}
let key_ty = effective_type_in_map_entry(ctx, key_field, features);
let val_ty = effective_type_in_map_entry(ctx, val_field, features);
let proto_fqn = fqn.strip_prefix('.').unwrap_or(fqn);
let custom_string = [key_ty, val_ty]
.into_iter()
.any(|ty| !map_string_repr(ctx, ty, proto_fqn, name).is_default());
let custom_bytes =
!map_value_bytes_repr(ctx, Some(key_ty), Some(val_ty), proto_fqn, name).is_default();
if custom_string || custom_bytes {
return Err(ineligible(
CUSTOM_TYPE_REASON,
format!("map field `{name}` has a custom string, bytes or collection type"),
));
}
let number = crate::impl_message::validated_field_number(f)
.map_err(|e| ineligible("has an invalid field number", e.to_string()))?;
let closed_enum = val_ty == Type::TYPE_ENUM
&& is_closed_enum(&crate::features::resolve_field(ctx, val_field, features));
Ok(TableField {
field: f,
number,
shape: Shape::Map(MapField {
key_ty,
val_ty,
val_field,
closed_enum,
}),
})
}
struct Candidate<'a> {
fqn: String,
fields: Result<Vec<TableField<'a>>, Ineligible>,
own_bytes: bool,
holds: Vec<String>,
}
fn has_non_default_bytes(
ctx: &CodeGenContext,
msg: &DescriptorProto,
fqn: &str,
features: &ResolvedFeatures,
) -> bool {
let proto_fqn = fqn.trim_start_matches('.');
msg.field.iter().any(|f| {
let name = f.name.as_deref().unwrap_or("");
let repr = match find_map_entry(msg, f) {
Some(entry) => map_value_bytes_repr(
ctx,
map_entry_key_type(ctx, entry, features),
map_entry_value_type(ctx, entry, features),
proto_fqn,
name,
),
None if effective_type(ctx, f, features) == Type::TYPE_BYTES => {
field_bytes_repr(ctx, proto_fqn, name)
}
None => return false,
};
!repr.is_default()
})
}
fn held_messages(
ctx: &CodeGenContext,
msg: &DescriptorProto,
features: &ResolvedFeatures,
) -> Vec<String> {
msg.field
.iter()
.filter_map(|f| {
if let Some(entry) = find_map_entry(msg, f) {
if map_entry_value_type(ctx, entry, features) != Some(Type::TYPE_MESSAGE) {
return None;
}
let value = entry.field.iter().find(|v| v.number == Some(2))?;
return value.type_name.clone();
}
matches!(
effective_type(ctx, f, features),
Type::TYPE_MESSAGE | Type::TYPE_GROUP
)
.then(|| f.type_name.clone())
.flatten()
})
.collect()
}
fn collect<'a>(
ctx: &CodeGenContext,
messages: &'a [DescriptorProto],
(package, scope): (&str, &str),
parent_features: &ResolvedFeatures,
top_level: bool,
out: &mut Vec<Candidate<'a>>,
group_types: &mut HashSet<String>,
) {
for msg in messages {
let is_map_entry = msg
.options
.as_option()
.is_some_and(|o| o.map_entry.unwrap_or(false));
let fqn = format!("{scope}.{}", msg.name.as_deref().unwrap_or(""));
let features = crate::features::message_scope_features(parent_features, msg, top_level);
let is_extern = ctx
.rust_type_relative(&fqn, package, 0)
.is_some_and(|path| path.starts_with("::") || path.starts_with("crate::"));
if !is_map_entry && !is_extern {
for f in &msg.field {
if effective_type(ctx, f, &features) == Type::TYPE_GROUP {
group_types.extend(f.type_name.clone());
}
}
out.push(Candidate {
fields: table_fields(ctx, msg, &fqn, &features),
own_bytes: has_non_default_bytes(ctx, msg, &fqn, &features),
holds: held_messages(ctx, msg, &features),
fqn: fqn.clone(),
});
}
collect(
ctx,
&msg.nested_type,
(package, &fqn),
&features,
false,
out,
group_types,
);
}
}
fn holders_of_non_default_bytes<'a>(candidates: &'a [Candidate<'_>]) -> HashMap<&'a str, &'a str> {
let mut tainted: HashSet<&str> = candidates
.iter()
.filter(|c| c.own_bytes)
.map(|c| c.fqn.as_str())
.collect();
let mut holders = HashMap::new();
loop {
let before = tainted.len();
for c in candidates {
if tainted.contains(c.fqn.as_str()) {
continue;
}
if let Some(child) = c.holds.iter().find(|h| tainted.contains(h.as_str())) {
tainted.insert(&c.fqn);
holders.insert(c.fqn.as_str(), child.as_str());
}
}
if tainted.len() == before {
return holders;
}
}
}
#[derive(Default)]
pub(crate) struct TablePlan {
tables: HashSet<String>,
}
impl TablePlan {
pub(crate) fn contains(&self, fqn: &str) -> bool {
self.tables.contains(fqn)
}
}
pub(crate) fn plan(
ctx: &CodeGenContext,
files: &[FileDescriptorProto],
files_to_generate: &[String],
) -> Result<(TablePlan, Vec<CodeGenWarning>), CodeGenError> {
let mut candidates = Vec::new();
let mut group_types = HashSet::new();
for file in files.iter().filter(|f| {
f.name
.as_deref()
.is_some_and(|n| files_to_generate.iter().any(|g| g == n))
}) {
let package = file.package.as_deref().unwrap_or("");
let scope = if package.is_empty() {
String::new()
} else {
format!(".{package}")
};
collect(
ctx,
&file.message_type,
(package, &scope),
&crate::features::for_file(file),
true,
&mut candidates,
&mut group_types,
);
}
let bytes_holders = holders_of_non_default_bytes(&candidates);
let mut tables = HashSet::new();
let mut selected = 0;
let mut errors = Vec::new();
let mut summary: Vec<TableCodecFallbackReason> = Vec::new();
let mut fallbacks = 0;
for c in &candidates {
if ctx.codec_strategy(&c.fqn) != CodecStrategy::Table {
continue;
}
selected += 1;
let why = match &c.fields {
Err(why) => why.clone(),
Ok(_) if group_types.contains(&c.fqn) => same("is the type of a group field"),
Ok(_) if bytes_holders.contains_key(c.fqn.as_str()) => ineligible(
"holds a message with bytes fields of a non-default type",
format!(
"it holds `{}`, which has bytes fields of a non-default type or holds a \
message that has",
bytes_holders[c.fqn.as_str()]
),
),
Ok(_) => {
tables.insert(c.fqn.clone());
continue;
}
};
if let Some((rule, _)) = ctx.codec_strategy_rule(&c.fqn) {
if *rule == c.fqn {
errors.push(format!(
"codec_strategy_in rule '{rule}' selects the table codec for a message that \
cannot use it: {}. Select the unrolled codec for it instead (buffa-build: \
`.codec_strategy_in(CodecStrategy::Unrolled, &[\"{rule}\"])`; plugin: \
`codec_strategy_in={rule}=unrolled`), or remove the rule",
why.detail
));
continue;
}
}
fallbacks += 1;
let entry = match summary.iter().position(|r| r.reason == why.reason) {
Some(index) => &mut summary[index],
None => {
summary.push(TableCodecFallbackReason {
reason: why.reason.clone(),
messages: Vec::new(),
});
summary.last_mut().expect("just pushed")
}
};
entry.messages.push(c.fqn.clone());
}
if !errors.is_empty() {
return Err(CodeGenError::Other(errors.join("\n")));
}
let mut warnings = Vec::new();
if fallbacks > 0 {
summary.sort_by_key(|reason| std::cmp::Reverse(reason.messages.len()));
warnings.push(CodeGenWarning::TableCodecFallbackSummary {
fallbacks,
selected,
reasons: summary,
});
}
Ok((TablePlan { tables }, warnings))
}