use buffa::Message;
use buffa_codegen::generated::descriptor::FileDescriptorSet;
use buffa_codegen::{CodeGenConfig, GeneratedFile};
use std::path::Path;
use std::process::Command;
const PROTOS_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../buffa-test/protos");
fn dedent(s: &str) -> String {
let lines: Vec<&str> = s.lines().collect();
let min_indent = lines
.iter()
.filter(|l| !l.trim().is_empty())
.map(|l| l.len() - l.trim_start().len())
.min()
.unwrap_or(0);
lines
.iter()
.map(|l| {
if l.len() >= min_indent {
&l[min_indent..]
} else {
l.trim()
}
})
.collect::<Vec<_>>()
.join("\n")
}
fn compile_protos(files: &[&str], includes: &[&str]) -> FileDescriptorSet {
let protoc = std::env::var("PROTOC").unwrap_or_else(|_| "protoc".to_string());
let tmp = tempfile::NamedTempFile::new().expect("failed to create temp file");
let descriptor_path = tmp.path().to_path_buf();
let mut cmd = Command::new(&protoc);
cmd.arg("--include_imports");
cmd.arg(format!(
"--descriptor_set_out={}",
descriptor_path.display()
));
for include in includes {
cmd.arg(format!("--proto_path={include}"));
}
for file in files {
cmd.arg(file);
}
let output = cmd.output().unwrap_or_else(|e| {
panic!("protoc not found ({e}). Install protoc to run codegen integration tests.")
});
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
panic!("protoc failed: {stderr}");
}
let bytes = std::fs::read(&descriptor_path).expect("failed to read descriptor set");
FileDescriptorSet::decode_from_slice(&bytes).expect("failed to decode descriptors")
}
fn generate_proto(proto: &str, config: &CodeGenConfig) -> String {
let proto = dedent(proto);
let dir = tempfile::tempdir().expect("failed to create temp dir");
let proto_path = dir.path().join("test.proto");
std::fs::write(&proto_path, &proto).expect("failed to write proto");
let fds = compile_protos(
&[proto_path.to_str().unwrap()],
&[dir.path().to_str().unwrap()],
);
let files = buffa_codegen::generate(&fds.file, &["test.proto".into()], config)
.unwrap_or_else(|e| panic!("codegen failed: {e}\n\nProto:\n{proto}"));
files
.into_iter()
.map(|f| f.content)
.collect::<Vec<_>>()
.join("\n")
}
fn generate_for(proto_file: &str, config: &CodeGenConfig) -> Vec<GeneratedFile> {
let proto_path = Path::new(PROTOS_DIR).join(proto_file);
let fds = compile_protos(&[proto_path.to_str().unwrap()], &[PROTOS_DIR]);
let files_to_generate = vec![proto_file.to_string()];
buffa_codegen::generate(&fds.file, &files_to_generate, config)
.unwrap_or_else(|e| panic!("codegen failed for {proto_file}: {e}"))
}
fn no_views() -> CodeGenConfig {
let mut c = CodeGenConfig::default();
c.generate_views = false;
c
}
fn json_no_views() -> CodeGenConfig {
let mut c = CodeGenConfig::default();
c.generate_views = false;
c.generate_json = true;
c
}
fn json_with_views() -> CodeGenConfig {
let mut c = CodeGenConfig::default();
c.generate_json = true;
c
}
fn struct_body<'a>(content: &'a str, name: &str) -> &'a str {
let needle = format!("pub struct {name}");
let mut search = 0;
loop {
let rel = content[search..]
.find(&needle)
.unwrap_or_else(|| panic!("missing `{needle}` in:\n{content}"));
let abs = search + rel;
let after_name = abs + needle.len();
let rest = &content[after_name..];
if rest
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphanumeric() || c == '_')
{
search = after_name;
continue;
}
let brace = rest
.find('{')
.unwrap_or_else(|| panic!("no `{{` after `{needle}` in:\n{content}"));
let body_start = after_name + brace + 1;
let bytes = content.as_bytes();
let mut depth = 1usize;
for (i, &b) in bytes.iter().enumerate().skip(body_start) {
match b {
b'{' => depth += 1,
b'}' => {
depth -= 1;
if depth == 0 {
return &content[body_start..i];
}
}
_ => {}
}
}
panic!("unclosed `{needle}` in:\n{content}");
}
}
fn struct_has_unknown_fields(content: &str, name: &str) -> bool {
struct_body(content, name).contains("__buffa_unknown_fields")
}
#[test]
fn codegen_basic() {
let files = generate_for("basic.proto", &CodeGenConfig::default());
assert!(!files.is_empty());
assert!(files[0].content.contains("@generated"));
}
#[test]
fn codegen_basic_views_vs_no_views() {
let total = |files: &[GeneratedFile]| files.iter().map(|f| f.content.len()).sum::<usize>();
let with_views = generate_for("basic.proto", &CodeGenConfig::default());
let without_views = generate_for("basic.proto", &no_views());
assert!(
total(&without_views) < total(&with_views),
"disabling views should produce shorter output"
);
}
#[test]
fn codegen_keywords() {
let files = generate_for("keywords.proto", &no_views());
let content = &files[0].content;
assert!(content.contains("pub mod r#type"));
assert!(content.contains("self_:"));
assert!(content.contains("r#type:"));
}
#[test]
fn codegen_proto2_defaults() {
let files = generate_for("proto2_defaults.proto", &no_views());
let content = &files[0].content;
assert!(content.contains("pub struct WithDefaults"));
assert!(content.contains("pub struct Proto2Message"));
assert!(content.contains("pub name:"));
assert!(content.contains("pub id:"));
}
#[test]
fn codegen_json_produces_serde_derives() {
let files = generate_for("json_types.proto", &json_no_views());
let content = &files[0].content;
assert!(content.contains("::serde::Serialize"));
assert!(content.contains("::serde::Deserialize"));
}
#[test]
fn codegen_json_disabled_has_no_serde() {
let files = generate_for("json_types.proto", &no_views());
assert!(!files[0].content.contains("::serde::Serialize"));
}
#[test]
fn codegen_nested_deep() {
let files = generate_for("nested_deep.proto", &no_views());
let content = &files[0].content;
assert!(content.contains("pub mod outer"));
assert!(content.contains("pub mod middle"));
assert!(content.contains("pub struct Inner"));
}
#[test]
fn codegen_wkt_auto_mapping() {
let files = generate_for("wkt_usage.proto", &no_views());
let content = &files[0].content;
assert!(content.contains("::buffa_types::google::protobuf::Timestamp"));
assert!(content.contains("::buffa_types::google::protobuf::Duration"));
}
#[test]
fn gen_wkt_types_derives_eq_and_hash_only_for_selected_owned_messages() {
let expected = [
"Any",
"Api",
"BoolValue",
"BytesValue",
"Duration",
"Empty",
"Enum",
"EnumValue",
"Field",
"FieldMask",
"Int32Value",
"Int64Value",
"Method",
"Mixin",
"Option",
"SourceContext",
"StringValue",
"Timestamp",
"Type",
"UInt32Value",
"UInt64Value",
];
let excluded = ["DoubleValue", "FloatValue", "ListValue", "Struct", "Value"];
let protos_dir = concat!(env!("CARGO_MANIFEST_DIR"), "/../buffa-types/protos");
let fds = compile_protos(
&[
"google/protobuf/any.proto",
"google/protobuf/api.proto",
"google/protobuf/duration.proto",
"google/protobuf/empty.proto",
"google/protobuf/field_mask.proto",
"google/protobuf/source_context.proto",
"google/protobuf/struct.proto",
"google/protobuf/timestamp.proto",
"google/protobuf/type.proto",
"google/protobuf/wrappers.proto",
],
&[protos_dir],
);
let dir = tempfile::tempdir().unwrap();
let descriptor_path = dir.path().join("wkt.pb");
let output_dir = dir.path().join("generated");
std::fs::write(&descriptor_path, fds.encode_to_vec()).unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_gen_wkt_types"))
.arg(&descriptor_path)
.arg(&output_dir)
.output()
.unwrap();
assert!(
output.status.success(),
"gen_wkt_types failed: {}",
String::from_utf8_lossy(&output.stderr)
);
let mut actual = Vec::new();
let mut actual_excluded = Vec::new();
for entry in std::fs::read_dir(output_dir).unwrap() {
let content = std::fs::read_to_string(entry.unwrap().path()).unwrap();
for item in syn::parse_file(&content).unwrap().items {
let syn::Item::Struct(message) = item else {
continue;
};
let name = message.ident.to_string();
let mut derives = Vec::new();
for attr in &message.attrs {
if attr.path().is_ident("derive") {
derives.extend(
attr.parse_args_with(
syn::punctuated::Punctuated::<syn::Path, syn::Token![,]>::parse_terminated,
)
.unwrap(),
);
}
}
let has_trait = |trait_name: &str| derives.iter().any(|path| path.is_ident(trait_name));
let selected = expected.contains(&name.as_str());
assert_eq!(has_trait("Eq"), selected, "Eq derive for {name}");
assert_eq!(has_trait("Hash"), selected, "Hash derive for {name}");
if !name.ends_with("View") {
assert!(has_trait("PartialEq"), "PartialEq derive for {name}");
}
if selected {
actual.push(name);
} else if excluded.contains(&name.as_str()) {
actual_excluded.push(name);
}
}
}
actual.sort();
assert_eq!(actual, expected);
actual_excluded.sort();
assert_eq!(actual_excluded, excluded);
}
#[test]
fn codegen_wkt_api_auto_mapping() {
let files = generate_for("wkt_api.proto", &CodeGenConfig::default());
let combined = files
.iter()
.map(|f| f.content.as_str())
.collect::<Vec<_>>()
.join("\n");
assert!(combined.contains("::buffa_types::google::protobuf::Api"));
assert!(combined.contains("::buffa_types::google::protobuf::Type"));
assert!(combined.contains("::buffa_types::google::protobuf::SourceContext"));
assert!(combined.contains("::buffa_types::google::protobuf::Enum"));
assert!(combined.contains("::buffa_types::google::protobuf::__buffa::view::ApiView"));
assert!(combined.contains("::buffa_types::google::protobuf::__buffa::view::TypeView"));
assert!(combined.contains("::buffa_types::google::protobuf::__buffa::view::EnumView"));
}
#[test]
fn codegen_wkt_explicit_extern_overrides_auto() {
let mut config = no_views();
config
.extern_paths
.push((".google.protobuf".into(), "::my_custom_wkts".into()));
let files = generate_for("wkt_usage.proto", &config);
let content = &files[0].content;
assert!(content.contains("::my_custom_wkts::Timestamp"));
assert!(!content.contains("::buffa_types::"));
}
#[test]
fn codegen_name_collisions() {
let files = generate_for("name_collisions.proto", &no_views());
let content = &files[0].content;
assert!(content.contains("pub struct Vec"));
assert!(content.contains("pub struct String"));
assert!(content.contains("pub struct Option"));
assert!(content.contains("::core::option::Option"));
}
#[test]
fn codegen_renames_keyword_members_that_collide_after_escaping() {
let proto = r#"
syntax = "proto2";
package test;
message FieldCollision {
optional int32 self = 1;
optional int32 self_ = 2;
optional int32 crate = 3;
optional int32 crate_ = 4;
}
message OneofCollision {
optional int32 self_ = 1;
oneof self {
string choice = 2;
}
}
"#;
for idiomatic_field_names in [false, true] {
let mut config = CodeGenConfig::default();
config.idiomatic_field_names = idiomatic_field_names;
let content = generate_proto(proto, &config);
let field_body = struct_body(&content, "FieldCollision");
assert!(field_body.contains("pub self__:"), "{field_body}");
assert!(field_body.contains("pub self_:"), "{field_body}");
assert!(field_body.contains("pub crate__:"), "{field_body}");
assert!(field_body.contains("pub crate_:"), "{field_body}");
let oneof_body = struct_body(&content, "OneofCollision");
assert!(oneof_body.contains("pub self_:"), "{oneof_body}");
assert!(oneof_body.contains("pub self__:"), "{oneof_body}");
}
}
#[test]
fn codegen_cross_package_uses_super() {
let basic_path = Path::new(PROTOS_DIR).join("basic.proto");
let nested_path = Path::new(PROTOS_DIR).join("nested_deep.proto");
let cross_path = Path::new(PROTOS_DIR).join("cross_package.proto");
let fds = compile_protos(
&[
basic_path.to_str().unwrap(),
nested_path.to_str().unwrap(),
cross_path.to_str().unwrap(),
],
&[PROTOS_DIR],
);
let files = buffa_codegen::generate(&fds.file, &["cross_package.proto".into()], &no_views())
.expect("codegen failed");
assert!(files[0].content.contains("super::"));
}
#[test]
fn module_tree_basic() {
let entries = vec![
("foo.rs", "my.pkg"),
("bar.rs", "my.pkg"),
("baz.rs", "other"),
];
let tree = buffa_codegen::generate_module_tree(
&entries,
buffa_codegen::IncludeMode::Relative(""),
false,
);
assert!(tree.contains("pub mod my"));
assert!(tree.contains("pub mod pkg"));
assert!(tree.contains("pub mod other"));
assert!(tree.contains(r#"include!("foo.rs")"#));
}
#[test]
fn module_tree_inner_allow() {
let entries = vec![("f.rs", "pkg")];
let with = buffa_codegen::generate_module_tree(
&entries,
buffa_codegen::IncludeMode::Relative(""),
true,
);
let without = buffa_codegen::generate_module_tree(
&entries,
buffa_codegen::IncludeMode::Relative(""),
false,
);
assert!(with.contains("#![allow("));
assert!(!without.contains("#![allow("));
}
#[test]
fn module_tree_keyword_escaping() {
let entries = vec![("t.rs", "google.type")];
let tree = buffa_codegen::generate_module_tree(
&entries,
buffa_codegen::IncludeMode::Relative(""),
false,
);
assert!(tree.contains("pub mod r#type"));
}
#[test]
fn inline_scalar_fields() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Scalars {
int32 id = 1;
string name = 2;
bytes data = 3;
bool active = 4;
double score = 5;
float ratio = 6;
uint32 count = 7;
uint64 big = 8;
sint32 delta = 9;
sint64 offset = 10;
fixed32 hash = 11;
fixed64 fingerprint = 12;
sfixed32 temp = 13;
sfixed64 precise = 14;
}
"#,
&no_views(),
);
assert!(content.contains("pub id: i32"));
assert!(content.contains("pub name: ::buffa::alloc::string::String"));
assert!(content.contains("pub data: ::buffa::alloc::vec::Vec<u8>"));
assert!(content.contains("pub active: bool"));
assert!(content.contains("pub score: f64"));
assert!(content.contains("pub ratio: f32"));
}
#[test]
fn inline_nested_message() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Outer {
message Inner { int32 value = 1; }
Inner child = 1;
}
"#,
&no_views(),
);
assert!(content.contains("pub struct Outer"));
assert!(content.contains("pub mod outer"));
assert!(content.contains("pub struct Inner"));
assert!(content.contains("::buffa::MessageField<outer::Inner, ::buffa::Inline<outer::Inner>>"));
}
#[test]
fn inline_enum_field() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
enum Status { UNKNOWN = 0; ACTIVE = 1; INACTIVE = 2; }
message Item { Status status = 1; }
"#,
&no_views(),
);
assert!(content.contains("pub enum Status"));
assert!(content.contains("pub status: ::buffa::EnumValue<Status>"));
}
#[test]
fn inline_map_field() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Address { string street = 1; }
message Book {
map<string, int32> stock = 1;
map<string, Address> locations = 2;
}
"#,
&no_views(),
);
assert!(content.contains("pub stock: ::buffa::__private::HashMap<"));
assert!(content.contains("pub locations: ::buffa::__private::HashMap<"));
}
#[test]
fn inline_oneof() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Contact {
oneof info {
string email = 1;
string phone = 2;
}
}
"#,
&no_views(),
);
assert!(content.contains("pub info: ::core::option::Option<__buffa::oneof::contact::Info>"));
assert!(content.contains("pub enum Info"));
assert!(content.contains("Email("));
assert!(content.contains("Phone("));
}
#[test]
fn inline_proto3_optional() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Item {
optional int32 count = 1;
optional string label = 2;
}
"#,
&no_views(),
);
assert!(content.contains("pub count: ::core::option::Option<i32>"));
assert!(content.contains("pub label: ::core::option::Option<::buffa::alloc::string::String>"));
}
#[test]
fn inline_repeated_fields() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Address { string street = 1; }
message Item {
repeated string tags = 1;
repeated int32 numbers = 2;
repeated Address addresses = 3;
}
"#,
&no_views(),
);
assert!(content.contains("pub tags: ::buffa::alloc::vec::Vec<::buffa::alloc::string::String>"));
assert!(content.contains("pub numbers: ::buffa::alloc::vec::Vec<i32>"));
assert!(content.contains("pub addresses: ::buffa::alloc::vec::Vec<Address>"));
}
#[test]
fn inline_type_url() {
let content = generate_proto(
r#"
syntax = "proto3";
package my.pkg;
message Foo {
message Bar { int32 x = 1; }
Bar bar = 1;
}
"#,
&no_views(),
);
assert!(
content.contains(r#"TYPE_URL: &'static str = "type.googleapis.com/my.pkg.Foo""#),
"top-level TYPE_URL should use package: {content}"
);
assert!(
content.contains(r#"TYPE_URL: &'static str = "type.googleapis.com/my.pkg.Foo.Bar""#),
"nested TYPE_URL should include parent: {content}"
);
}
#[test]
fn inline_enum_alias() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
enum Priority {
option allow_alias = true;
LOW = 0;
DEFAULT = 0;
HIGH = 1;
}
"#,
&no_views(),
);
assert!(content.contains("LOW = 0i32"));
assert!(content.contains("pub const DEFAULT: Self = Self::LOW"));
}
#[test]
fn inline_json_message() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Item {
int64 id = 1;
string name = 2;
bytes data = 3;
}
"#,
&json_no_views(),
);
assert!(content.contains("::serde::Serialize"));
assert!(content.contains("::serde::Deserialize"));
assert!(content.contains("json_helpers::int64"));
assert!(content.contains("json_helpers::bytes"));
}
#[test]
fn inline_json_oneof() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Msg {
oneof value {
string text = 1;
int32 number = 2;
}
}
"#,
&json_no_views(),
);
assert!(content.contains("serde(flatten)"));
}
#[test]
fn inline_oneof_duplicate_message_type_no_from_collision() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Placeholder {}
message T {
oneof kind {
Placeholder dyn = 1;
Placeholder error = 2;
string name = 3;
T nested = 4;
}
}
"#,
&no_views(),
);
assert_eq!(
content
.matches("::buffa::alloc::boxed::Box<super::super::super::Placeholder>")
.count(),
2,
"both Placeholder variants should be boxed: {content}"
);
assert_eq!(
content
.matches("impl ::core::convert::From<super::super::super::Placeholder> for Kind")
.count(),
0,
"duplicate-type From impls must be skipped: {content}"
);
assert_eq!(
content
.matches("impl ::core::convert::From<super::super::super::T> for Kind")
.count(),
1,
"unique-type From impl must still be generated: {content}"
);
}
#[test]
fn inline_oneof_unbox_opt_out_drops_box() {
let mut config = no_views();
config
.unboxed_oneof_fields
.push(".test.Envelope.body.small".to_string());
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Small { int32 value = 1; }
message Large { string label = 1; }
message Envelope {
oneof body {
Small small = 1;
Large large = 2;
}
}
"#,
&config,
);
assert!(
content.contains("Small(super::super::super::Small)"),
"opted-out variant should be stored inline: {content}"
);
assert!(
content.contains("Large(::buffa::alloc::boxed::Box<super::super::super::Large>)"),
"unmatched variant should stay boxed: {content}"
);
assert!(
content.contains("Self::Small(v)"),
"From impl for the inline variant must not wrap in Box: {content}"
);
assert!(
content.contains("#[allow(clippy::large_enum_variant)]"),
"enum with an inline variant should allow large_enum_variant: {content}"
);
}
#[test]
fn inline_oneof_no_unbox_rules_no_large_enum_allow() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Small { int32 value = 1; }
message Envelope {
oneof body {
Small small = 1;
}
}
"#,
&no_views(),
);
assert!(
!content.contains("large_enum_variant"),
"default output must not carry the allow: {content}"
);
}
#[test]
fn inline_oneof_unbox_recursive_variant_is_rejected() {
let proto = dedent(
r#"
syntax = "proto3";
package test;
message Node {
oneof kind {
Node child = 1;
int32 leaf = 2;
}
}
"#,
);
let dir = tempfile::tempdir().expect("temp dir");
let proto_path = dir.path().join("test.proto");
std::fs::write(&proto_path, &proto).expect("write proto");
let fds = compile_protos(
&[proto_path.to_str().unwrap()],
&[dir.path().to_str().unwrap()],
);
let mut config = no_views();
config
.unboxed_oneof_fields
.push(".test.Node.kind.child".to_string());
let result = buffa_codegen::generate(&fds.file, &["test.proto".into()], &config);
let err = result.expect_err("unboxing a recursive variant should error");
assert!(
err.to_string().contains("recursive"),
"error should explain the recursion: {err}"
);
}
#[test]
fn inline_proto2_required_no_json_skip() {
let content = generate_proto(
r#"
syntax = "proto2";
package test;
message Item {
required int32 id = 1;
required string name = 2;
optional int32 count = 3;
}
"#,
&json_no_views(),
);
let skip_count = content.matches("skip_serializing_if").count();
assert_eq!(
skip_count, 1,
"only the optional field should have skip_serializing_if, got {skip_count}: {content}"
);
}
#[test]
fn inline_proto2_custom_deserialize_honours_struct_default() {
let content = generate_proto(
r#"
syntax = "proto2";
package test;
message Item {
required int32 bar = 1 [default = 42];
oneof choice {
string a = 2;
int32 b = 3;
}
}
"#,
&json_no_views(),
);
assert!(
content.contains("bar: 42i32"),
"custom Default must set bar: 42: {content}"
);
assert!(
content.contains("::core::default::Default>::default()") && content.contains("__r.bar"),
"custom Deserialize must init from struct Default: {content}"
);
assert!(
!content.contains(".unwrap_or_default()"),
"must not use unwrap_or_default (uses type default, not struct): {content}"
);
}
#[test]
fn inline_proto2_optional() {
let content = generate_proto(
r#"
syntax = "proto2";
package test;
message Item {
optional int32 count = 1;
required string name = 2;
optional string label = 3 [default = "untitled"];
}
"#,
&no_views(),
);
assert!(content.contains("pub count: ::core::option::Option<i32>"));
assert!(content.contains("pub name: ::buffa::alloc::string::String"));
}
#[test]
fn inline_proto2_closed_enum() {
let content = generate_proto(
r#"
syntax = "proto2";
package test;
enum Status { UNKNOWN = 0; ACTIVE = 1; }
message Item { optional Status status = 1; }
"#,
&no_views(),
);
assert!(
content.contains("pub status: ::core::option::Option<Status>"),
"proto2 closed enum should be ::core::option::Option<Status>, not EnumValue: {content}"
);
}
#[test]
fn inline_cross_package_super() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("dep.proto"),
"syntax = \"proto3\";\npackage dep;\nmessage Shared { int32 x = 1; }\n",
)
.unwrap();
std::fs::write(
dir.path().join("main.proto"),
"syntax = \"proto3\";\npackage main;\nimport \"dep.proto\";\nmessage Msg { dep.Shared s = 1; }\n",
)
.unwrap();
let dep_path = dir.path().join("dep.proto");
let main_path = dir.path().join("main.proto");
let fds = compile_protos(
&[dep_path.to_str().unwrap(), main_path.to_str().unwrap()],
&[dir.path().to_str().unwrap()],
);
let files = buffa_codegen::generate(&fds.file, &["main.proto".into()], &no_views())
.expect("codegen failed");
let content = &files[0].content;
assert!(
content.contains("super::dep::Shared"),
"cross-package ref should use super::dep::Shared: {content}"
);
}
#[test]
fn inline_view_keyword_package_path() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("latlng.proto"),
"syntax = \"proto3\";\npackage google.type;\nmessage LatLng { double lat = 1; double lng = 2; }\n",
)
.unwrap();
std::fs::write(
dir.path().join("marker.proto"),
"syntax = \"proto3\";\npackage google.maps;\nimport \"latlng.proto\";\nmessage Marker { google.type.LatLng location = 1; }\n",
)
.unwrap();
let latlng_path = dir.path().join("latlng.proto");
let marker_path = dir.path().join("marker.proto");
let fds = compile_protos(
&[latlng_path.to_str().unwrap(), marker_path.to_str().unwrap()],
&[dir.path().to_str().unwrap()],
);
let files = buffa_codegen::generate(
&fds.file,
&["marker.proto".into(), "latlng.proto".into()],
&CodeGenConfig::default(),
)
.expect("codegen failed for keyword package with views");
let marker = files
.iter()
.find(|f| f.package == "google.maps")
.expect("google.maps.rs not generated");
assert!(
marker.content.contains("r#type"),
"keyword package segment must use raw ident: {}",
marker.content
);
}
#[test]
fn inline_bytes_field_mapping() {
let mut config = no_views();
config
.bytes_fields
.push((".".into(), buffa_codegen::BytesRepr::Bytes));
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Msg {
bytes data = 1;
bytes payload = 2;
}
"#,
&config,
);
assert!(
content.contains("::buffa::bytes::Bytes"),
"bytes fields should use Bytes type: {content}"
);
assert!(
!content.contains("Vec<u8>"),
"bytes fields should not use Vec<u8> when bytes mapping is enabled"
);
assert!(
content.contains("::buffa::types::decode_bytes_to_bytes"),
"decode must use decode_bytes_to_bytes for Bytes field: {content}"
);
assert!(
!content.contains("Bytes::from("),
"must not wrap decode in Bytes::from (regression to alloc+copy): {content}"
);
assert!(
!content.contains("merge_bytes(&mut self"),
"must not call merge_bytes on Bytes field (no &mut): {content}"
);
}
#[test]
fn inline_bytes_field_selective_mapping() {
let mut config = no_views();
config
.bytes_fields
.push((".test.Msg.data".into(), buffa_codegen::BytesRepr::Bytes));
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Msg {
bytes data = 1;
bytes other = 2;
}
"#,
&config,
);
assert!(
content.contains("::buffa::bytes::Bytes"),
"selected field should use Bytes: {content}"
);
assert!(
content.contains("Vec<u8>"),
"non-selected field should still use Vec<u8>: {content}"
);
}
#[test]
fn module_collision_nested_message_vs_subpackage() {
let dir = tempfile::tempdir().expect("temp dir");
std::fs::write(
dir.path().join("foo.proto"),
"syntax = \"proto3\";\npackage foo;\nmessage Oof { message Inner { int32 x = 1; } Inner inner = 1; }\n",
)
.unwrap();
std::fs::write(
dir.path().join("foo_oof.proto"),
"syntax = \"proto3\";\npackage foo.oof;\nmessage Thing { int32 y = 1; }\n",
)
.unwrap();
let fds = compile_protos(
&[
dir.path().join("foo.proto").to_str().unwrap(),
dir.path().join("foo_oof.proto").to_str().unwrap(),
],
&[dir.path().to_str().unwrap()],
);
let config = no_views();
let content = buffa_codegen::generate(&fds.file, &["foo.proto".into()], &config)
.expect("codegen")
.into_iter()
.map(|f| f.content)
.collect::<Vec<_>>()
.join("\n");
assert!(
content.contains("pub mod oof_"),
"nested module should be deconflicted to `oof_`: {content}"
);
assert!(
content.contains("oof_ :: Inner") || content.contains("oof_::Inner"),
"nested-type references should use the deconflicted module: {content}"
);
assert!(content.contains("struct Oof"), "{content}");
}
#[test]
fn module_collision_two_messages_race_to_distinct_names() {
let dir = tempfile::tempdir().expect("temp dir");
std::fs::write(
dir.path().join("foo.proto"),
"syntax = \"proto3\";\npackage foo;\n\
message Oof { message Inner { int32 x = 1; } Inner inner = 1; }\n\
message Oof_ { message Inner { int32 x = 1; } Inner inner = 1; }\n",
)
.unwrap();
std::fs::write(
dir.path().join("a.proto"),
"syntax = \"proto3\";\npackage foo.oof;\nmessage Thing { int32 y = 1; }\n",
)
.unwrap();
std::fs::write(
dir.path().join("b.proto"),
"syntax = \"proto3\";\npackage foo.oof_;\nmessage Widget { int32 z = 1; }\n",
)
.unwrap();
let fds = compile_protos(
&[
dir.path().join("foo.proto").to_str().unwrap(),
dir.path().join("a.proto").to_str().unwrap(),
dir.path().join("b.proto").to_str().unwrap(),
],
&[dir.path().to_str().unwrap()],
);
let content = buffa_codegen::generate(&fds.file, &["foo.proto".into()], &no_views())
.expect("codegen")
.into_iter()
.map(|f| f.content)
.collect::<Vec<_>>()
.join("\n");
assert!(
content.contains("oof__ :: Inner") || content.contains("oof__::Inner"),
"Oof should reference oof__::Inner: {content}"
);
assert!(
content.contains("oof___ :: Inner") || content.contains("oof___::Inner"),
"Oof_ should reference oof___::Inner: {content}"
);
}
#[test]
fn module_no_collision_keeps_natural_module_name() {
let content = generate_proto(
r#"
syntax = "proto3";
package foo;
message Oof { message Inner { int32 x = 1; } Inner inner = 1; }
"#,
&no_views(),
);
assert!(
content.contains("oof :: Inner") || content.contains("oof::Inner"),
"{content}"
);
assert!(
!content.contains("pub mod oof_") && !content.contains("oof_ ::"),
"no spurious dedup: {content}"
);
}
#[test]
fn module_of_a_lower_case_message_gets_a_suffix() {
let content = generate_proto(
r#"
syntax = "proto3";
package foo;
message item {
message Inner { int32 x = 1; }
Inner inner = 1;
oneof pick { int32 a = 2; string b = 3; }
}
message User { item.Inner inner = 1; }
"#,
&CodeGenConfig::default(),
);
assert!(content.contains("pub struct item {"), "{content}");
assert!(!content.contains("pub mod item {"), "{content}");
assert_eq!(content.matches("pub mod item_ {").count(), 4, "{content}");
for path in [
"item_::Inner",
"__buffa::view::item_::InnerView",
"__buffa::oneof::item_::Pick",
] {
assert!(content.contains(path), "missing `{path}`: {content}");
}
assert!(
content.contains("because `item` is not available in this scope"),
"the renamed module is documented: {content}"
);
}
#[test]
fn module_named_after_a_path_keyword_gets_a_suffix() {
let content = generate_proto(
r#"
syntax = "proto3";
package foo;
message Crate { message Inner { int32 x = 1; } Inner inner = 1; }
message Self { message Inner { int32 x = 1; } Inner inner = 1; }
message User { Crate.Inner a = 1; Self.Inner b = 2; }
"#,
&CodeGenConfig::default(),
);
for expected in [
"pub mod crate_ {",
"pub mod self_ {",
"pub struct Self_ {",
"crate_::Inner",
"self_::Inner",
"__buffa::view::crate_::InnerView",
] {
assert!(
content.contains(expected),
"missing `{expected}`: {content}"
);
}
assert!(!content.contains("crate::Inner"), "{content}");
}
#[test]
fn module_named_arbitrary_gets_a_suffix_whatever_the_config() {
let proto = r#"
syntax = "proto3";
package foo;
message Arbitrary { message Inner { int32 x = 1; } Inner inner = 1; }
"#;
let mut config = no_views();
for generate_arbitrary in [false, true] {
config.generate_arbitrary = generate_arbitrary;
let content = generate_proto(proto, &config);
assert!(content.contains("pub mod arbitrary_ {"), "{content}");
assert!(content.contains("arbitrary_::Inner"), "{content}");
}
}
#[test]
fn module_named_oneof_gets_a_suffix_at_package_level() {
let proto = r#"
syntax = "proto3";
package foo;
message Oneof {
message Inner { int32 x = 1; }
message Oneof { message Deep { int32 x = 1; } Deep deep = 1; }
Inner inner = 1;
oneof pick { int32 a = 2; string b = 3; }
}
"#;
for config in [CodeGenConfig::default(), no_views()] {
let content = generate_proto(proto, &config);
assert!(content.contains("pub mod oneof_ {"), "{content}");
assert!(content.contains("oneof_::Inner"), "{content}");
assert!(content.contains("oneof_::oneof::Deep"), "{content}");
}
let content = generate_proto(proto, &CodeGenConfig::default());
assert!(
content.contains("__buffa::view::oneof_::InnerView"),
"{content}"
);
assert!(
content.contains("__buffa::view::oneof::oneof_::Pick"),
"{content}"
);
}
#[test]
fn nested_module_avoids_a_sibling_type_name() {
let content = generate_proto(
r#"
syntax = "proto3";
package foo;
message Outer {
message leaf { message Deep { int32 n = 1; } Deep deep = 1; }
message Mode { message Detail { int32 n = 1; } Detail detail = 1; }
enum mode { MODE_UNSPECIFIED = 0; }
leaf l = 1;
Mode m = 2;
mode e = 3;
}
"#,
&no_views(),
);
for expected in [
"pub mod leaf_ {",
"pub mod mode_ {",
"leaf_::Deep",
"mode_::Detail",
] {
assert!(
content.contains(expected),
"missing `{expected}`: {content}"
);
}
assert!(!content.contains("pub mod leaf {"), "{content}");
}
#[test]
fn module_deconflicted_from_a_subpackage_is_renamed_in_the_view_tree() {
let dir = tempfile::tempdir().expect("temp dir");
std::fs::write(
dir.path().join("foo.proto"),
"syntax = \"proto3\";\npackage foo;\n\
message Oof {\n\
message Inner { int32 x = 1; }\n\
message InnerView { int32 y = 1; }\n\
Inner inner = 1;\n\
}\n",
)
.unwrap();
std::fs::write(
dir.path().join("foo_oof.proto"),
"syntax = \"proto3\";\npackage foo.oof;\nmessage Thing { int32 y = 1; }\n",
)
.unwrap();
let fds = compile_protos(
&[
dir.path().join("foo.proto").to_str().unwrap(),
dir.path().join("foo_oof.proto").to_str().unwrap(),
],
&[dir.path().to_str().unwrap()],
);
let content =
buffa_codegen::generate(&fds.file, &["foo.proto".into()], &CodeGenConfig::default())
.expect("codegen")
.into_iter()
.map(|f| f.content)
.collect::<Vec<_>>()
.join("\n");
assert!(!content.contains("pub mod oof {"), "{content}");
assert!(
content.contains("__buffa::view::oof_::InnerView"),
"{content}"
);
}
#[test]
fn inline_string_field_mapping() {
use buffa_codegen::StringRepr;
let mut config = no_views();
config
.string_fields
.push((".".into(), StringRepr::Custom("::smol_str::SmolStr".into())));
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Msg {
string name = 1;
optional string nick = 2;
}
"#,
&config,
);
assert!(
content.contains("::smol_str::SmolStr"),
"string fields should use the custom type: {content}"
);
assert!(
content.contains("::buffa::types::decode_string_to"),
"decode must use decode_string_to for non-default string repr: {content}"
);
assert!(
!content.contains("merge_string(&mut self"),
"must not call merge_string on a non-default string field: {content}"
);
}
#[test]
fn inline_string_field_selective_and_override() {
use buffa_codegen::StringRepr;
let mut config = no_views();
config
.string_fields
.push((".".into(), StringRepr::Custom("::smol_str::SmolStr".into())));
config.string_fields.push((
".test.Msg.code".into(),
StringRepr::Custom("::compact_str::CompactString".into()),
));
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Msg {
string name = 1;
string code = 2;
}
"#,
&config,
);
assert!(
content.contains("::smol_str::SmolStr"),
"broad rule should apply SmolStr to `name`: {content}"
);
assert!(
content.contains("::compact_str::CompactString"),
"specific override should apply CompactString to `code`: {content}"
);
}
#[test]
fn inline_string_custom_emits_generic_arbitrary_builder() {
use buffa_codegen::{BytesRepr, StringRepr};
let mut config = no_views();
config.generate_arbitrary = true;
config
.string_fields
.push((".".into(), StringRepr::Custom("::ecow::EcoString".into())));
config.bytes_fields.push((".".into(), BytesRepr::Bytes));
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Msg {
string name = 1;
optional string nick = 2;
repeated string tags = 3;
bytes blob = 4;
}
"#,
&config,
);
assert!(
content.contains("::ecow::EcoString"),
"string field should use the custom type: {content}"
);
assert_eq!(
["name", "nick", "tags", "blob"].map(|field| arbitrary_builder_of(&content, field)),
[
Some("arbitrary_proto_string"),
Some("arbitrary_proto_string_opt"),
Some("arbitrary_proto_string_vec"),
Some("arbitrary_proto_bytes"),
]
);
}
fn arbitrary_builder_of<'a>(content: &'a str, field: &str) -> Option<&'a str> {
let decl = content
.find(&format!("pub {field}:"))
.unwrap_or_else(|| panic!("no field `{field}` in: {content}"));
let before = &content[..decl];
let attrs = &before[before.rfind("pub ").unwrap_or(0)..];
let prefix = "arbitrary(with = ::buffa::__private::";
let rest = &attrs[attrs.find(prefix)? + prefix.len()..];
Some(&rest[..rest.find(')').expect("unterminated arbitrary attribute")])
}
const ARBITRARY_MAP_PROTO: &str = r#"
syntax = "proto3";
package test;
message Msg {
map<string, int32> string_key = 1;
map<int32, string> string_value = 2;
map<string, string> both_strings = 3;
map<string, bytes> string_key_bytes_value = 4;
map<int32, bytes> bytes_value = 5;
map<int32, int32> no_string_or_bytes = 6;
}
"#;
#[track_caller]
fn assert_arbitrary_map_builders(config: &CodeGenConfig, expected: [Option<&str>; 6]) {
let content = generate_proto(ARBITRARY_MAP_PROTO, config);
let fields = [
"string_key",
"string_value",
"both_strings",
"string_key_bytes_value",
"bytes_value",
"no_string_or_bytes",
];
assert_eq!(
fields.map(|field| arbitrary_builder_of(&content, field)),
expected,
"builders of {fields:?}"
);
}
#[test]
fn arbitrary_map_builder_matches_the_slots_that_convert() {
use buffa_codegen::{BytesRepr, StringRepr};
let custom_string = (
".".to_owned(),
StringRepr::Custom("::ecow::EcoString".into()),
);
let bytes = (".".to_owned(), BytesRepr::Bytes);
let mut config = no_views();
config.generate_arbitrary = true;
config.string_fields.push(custom_string.clone());
config.bytes_fields.push(bytes.clone());
assert_arbitrary_map_builders(
&config,
[
Some("arbitrary_proto_string_map_key"),
Some("arbitrary_proto_string_map_value"),
Some("arbitrary_proto_string_map"),
Some("arbitrary_proto_string_bytes_map"),
Some("arbitrary_proto_bytes_map"),
None,
],
);
let mut config = no_views();
config.generate_arbitrary = true;
config.string_fields.push(custom_string);
assert_arbitrary_map_builders(
&config,
[
Some("arbitrary_proto_string_map_key"),
Some("arbitrary_proto_string_map_value"),
Some("arbitrary_proto_string_map"),
Some("arbitrary_proto_string_map_key"),
None,
None,
],
);
let mut config = no_views();
config.generate_arbitrary = true;
config.bytes_fields.push(bytes);
assert_arbitrary_map_builders(
&config,
[
None,
None,
None,
Some("arbitrary_proto_bytes_map"),
Some("arbitrary_proto_bytes_map"),
None,
],
);
}
#[test]
fn arbitrary_builder_for_a_proto2_repeated_field_is_the_vec_builder() {
use buffa_codegen::{BytesRepr, StringRepr};
let mut config = no_views();
config.generate_arbitrary = true;
config
.string_fields
.push((".".into(), StringRepr::Custom("::ecow::EcoString".into())));
config.bytes_fields.push((".".into(), BytesRepr::Bytes));
let content = generate_proto(
r#"
syntax = "proto2";
package test;
message Msg {
repeated string names = 1;
repeated bytes blobs = 2;
optional string name = 3;
optional bytes blob = 4;
}
"#,
&config,
);
assert_eq!(
["names", "blobs", "name", "blob"].map(|field| arbitrary_builder_of(&content, field)),
[
Some("arbitrary_proto_string_vec"),
Some("arbitrary_proto_bytes_vec"),
Some("arbitrary_proto_string_opt"),
Some("arbitrary_proto_bytes_opt"),
]
);
}
#[test]
fn inline_string_default_is_unchanged() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Msg { string name = 1; }
"#,
&no_views(),
);
assert!(
content.contains("::buffa::alloc::string::String"),
"default string repr should remain String: {content}"
);
assert!(
content.contains("merge_string(&mut self"),
"default string field should keep the in-place merge_string fast path: {content}"
);
assert!(
!content.contains("decode_string_to"),
"default string field must not use the generic decode helper: {content}"
);
}
#[test]
fn inline_preserve_unknown_fields_disabled() {
let mut config = no_views();
config.preserve_unknown_fields = false;
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Msg { int32 x = 1; }
"#,
&config,
);
assert!(
!content.contains("__buffa_unknown_fields"),
"unknown fields should not be present when preservation is disabled: {content}"
);
}
#[test]
fn inline_empty_message() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Empty {}
"#,
&no_views(),
);
assert!(content.contains("pub struct Empty"));
assert!(content.contains("impl ::buffa::Message for Empty"));
assert!(content.contains("__buffa_unknown_fields"));
assert!(!content.contains("__buffa_cached_size"));
}
#[test]
fn inline_empty_message_no_unknown_fields() {
let mut config = no_views();
config.preserve_unknown_fields = false;
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Empty {}
"#,
&config,
);
assert!(content.contains("pub struct Empty"));
assert!(!content.contains("__buffa_unknown_fields"));
}
#[test]
fn inline_preserve_unknown_fields_in_keeps_selected_messages() {
let mut config = no_views();
config.preserve_unknown_fields = false;
config.preserve_unknown_fields_in = vec![(".test.Keep".to_string(), true)];
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Keep {}
message Drop {}
"#,
&config,
);
assert!(
struct_has_unknown_fields(&content, "Keep"),
"Keep must retain unknown fields: {content}"
);
assert!(
!struct_has_unknown_fields(&content, "Drop"),
"Drop must omit unknown fields: {content}"
);
}
#[test]
fn inline_preserve_unknown_fields_in_last_match_and_nested() {
let mut config = no_views();
config.preserve_unknown_fields = false;
config.preserve_unknown_fields_in = vec![
(".test".to_string(), true),
(".test.Drop".to_string(), false),
(".test.Outer.Inner".to_string(), true),
];
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Keep {}
message Drop {}
message Outer {
message Inner {}
}
"#,
&config,
);
assert!(struct_has_unknown_fields(&content, "Keep"));
assert!(!struct_has_unknown_fields(&content, "Drop"));
assert!(struct_has_unknown_fields(&content, "Outer"));
assert!(struct_has_unknown_fields(&content, "Inner"));
}
#[test]
fn inline_preserve_unknown_fields_in_outer_on_inner_off() {
let proto = r#"
syntax = "proto3";
package test;
message Outer {
message Inner {}
}
"#;
let mut config = no_views();
config.preserve_unknown_fields = false;
config.preserve_unknown_fields_in = vec![(".test.Outer".to_string(), true)];
let content = generate_proto(proto, &config);
assert!(struct_has_unknown_fields(&content, "Outer"));
assert!(
struct_has_unknown_fields(&content, "Inner"),
"a rule naming Outer covers Outer.Inner: {content}"
);
config
.preserve_unknown_fields_in
.push((".test.Outer.Inner".to_string(), false));
let content = generate_proto(proto, &config);
assert!(struct_has_unknown_fields(&content, "Outer"));
assert!(
!struct_has_unknown_fields(&content, "Inner"),
"a later Inner=false rule must win over the enclosing Outer=true rule: {content}"
);
}
#[test]
fn inline_preserve_unknown_fields_in_nested_independent_of_outer() {
let mut config = no_views();
config.preserve_unknown_fields = false;
config.preserve_unknown_fields_in = vec![(".test.Outer.Inner".to_string(), true)];
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Outer {
message Inner {}
}
"#,
&config,
);
assert!(
!struct_has_unknown_fields(&content, "Outer"),
"enabling Inner must not enable Outer: {content}"
);
assert!(
struct_has_unknown_fields(&content, "Inner"),
"Inner must retain unknown fields: {content}"
);
}
#[test]
fn inline_preserve_unknown_fields_in_views() {
let mut config = CodeGenConfig::default();
config.preserve_unknown_fields = false;
config.preserve_unknown_fields_in = vec![(".test.Keep".to_string(), true)];
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Keep {}
message Drop {}
"#,
&config,
);
assert!(struct_has_unknown_fields(&content, "Keep"));
assert!(!struct_has_unknown_fields(&content, "Drop"));
assert!(
struct_has_unknown_fields(&content, "KeepView"),
"KeepView must retain unknown fields: {content}"
);
assert!(
!struct_has_unknown_fields(&content, "DropView"),
"DropView must omit unknown fields: {content}"
);
}
#[test]
fn test_view_serialize_impl_emitted_when_json_enabled() {
let files = generate_for("json_types.proto", &json_with_views());
let combined = files
.iter()
.map(|f| f.content.as_str())
.collect::<Vec<_>>()
.join("\n");
assert!(
combined.contains("impl<'__a> ::serde::Serialize for"),
"view Serialize impl must be emitted when generate_json=true: {combined}"
);
for file in &files {
syn::parse_file(&file.content).unwrap_or_else(|e| {
panic!(
"generated file must parse ({}): {e}\n---\n{}",
file.package, file.content
)
});
}
}
#[test]
fn test_view_serialize_not_emitted_when_json_disabled() {
let files = generate_for("json_types.proto", &CodeGenConfig::default());
let combined = files
.iter()
.map(|f| f.content.as_str())
.collect::<Vec<_>>()
.join("\n");
assert!(
!combined.contains("impl<'__a> ::serde::Serialize for"),
"view Serialize impl must NOT be emitted when generate_json=false"
);
}
#[test]
fn test_view_serialize_json_helpers_used() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Item {
int64 id = 1;
bytes data = 2;
double score = 3;
}
"#,
&json_with_views(),
);
assert!(
content.contains("json_helpers"),
"view Serialize must use json_helpers for int64/bytes/double: {content}"
);
syn::parse_file(&content).expect("generated content must parse");
}
#[test]
fn test_view_serialize_map_keys_stringified() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Maps {
map<int64, string> by_id = 1;
map<bool, string> by_flag = 2;
map<string, string> by_name = 3;
}
"#,
&json_with_views(),
);
syn::parse_file(&content).expect("generated content must parse");
assert!(
content.contains("::buffa::json_helpers::MapKeyJson"),
"non-string map keys must be stringified via MapKeyJson: {content}"
);
}
#[test]
fn test_view_serialize_proto2_required_multi_field_parses() {
let content = generate_proto(
r#"
syntax = "proto2";
package test;
message TwoRequired {
required int64 a = 1;
required bytes b = 2;
required int64 c = 3;
}
"#,
&json_with_views(),
);
syn::parse_file(&content)
.expect("proto2 required multi-field view Serialize must produce parseable output");
assert!(
content.contains("impl<'__a> ::serde::Serialize for TwoRequiredView"),
"view Serialize impl must be emitted: {content}"
);
}
#[test]
fn with_setters_emitted_for_explicit_presence_fields() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
enum Color { COLOR_UNSPECIFIED = 0; RED = 1; }
message Msg {
optional int32 count = 1;
optional string name = 2;
optional bytes data = 3;
optional Color color = 4;
// implicit-presence fields — no setter
int32 implicit_int = 5;
string implicit_str = 6;
// repeated — no setter
repeated string tags = 7;
}
"#,
&no_views(),
);
assert!(
content.contains("pub fn with_count"),
"with_count missing: {content}"
);
assert!(
content.contains("pub fn with_name"),
"with_name missing: {content}"
);
assert!(
content.contains("pub fn with_data"),
"with_data missing: {content}"
);
assert!(
content.contains("pub fn with_color"),
"with_color missing: {content}"
);
assert!(
content.contains("impl ::core::convert::Into"),
"string setter should use impl Into: {content}"
);
assert!(
!content.contains("with_implicit_int"),
"implicit int should not get setter: {content}"
);
assert!(
!content.contains("with_implicit_str"),
"implicit string should not get setter: {content}"
);
assert!(
!content.contains("with_tags"),
"repeated field should not get setter: {content}"
);
}
#[test]
fn with_setters_disabled_by_config() {
let mut config = no_views();
config.generate_with_setters = false;
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Msg { optional int32 count = 1; }
"#,
&config,
);
assert!(
!content.contains("pub fn with_count"),
"setter should be absent when generate_with_setters=false: {content}"
);
}
#[test]
fn with_setters_bytes_type_uses_into() {
let mut config = no_views();
config
.bytes_fields
.push((".test.Msg.data".into(), buffa_codegen::BytesRepr::Bytes));
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Msg { optional bytes data = 1; }
"#,
&config,
);
assert!(
content.contains("pub fn with_data"),
"with_data missing: {content}"
);
assert!(
content.contains("impl ::core::convert::Into"),
"bytes::Bytes setter should use impl Into: {content}"
);
}
#[test]
fn with_setters_vec_u8_uses_into() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Msg { optional bytes data = 1; }
"#,
&no_views(),
);
assert!(
content.contains("pub fn with_data"),
"with_data missing: {content}"
);
assert!(
content.contains("impl ::core::convert::Into"),
"Vec<u8> setter should use impl Into: {content}"
);
}
#[test]
fn with_setters_enum_uses_into() {
let content = generate_proto(
r#"
syntax = "proto3";
package test;
enum Color { COLOR_UNSPECIFIED = 0; RED = 1; }
message Msg { optional Color color = 1; }
"#,
&no_views(),
);
assert!(
content.contains("pub fn with_color"),
"with_color missing: {content}"
);
assert!(
content.contains("impl ::core::convert::Into"),
"enum setter should use impl Into: {content}"
);
}
#[test]
fn with_setters_proto2_repeated_no_setter() {
let content = generate_proto(
r#"
syntax = "proto2";
package test;
message Msg { repeated string items = 1; }
"#,
&no_views(),
);
assert!(
!content.contains("with_items"),
"proto2 repeated field should not get setter: {content}"
);
}
#[test]
fn blanket_unbox_oneof_skips_recursive_variants() {
let proto = dedent(
r#"
syntax = "proto3";
package test;
message Node {
oneof kind {
Node child = 1;
int32 leaf = 2;
}
}
message Small { int32 value = 1; }
message Envelope {
oneof body {
Small small = 1;
}
}
"#,
);
let dir = tempfile::tempdir().expect("temp dir");
let proto_path = dir.path().join("test.proto");
std::fs::write(&proto_path, &proto).expect("write proto");
let fds = compile_protos(
&[proto_path.to_str().unwrap()],
&[dir.path().to_str().unwrap()],
);
let mut config = no_views();
config.unboxed_oneof_fields.push(".".to_string()); let result = buffa_codegen::generate(&fds.file, &["test.proto".into()], &config);
let files = result.unwrap_or_else(|e| {
panic!("blanket unbox_oneof() should skip the recursive variant, but errored: {e}")
});
let content = files
.into_iter()
.map(|f| f.content)
.collect::<Vec<_>>()
.join("\n");
assert!(
content.contains("Child(::buffa::alloc::boxed::Box<"),
"recursive variant must stay boxed under the blanket rule"
);
assert!(
content.contains("Small(super::super::super::Small)"),
"non-recursive variant should be inline under the blanket rule"
);
}
#[test]
fn blanket_unbox_oneof_keeps_mutually_recursive_variants_boxed() {
let mut config = no_views();
config.unboxed_oneof_fields.push(".".to_string());
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message A {
oneof body {
B b = 1;
C c = 2;
}
}
message B {
oneof body {
A a = 1;
}
}
message C { int32 value = 1; }
"#,
&config,
);
assert!(
content.contains("B(::buffa::alloc::boxed::Box<super::super::super::B>)"),
"mutually recursive variant A.body.b must stay boxed: {content}"
);
assert!(
content.contains("A(::buffa::alloc::boxed::Box<super::super::super::A>)"),
"mutually recursive variant B.body.a must stay boxed: {content}"
);
assert!(
content.contains("C(super::super::super::C)"),
"non-recursive variant should be inline under the blanket rule: {content}"
);
}
#[test]
fn prefix_unbox_rule_on_recursive_variant_skips_without_error() {
let mut config = no_views();
config.unboxed_oneof_fields.push(".test.Node".to_string());
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Leaf { int32 value = 1; }
message Node {
oneof kind {
Node child = 1;
Leaf leaf = 2;
}
}
"#,
&config,
);
assert!(
content.contains("Child(::buffa::alloc::boxed::Box<super::super::super::Node>)"),
"recursive variant must stay boxed under a prefix rule: {content}"
);
assert!(
content.contains("Leaf(super::super::super::Leaf)"),
"non-recursive sibling should be inline under the prefix rule: {content}"
);
}
#[test]
fn default_inline_pointer_skips_recursive_fields() {
let config = no_views();
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Small { int32 value = 1; }
message Node {
Node child = 1;
Small leaf = 2;
}
"#,
&config,
);
assert!(
content.contains("pub child: ::buffa::MessageField<Self>,"),
"recursive singular field must stay on Box (default pointer) under the blanket rule: {content}"
);
assert!(
content.contains("pub leaf: ::buffa::MessageField<Small, ::buffa::Inline<Small>>"),
"non-recursive singular field should be inline under the blanket rule: {content}"
);
}
#[test]
fn default_inline_pointer_keeps_mutually_recursive_boxed() {
let config = no_views();
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message A { B b = 1; C c = 2; }
message B { A a = 1; }
message C { int32 value = 1; }
"#,
&config,
);
assert!(
content.contains("pub b: ::buffa::MessageField<B>,"),
"mutually recursive field A.b must stay on Box: {content}"
);
assert!(
content.contains("pub a: ::buffa::MessageField<A>,"),
"mutually recursive field B.a must stay on Box: {content}"
);
assert!(
content.contains("pub c: ::buffa::MessageField<C, ::buffa::Inline<C>>"),
"non-recursive field A.c should be inline under the blanket rule: {content}"
);
}
#[test]
fn exact_inline_rule_on_recursive_field_is_rejected() {
let proto = dedent(
r#"
syntax = "proto3";
package test;
message Node { Node child = 1; }
"#,
);
let dir = tempfile::tempdir().expect("temp dir");
let proto_path = dir.path().join("test.proto");
std::fs::write(&proto_path, &proto).expect("write proto");
let fds = compile_protos(
&[proto_path.to_str().unwrap()],
&[dir.path().to_str().unwrap()],
);
let mut config = no_views();
config.pointer_fields.push((
".test.Node.child".to_string(),
buffa_codegen::PointerRepr::Inline,
));
let result = buffa_codegen::generate(&fds.file, &["test.proto".into()], &config);
let err = result.expect_err("inlining a recursive singular field should error");
let msg = err.to_string();
assert!(
msg.contains("recursive") && msg.contains(".test.Node.child"),
"error should explain the recursion and name the field: {err}"
);
}
#[test]
fn box_type_opt_out_restores_boxed_default() {
let mut config = no_views();
config
.pointer_fields
.push((".test.Node".to_string(), buffa_codegen::PointerRepr::Box));
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Leaf { int32 value = 1; }
message Node { Leaf leaf = 1; }
message Elsewhere { Leaf leaf = 1; }
"#,
&config,
);
assert!(
content.contains("pub leaf: ::buffa::MessageField<Leaf>,"),
"Node.leaf opted out via Box rule must use the boxed pointer: {content}"
);
assert!(
content.contains("pub leaf: ::buffa::MessageField<Leaf, ::buffa::Inline<Leaf>>"),
"Elsewhere.leaf (no rule) should be inline by default: {content}"
);
}
#[test]
fn unbox_oneof_and_inline_field_detect_cross_kind_cycle() {
let mut config = no_views();
config.unboxed_oneof_fields.push(".".to_string());
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message A {
oneof body { B b = 1; }
}
message B { A a = 1; }
"#,
&config,
);
assert!(
content.contains("B(::buffa::alloc::boxed::Box<super::super::super::B>)"),
"oneof variant in a cross-kind cycle must stay boxed: {content}"
);
assert!(
content.contains("pub a: ::buffa::MessageField<A>,"),
"singular field in a cross-kind cycle must stay on Box: {content}"
);
}
#[test]
fn boxed_oneof_variant_under_inline_default_uses_box() {
let config = no_views();
let content = generate_proto(
r#"
syntax = "proto3";
package test;
message Node {
oneof kind { Node child = 1; }
}
"#,
&config,
);
assert!(
content.contains("Child(::buffa::alloc::boxed::Box<"),
"boxed oneof variant must use Box even under a blanket Inline rule: {content}"
);
assert!(
!content.contains("Child(::buffa::Inline<"),
"boxed oneof variant must not use the Inline pointer: {content}"
);
}
#[test]
fn custom_deserialize_qualifies_core_paths() {
let content = generate_proto(
r#"
syntax = "proto3";
package core;
message RegisterRequest {
string name = 1;
oneof kind {
string a = 2;
int32 b = 3;
}
}
"#,
&json_with_views(),
);
assert!(
content.contains("&mut ::core::fmt::Formatter"),
"expecting() must take an absolute ::core::fmt path: {content}"
);
assert!(
!content.contains("&mut core::fmt::Formatter"),
"expecting() must not emit a bare core::fmt path: {content}"
);
}
#[test]
fn json_codegen_qualifies_serde_paths() {
let content = generate_proto(
r#"
syntax = "proto3";
package serde;
message RegisterRequest {
string name = 1;
oneof kind {
string a = 2;
int32 b = 3;
}
}
"#,
&json_with_views(),
);
assert_eq!(
content.matches("serde::").count(),
content.matches("::serde::").count(),
"generated code must not contain a bare serde:: path: {content}"
);
assert!(
content.contains("impl ::serde::Serialize for Kind"),
"oneof serialization must use an absolute serde path: {content}"
);
assert!(
content.contains("impl<'de> ::serde::Deserialize<'de> for RegisterRequest"),
"custom deserialization must use an absolute serde path: {content}"
);
}
const STRICT_JSON_PROTO: &str = r#"
syntax = "proto3";
package t;
message Plain {
int32 value = 1;
int32 max_items = 2;
}
message Choice {
oneof kind {
int32 a = 1;
string b_name = 2;
}
}
"#;
fn json_strict() -> CodeGenConfig {
let mut c = json_no_views();
c.deny_unknown_json_fields = true;
c
}
fn generate_warnings(proto: &str, config: &CodeGenConfig) -> Vec<String> {
let proto = dedent(proto);
let dir = tempfile::tempdir().expect("failed to create temp dir");
let proto_path = dir.path().join("test.proto");
std::fs::write(&proto_path, &proto).expect("failed to write proto");
let fds = compile_protos(
&[proto_path.to_str().unwrap()],
&[dir.path().to_str().unwrap()],
);
let (_, warnings) =
buffa_codegen::generate_with_diagnostics(&fds.file, &["test.proto".into()], config)
.expect("codegen failed");
warnings.iter().map(ToString::to_string).collect()
}
#[test]
fn deny_unknown_json_fields_off_by_default() {
let content = generate_proto(STRICT_JSON_PROTO, &json_no_views());
assert!(!content.contains("deny_unknown_fields"));
assert!(content.contains("IgnoredAny"));
}
#[test]
fn deny_unknown_json_fields_uses_serdes_attribute_on_the_derive_path() {
let content = generate_proto(STRICT_JSON_PROTO, &json_strict());
assert!(content.contains("serde(deny_unknown_fields)"));
}
#[test]
fn deny_unknown_json_fields_uses_a_strict_arm_in_the_custom_visitor() {
let content = generate_proto(STRICT_JSON_PROTO, &json_strict());
assert!(content.contains("::unknown_field("));
assert!(content.contains("\"bName\""));
assert!(content.contains("\"b_name\""));
assert!(!content.contains("IgnoredAny"));
}
#[test]
fn deny_unknown_json_fields_emits_nothing_without_json() {
let mut config = no_views();
config.deny_unknown_json_fields = true;
let content = generate_proto(STRICT_JSON_PROTO, &config);
assert!(!content.contains("deny_unknown_fields"));
assert!(!content.contains("::unknown_field("));
}
#[test]
fn deny_unknown_json_fields_without_json_warns() {
let mut config = no_views();
config.deny_unknown_json_fields = true;
let warnings = generate_warnings(STRICT_JSON_PROTO, &config);
assert!(
warnings
.iter()
.any(|w| w.contains("deny_unknown_json_fields requires generate_json")),
"{warnings:?}"
);
let mut config = no_views();
config.deny_unknown_json_fields_in = vec![(".t.Choice".to_string(), true)];
let warnings = generate_warnings(STRICT_JSON_PROTO, &config);
assert!(
warnings
.iter()
.any(|w| w.contains("deny_unknown_json_fields requires generate_json")),
"{warnings:?}"
);
let mut config = no_views();
config.deny_unknown_json_fields_in = vec![(".t.Choice".to_string(), false)];
let warnings = generate_warnings(STRICT_JSON_PROTO, &config);
assert!(
!warnings
.iter()
.any(|w| w.contains("deny_unknown_json_fields requires generate_json")),
"{warnings:?}"
);
let warnings = generate_warnings(STRICT_JSON_PROTO, &json_strict());
assert!(
!warnings
.iter()
.any(|w| w.contains("deny_unknown_json_fields requires generate_json")),
"{warnings:?}"
);
}
#[test]
fn deny_unknown_json_fields_in_scopes_to_matching_messages() {
let mut config = json_no_views();
config.deny_unknown_json_fields_in = vec![(".t.Choice".to_string(), true)];
let content = generate_proto(STRICT_JSON_PROTO, &config);
assert!(content.contains("::unknown_field("));
assert!(!content.contains("deny_unknown_fields"));
}
#[test]
fn deny_unknown_json_fields_in_rule_matching_nothing_warns() {
let mut config = json_no_views();
config.deny_unknown_json_fields_in = vec![(".t.Typo".to_string(), true)];
let warnings = generate_warnings(STRICT_JSON_PROTO, &config);
assert!(
warnings.iter().any(|w| w
.contains("deny_unknown_json_fields_in rule '.t.Typo' matched no generated message")),
"{warnings:?}"
);
}
#[test]
fn deny_unknown_json_fields_never_meets_the_flattened_extension_wrapper() {
let proto = r#"
syntax = "proto2";
package t;
message Carrier {
extensions 100 to 200;
optional int32 x = 1;
}
extend Carrier {
optional string label = 100;
}
"#;
let content = generate_proto(proto, &json_strict());
assert!(
content.contains("serde(flatten)"),
"no flattened wrapper emitted"
);
assert!(content.contains("::unknown_field("));
assert!(!content.contains("deny_unknown_fields"));
}