use super::*;
fn message_field(name: &str, number: i32, type_name: &str) -> FieldDescriptorProto {
FieldDescriptorProto {
name: Some(name.to_string()),
number: Some(number),
label: Some(Label::LABEL_OPTIONAL),
r#type: Some(Type::TYPE_MESSAGE),
type_name: Some(format!(".pkg.{type_name}")),
..Default::default()
}
}
fn oneof_field(
name: &str,
number: i32,
ty: Type,
type_name: Option<&str>,
index: i32,
) -> FieldDescriptorProto {
FieldDescriptorProto {
name: Some(name.to_string()),
number: Some(number),
label: Some(Label::LABEL_OPTIONAL),
r#type: Some(ty),
type_name: type_name.map(|t| format!(".pkg.{t}")),
oneof_index: Some(index),
..Default::default()
}
}
fn oneof_decl(name: &str) -> OneofDescriptorProto {
OneofDescriptorProto {
name: Some(name.to_string()),
..Default::default()
}
}
fn generate_lean(messages: Vec<DescriptorProto>, lazy: bool) -> String {
let mut file = proto3_file("lean.proto");
file.package = Some("pkg".to_string());
file.message_type = messages;
let config = CodeGenConfig {
generate_views: true,
lazy_views: lazy,
preserve_unknown_fields: false,
..Default::default()
};
let files = generate(&[file], &["lean.proto".to_string()], &config).expect("should generate");
joined(&files)
}
fn struct_body<'a>(content: &'a str, decl: &str) -> &'a str {
let start = content
.find(decl)
.unwrap_or_else(|| panic!("`{decl}` missing from generated output:\n{content}"));
let rest = &content[start..];
let end = rest
.find("\n}")
.unwrap_or_else(|| panic!("`{decl}` body not terminated:\n{rest}"));
&rest[..end]
}
#[test]
fn test_lean_eager_view_markers_follow_the_anchor_rule() {
let content = generate_lean(
vec![DescriptorProto {
name: Some("Node".to_string()),
field: vec![message_field("next", 1, "Node")],
..Default::default()
}],
false,
);
assert!(
struct_body(&content, "pub struct NodeView<'a>").contains("__buffa_phantom"),
"a singular self-reference must anchor 'a with the marker"
);
let content = generate_lean(
vec![DescriptorProto {
name: Some("Chain".to_string()),
field: vec![FieldDescriptorProto {
label: Some(Label::LABEL_REPEATED),
..message_field("links", 1, "Chain")
}],
..Default::default()
}],
false,
);
assert!(
!struct_body(&content, "pub struct ChainView<'a>").contains("__buffa_phantom"),
"a repeated field already carries 'a; the marker would be noise"
);
let content = generate_lean(
vec![DescriptorProto {
name: Some("SelfMap".to_string()),
field: vec![FieldDescriptorProto {
label: Some(Label::LABEL_REPEATED),
r#type: Some(Type::TYPE_MESSAGE),
name: Some("kids".to_string()),
number: Some(1),
type_name: Some(".pkg.SelfMap.KidsEntry".to_string()),
..Default::default()
}],
nested_type: vec![DescriptorProto {
name: Some("KidsEntry".to_string()),
field: vec![
make_field("key", 1, Label::LABEL_OPTIONAL, Type::TYPE_INT32),
message_field("value", 2, "SelfMap"),
],
options: MessageOptions {
map_entry: Some(true),
..Default::default()
}
.into(),
..Default::default()
}],
..Default::default()
}],
false,
);
assert!(
!struct_body(&content, "pub struct SelfMapView<'a>").contains("__buffa_phantom"),
"a map field already carries 'a"
);
let content = generate_lean(
vec![DescriptorProto {
name: Some("NamedNode".to_string()),
field: vec![
make_field("name", 1, Label::LABEL_OPTIONAL, Type::TYPE_STRING),
message_field("next", 2, "NamedNode"),
],
..Default::default()
}],
false,
);
assert!(
!struct_body(&content, "pub struct NamedNodeView<'a>").contains("__buffa_phantom"),
"a borrowed scalar already anchors 'a"
);
let content = generate_lean(
vec![DescriptorProto {
name: Some("Tree".to_string()),
field: vec![
oneof_field("left", 1, Type::TYPE_MESSAGE, Some("Tree"), 0),
oneof_field("leaf", 2, Type::TYPE_INT32, None, 0),
],
oneof_decl: vec![oneof_decl("shape")],
..Default::default()
}],
false,
);
assert!(
struct_body(&content, "pub struct TreeView<'a>").contains("__buffa_phantom"),
"a oneof of messages does not anchor 'a"
);
for (name, ty, label) in [
("MixedString", Type::TYPE_STRING, "string"),
("MixedBytes", Type::TYPE_BYTES, "bytes"),
] {
let content = generate_lean(
vec![DescriptorProto {
name: Some(name.to_string()),
field: vec![
oneof_field("tag", 1, ty, None, 0),
oneof_field("node", 2, Type::TYPE_MESSAGE, Some(name), 0),
],
oneof_decl: vec![oneof_decl("payload")],
..Default::default()
}],
false,
);
assert!(
!struct_body(&content, &format!("pub struct {name}View<'a>"))
.contains("__buffa_phantom"),
"a oneof with a {label} variant anchors 'a itself"
);
}
let content = generate_lean(
vec![
DescriptorProto {
name: Some("IndirectA".to_string()),
field: vec![message_field("other", 1, "IndirectB")],
..Default::default()
},
DescriptorProto {
name: Some("IndirectB".to_string()),
field: vec![message_field("other", 1, "IndirectA")],
..Default::default()
},
],
false,
);
for name in ["IndirectAView", "IndirectBView"] {
assert!(
struct_body(&content, &format!("pub struct {name}<'a>")).contains("__buffa_phantom"),
"{name} sits in a reference cycle and must anchor 'a itself"
);
}
let content = generate_lean(
vec![DescriptorProto {
name: Some("Outer".to_string()),
field: vec![message_field("child", 1, "Outer.Inner")],
nested_type: vec![DescriptorProto {
name: Some("Inner".to_string()),
field: vec![message_field("back", 1, "Outer")],
..Default::default()
}],
..Default::default()
}],
false,
);
assert!(
struct_body(&content, "pub struct OuterView<'a>").contains("__buffa_phantom"),
"a cycle through a nested message still leaves Outer unanchored"
);
}
#[test]
fn test_lean_lazy_views_need_no_marker_for_message_fields() {
let content = generate_lean(
vec![
DescriptorProto {
name: Some("Node".to_string()),
field: vec![message_field("next", 1, "Node")],
..Default::default()
},
DescriptorProto {
name: Some("Tree".to_string()),
field: vec![
oneof_field("left", 1, Type::TYPE_MESSAGE, Some("Tree"), 0),
oneof_field("leaf", 2, Type::TYPE_INT32, None, 0),
],
oneof_decl: vec![oneof_decl("shape")],
..Default::default()
},
],
true,
);
for name in ["NodeView", "TreeView"] {
assert!(
struct_body(&content, &format!("pub struct {name}<'a>")).contains("__buffa_phantom"),
"the eager view is still generated under lazy_views(true) and still needs the marker"
);
}
for name in ["NodeLazyView", "TreeLazyView"] {
assert!(
!struct_body(&content, &format!("pub struct {name}<'a>")).contains("__buffa_phantom"),
"{name} anchors 'a through LazyMessageFieldView, so it must not gain the marker"
);
}
}
#[test]
fn test_lean_marker_follows_the_per_message_preservation_scope() {
let self_referencing = |name: &str| DescriptorProto {
name: Some(name.to_string()),
field: vec![message_field("next", 1, name)],
..Default::default()
};
let mut file = proto3_file("lean.proto");
file.package = Some("pkg".to_string());
file.message_type = vec![self_referencing("Keep"), self_referencing("Drop")];
let config = CodeGenConfig {
generate_views: true,
preserve_unknown_fields: false,
preserve_unknown_fields_in: vec![(".pkg.Keep".to_string(), true)],
..Default::default()
};
let files = generate(&[file], &["lean.proto".to_string()], &config).expect("should generate");
let content = joined(&files);
let keep = struct_body(&content, "pub struct KeepView<'a>");
assert!(
keep.contains("__buffa_unknown_fields") && !keep.contains("__buffa_phantom"),
"a preserving message anchors 'a through its unknown fields, not the marker"
);
let drop = struct_body(&content, "pub struct DropView<'a>");
assert!(
drop.contains("__buffa_phantom") && !drop.contains("__buffa_unknown_fields"),
"a non-preserving self-reference still needs the marker"
);
}