Skip to content

Commit 3db2d37

Browse files
Rusty Conoverclaude
andcommitted
codegen: rust_types — generate vgi-rust record structs from ARROW_SCHEMA
New emitter vgi.codegen.rust_types, modelled on csharp_types: emits #[derive(Debug, Clone, VgiArrow)] structs in vgi-rust's existing DTO idiom (Bytes / Vec<Bytes> / Option<T> for nullable / StrMap / DictString ...) into vgi-protocol/src/generated/protocol_types.rs. Data-driven via RUST_TYPES (CatalogAttachResult, SchemaContents, CatalogContentsResponse for now); an Arrow type with no mapping raises GeneratorError. The emitted file carries a schema_parity #[test] asserting each struct's derived schema equals its protocol_schemas.rs factory, so the derive is checked by the Rust compiler. Registered in scripts/regen_generated.py; tests/test_generated_rust_types.py adds determinism, shape and drift checks (VGI_RUST_GENERATED_TYPES_RS overrides the checkout path). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent b13832f commit 3db2d37

3 files changed

Lines changed: 420 additions & 0 deletions

File tree

‎scripts/regen_generated.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
("vgi.codegen.go_types", "vgi/generated/protocol_types.go", "vgi-go"),
5555
("vgi.codegen.rust_schemas", "vgi-protocol/src/generated/protocol_schemas.rs", "vgi-rust"),
5656
("vgi.codegen.rust_request_builders", "vgi-protocol/src/generated/request_params.rs", "vgi-rust"),
57+
("vgi.codegen.rust_types", "vgi-protocol/src/generated/protocol_types.rs", "vgi-rust"),
5758
("vgi.codegen.ts_schemas", "src/generated/vgi-protocol-schemas.ts", "vgi-typescript"),
5859
("vgi.codegen.ts_client", "src/generated/vgi-client.ts", "vgi-typescript"),
5960
("vgi.codegen.csharp_types", "src/QueryFarm.Vgi/Protocol/Generated/VgiProtocolTypes.g.cs", "vgi-csharp"),

‎tests/test_generated_rust_types.py‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright 2025, 2026 Query Farm LLC - https://query.farm
2+
3+
"""Drift + behavior tests for `vgi.codegen.rust_types`.
4+
5+
The deep correctness check — that each generated struct's `#[derive(VgiArrow)]`
6+
schema equals its `protocol_schemas.rs` factory — lives on the Rust side, in the
7+
`schema_parity` module the generator emits into `protocol_types.rs` itself: only
8+
the Rust compiler can run the derive. From here we check the shape of the
9+
emission, and that the checked-in file is current.
10+
11+
Skipped when the sibling `vgi-rust` checkout is not present;
12+
``VGI_RUST_GENERATED_TYPES_RS`` overrides the location.
13+
"""
14+
15+
from __future__ import annotations
16+
17+
import io
18+
import os
19+
import re
20+
from pathlib import Path
21+
22+
import pytest
23+
24+
from vgi.codegen import rust_types
25+
from vgi.codegen.rust_schemas import emit as emit_schemas
26+
27+
_REGEN_HINT = (
28+
"To regenerate, run:\n"
29+
" uv run --project ~/Development/vgi-python python scripts/regen_generated.py\n"
30+
"\n"
31+
"Do not redirect a generator into its destination with `>`: the shell\n"
32+
"truncates the file before the generator runs, so any failure destroys\n"
33+
"the checked-in artifact. The script renders to memory first."
34+
)
35+
36+
37+
def _vgi_rust_generated_path() -> Path:
38+
override = os.environ.get("VGI_RUST_GENERATED_TYPES_RS")
39+
if override:
40+
return Path(override)
41+
return Path(__file__).resolve().parents[2] / "vgi-rust" / "vgi-protocol" / "src" / "generated" / "protocol_types.rs"
42+
43+
44+
def _emitted() -> str:
45+
buf = io.StringIO()
46+
rust_types.emit(buf)
47+
return buf.getvalue()
48+
49+
50+
def test_generator_is_deterministic() -> None:
51+
"""Calling emit() twice produces byte-identical output."""
52+
assert _emitted() == _emitted(), "rust_types generator is non-deterministic"
53+
54+
55+
def test_every_record_is_emitted_with_the_derive() -> None:
56+
"""Each RUST_TYPES record becomes a `#[derive(VgiArrow)]` struct."""
57+
src = _emitted()
58+
for cls in rust_types.RUST_TYPES:
59+
assert f"#[derive(Debug, Clone, VgiArrow)]\npub struct {cls.__name__} {{" in src, cls.__name__
60+
61+
62+
def test_fields_follow_arrow_schema_order_and_nullability() -> None:
63+
"""Field order is column order; nullable columns are Option<T>."""
64+
for struct, cls in zip(rust_types.build_model(), rust_types.RUST_TYPES, strict=True):
65+
schema = cls.ARROW_SCHEMA # type: ignore[attr-defined]
66+
assert [f.wire_name for f in struct.fields] == schema.names
67+
for f, arrow in zip(struct.fields, schema, strict=True):
68+
assert f.rust_type.startswith("Option<") == arrow.nullable, (struct.name, f.wire_name)
69+
70+
71+
def test_known_shapes() -> None:
72+
"""Spot-check the mapping on the records vgi-rust consumes."""
73+
by_name = {s.name: {f.wire_name: f.rust_type for f in s.fields} for s in rust_types.build_model()}
74+
attach = by_name["CatalogAttachResult"]
75+
assert attach["attach_opaque_data"] == "Bytes"
76+
assert attach["comment"] == "Option<String>"
77+
assert attach["tags"] == "StrMap"
78+
assert attach["settings"] == "Vec<Bytes>"
79+
assert attach["supports_catalog_contents"] == "bool"
80+
assert by_name["CatalogContentsResponse"] == {"catalog_version": "i64", "schemas": "Vec<Bytes>"}
81+
assert by_name["SchemaContents"]["schema"] == "Bytes"
82+
83+
84+
def test_parity_module_covers_every_struct_against_an_existing_factory() -> None:
85+
"""The emitted Rust test asserts on every struct, against a factory rust_schemas emits."""
86+
src = _emitted()
87+
structs = set(re.findall(r"^pub struct (\w+) \{", src, re.MULTILINE))
88+
asserted = set(re.findall(r"flat_schema::<(\w+)>\(\)", src))
89+
assert structs == asserted, f"schema_parity does not cover: {sorted(structs - asserted)}"
90+
91+
buf = io.StringIO()
92+
emit_schemas(buf)
93+
factories = set(re.findall(r"pub fn (\w+_schema)\(\) -> SchemaRef", buf.getvalue()))
94+
used = set(re.findall(r"protocol_schemas::(\w+_schema)\(\)", src))
95+
assert used <= factories, f"schema_parity names factories rust_schemas does not emit: {sorted(used - factories)}"
96+
97+
98+
def test_no_python_doc_markup_leaks_into_rustdoc() -> None:
99+
"""Python doc markup (mkdocstrings cross-refs, RST roles) would be broken intra-doc links in rustdoc."""
100+
src = _emitted()
101+
assert ":class:" not in src
102+
assert "][]" not in src
103+
assert "``" not in src
104+
105+
106+
def test_checked_in_rust_matches_generator() -> None:
107+
"""The file committed in vgi-rust must equal what the generator emits now."""
108+
path = _vgi_rust_generated_path()
109+
if not path.exists():
110+
pytest.skip(f"vgi-rust checkout not found at {path}")
111+
assert path.read_text(encoding="utf-8") == _emitted(), f"{path} is stale.\n\n{_REGEN_HINT}"

0 commit comments

Comments
 (0)