Mizan codegen substrate: Rust kernel + Rust codegen binary, JS generator deleted
The Mizan codegen substrate moves off JavaScript template-literal emission
onto a compiled Rust binary that consumes the same OpenAPI + x-mizan-* IR
the JS substrate consumed. Three structural wins fall out of one move:
1. Moat closes. The codegen logic (how `affects` becomes auto-invalidation,
how named contexts collapse onto bundled fetches, how the registry-to-
Provider mapping is shaped) ships compiled instead of as source bytes
in every consumer's node_modules.
2. Pattern F (lines.push append-walls) becomes structurally unauthorable.
The emit substrate is askama templates in templates/<target>/*.j2 —
actual target-language files with {{ ... }} substitution markers,
syntax-highlighted natively, type-checked against the render context
structs at compile time. The Rust emit modules build typed render
contexts and call .render(); no string-builder surface exists.
3. OpenAPI `default`-bearing fields now emit as non-optional in TS / Python
/ Rust — the server always populates them, so consumer code reads them
without nullable checks. Surfaced by Blazr's typecheck on regeneration.
Layout:
frontends/mizan-rust/ — Rust port of @mizan/base; #[cfg(feature="pyo3")]
exposes PyMizanClient for the Python target.
protocol/mizan-codegen/ — codegen binary source + askama templates.
protocol/mizan-generate/ — npm-package shim. bin/launcher.mjs dispatches
to the platform-appropriate prebuilt binary.
Old generator/ JS tree deleted.
tests/rust/ — wire-parity drivers. drive_kernel exercises
raw client.call() / fetch_context(); drive_emitted
exercises the typed crate the codegen emits.
tests/afi/afi_codegen_app.py — codegen entrypoint module (imports + registers).
backends/mizan-fastapi/.../schema.py — adds outputNullable so the Rust
codegen can wrap T | None responses in Option<T>.
Verification:
- 20 mizan-codegen tests green (IR deserialization, byte-equivalent
parity vs JS baseline for stage1/rust/python/react/vue/svelte,
structural test for channels).
- tests/rust/run_wire_parity.py — 12/12 probes green via the Rust binary
driving the FastAPI fixture end-to-end.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
11
protocol/mizan-codegen/templates/rust/Cargo.toml.j2
Normal file
11
protocol/mizan-codegen/templates/rust/Cargo.toml.j2
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "{{ crate_name }}"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
mizan-rust = {{ kernel_dep }}
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
tokio = { version = "1", features = ["rt", "macros"] }
|
||||
|
||||
17
protocol/mizan-codegen/templates/rust/call.rs.j2
Normal file
17
protocol/mizan-codegen/templates/rust/call.rs.j2
Normal file
@@ -0,0 +1,17 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use mizan_rust::{MizanClient, MizanError};
|
||||
|
||||
{% if !type_imports.is_empty() -%}
|
||||
use crate::types::{ {{- type_imports|join(", ") -}} };
|
||||
|
||||
{% endif -%}
|
||||
pub async fn call_{{ snake }}(client: &MizanClient{{ input_param }}) -> Result<{{ return_type }}, MizanError> {
|
||||
let args_value = {{ args_value }};
|
||||
let raw = client.call("{{ name }}", args_value).await?;
|
||||
serde_json::from_value(raw)
|
||||
.map_err(|e| MizanError::transport(format!("decode {{ name }} result: {e}")))
|
||||
}
|
||||
|
||||
37
protocol/mizan-codegen/templates/rust/context.rs.j2
Normal file
37
protocol/mizan-codegen/templates/rust/context.rs.j2
Normal file
@@ -0,0 +1,37 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use mizan_rust::{MizanClient, MizanError};
|
||||
|
||||
{% if !type_imports.is_empty() -%}
|
||||
use crate::types::{ {{- type_imports|join(", ") -}} };
|
||||
|
||||
{% endif -%}
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct {{ pascal }}ContextData {
|
||||
{% for field in data_fields -%}
|
||||
{% if field.has_rename %} #[serde(rename = "{{ field.raw_name }}")]
|
||||
{% endif %} pub {{ field.ident }}: {{ field.ty }},
|
||||
{% endfor -%}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct {{ pascal }}ContextParams {
|
||||
{% for p in params -%}
|
||||
{% if p.has_rename %} #[serde(rename = "{{ p.raw_name }}")]
|
||||
{% endif %} pub {{ p.ident }}: {{ p.ty }},
|
||||
{% endfor -%}
|
||||
}
|
||||
|
||||
pub async fn fetch_{{ snake }}_context(
|
||||
client: &MizanClient,
|
||||
params: &{{ pascal }}ContextParams,
|
||||
) -> Result<{{ pascal }}ContextData, MizanError> {
|
||||
let params_value = serde_json::to_value(params).unwrap_or(Value::Object(Default::default()));
|
||||
let raw = client.fetch_context("{{ ctx_name }}", ¶ms_value).await?;
|
||||
serde_json::from_value(raw)
|
||||
.map_err(|e| MizanError::transport(format!("decode {{ ctx_name }} context: {e}")))
|
||||
}
|
||||
|
||||
9
protocol/mizan-codegen/templates/rust/lib.rs.j2
Normal file
9
protocol/mizan-codegen/templates/rust/lib.rs.j2
Normal file
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
pub mod types;
|
||||
{% if has_contexts %}pub mod contexts;
|
||||
{% endif %}{% if has_mutations %}pub mod mutations;
|
||||
{% endif %}{% if has_functions %}pub mod functions;
|
||||
{% endif %}
|
||||
pub use mizan_rust::{MizanClient, MizanConfig, MizanError};
|
||||
|
||||
5
protocol/mizan-codegen/templates/rust/mod.rs.j2
Normal file
5
protocol/mizan-codegen/templates/rust/mod.rs.j2
Normal file
@@ -0,0 +1,5 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
{% for name in modules -%}
|
||||
pub mod {{ name }};
|
||||
{% endfor %}
|
||||
8
protocol/mizan-codegen/templates/rust/types.rs.j2
Normal file
8
protocol/mizan-codegen/templates/rust/types.rs.j2
Normal file
@@ -0,0 +1,8 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
#![allow(non_camel_case_types)]
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
{{ schemas_block }}
|
||||
{{ hoisted_enums_block }}
|
||||
Reference in New Issue
Block a user