A channel's message slots are named from the client, on every backend
The IR called them react-message and django-message, so a FastAPI channel had to declare a DjangoMessage. They are client-message and server-message now, and the direction words hold wherever a channel is declared: Params / ClientMessage / ServerMessage, with mizan-core deriving <Pascal>Params and friends so no backend names a type itself. Django's ReactChannel and FastAPI's ReactChannel are both Channel. mizan-fastapi never registered a channels extension, so build_ir() emitted no channel at all and every payload type was invisible to codegen. It registers one now. RegistryExtension is an ABC requiring all(), which is what the IR reads — an extension that cannot enumerate its registrations no longer exists. The gate that should have caught the rename could not: tests/afi registered no channel because mizan-rust had no channel registry to register one in, so a five-package rename of the wire contract passed byte-parity without a channel byte crossing it. mizan-rust grows ChannelSlotKind, a CHANNELS slice, a #[mizan::channel] macro, and KDL emission whose wire_to_pascal matches Python's split; the AFI fixture now carries a channel with every slot and one with a single slot, so all three backends prove the contract byte for byte. MizanChannel held three Option<String> beside three has_*() predicates and unwrapped them with defaults; it holds an ordered slot vector, so an absent slot is absent rather than defaulted. The channels target emitted a React hooks file that a stage1-only consumer could not compile — react emits that now. The codegen's parity tests byte-compared emitted source against baselines without ever compiling it: they compile the generated crate and run its tests, import the generated Python package and call every method, and typecheck each TypeScript target against a consumer. Also fixed at source: app_visitor printed its import diagnostic to stdout, the stream export_mizan_ir writes KDL to, so a failed import silently corrupted the IR; the apps root was hardcoded to "apps"; _default_literal crashed build_ir on any non-JSON-serializable field default; Django and mizan-core derived Pascal names two different ways, disagreeing on every dotted channel name. ir.py builds a document and renders templates/ir/document.kdl.j2 rather than appending KDL strings with hand-tracked indentation, and named types resolve to a fixed point — a model reachable only through a union branch was referenced by a ref that no type block ever defined. The rest is the write-gate's own classifiers run over the standing tree: relative imports, silent swallows, Protocol contracts that should be ABCs, emitters hand-rendering target source, catch-all arms over closed enums, and comments narrating the project rather than the code. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
17
tests/afi/rust_app/Cargo.lock
generated
17
tests/afi/rust_app/Cargo.lock
generated
@@ -301,12 +301,28 @@ version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||
|
||||
[[package]]
|
||||
name = "memo-map"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38d1115007560874e373613744c6fba374c17688327a71c1476d1a5954cc857b"
|
||||
|
||||
[[package]]
|
||||
name = "mime"
|
||||
version = "0.3.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
|
||||
|
||||
[[package]]
|
||||
name = "minijinja"
|
||||
version = "2.21.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cb3d648e68cea56d9858d535ee28f9538404e2dd8cb08ed0bd05dca379477f39"
|
||||
dependencies = [
|
||||
"memo-map",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mio"
|
||||
version = "1.2.0"
|
||||
@@ -337,6 +353,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"linkme",
|
||||
"minijinja",
|
||||
"mizan-macros",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
//! Emit the AFI fixture's Mizan IR (KDL) to stdout. The codegen subprocess
|
||||
//! consumes this; the three-way codegen-parity test asserts it equals what
|
||||
//! Django and FastAPI emit.
|
||||
//! Print the registered fixture's Mizan IR (KDL) to stdout.
|
||||
|
||||
fn main() {
|
||||
// The fixture is registered via the `afi_rust_app` library crate at
|
||||
// link time — referencing any symbol keeps the linkme statics alive.
|
||||
// The fixture registers through linkme statics in the library crate;
|
||||
// referencing a symbol keeps the linker from dropping the crate.
|
||||
let _ = afi_rust_app::echo;
|
||||
print!("{}", mizan_core::build_ir());
|
||||
}
|
||||
|
||||
@@ -1,18 +1,21 @@
|
||||
//! Serve the AFI fixture under axum on `PORT` env var (or 8765 default).
|
||||
//! Used by the wire-parity test as the third-backend probe target.
|
||||
//! Serve the AFI fixture under axum on the port named by `PORT`, else 8765.
|
||||
|
||||
use axum::Router;
|
||||
use std::env::VarError;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Keep the fixture's linkme statics alive by touching one of its
|
||||
// symbols (the bin would otherwise dead-strip the library crate).
|
||||
// The fixture registers through linkme statics in the library crate;
|
||||
// referencing a symbol keeps the linker from dropping the crate.
|
||||
let _ = afi_rust_app::echo;
|
||||
|
||||
let port: u16 = std::env::var("PORT")
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.unwrap_or(8765);
|
||||
let port: u16 = match std::env::var("PORT") {
|
||||
Ok(raw) => raw
|
||||
.parse()
|
||||
.unwrap_or_else(|e| panic!("PORT={raw:?} is not a port number: {e}")),
|
||||
Err(VarError::NotPresent) => 8765,
|
||||
Err(VarError::NotUnicode(raw)) => panic!("PORT={raw:?} is not valid unicode"),
|
||||
};
|
||||
|
||||
let app = Router::new().nest("/api/mizan", mizan_axum::router_stateless());
|
||||
|
||||
@@ -21,5 +24,7 @@ async fn main() {
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("bind {bind}: {e}"));
|
||||
eprintln!("afi_rust_app listening on http://{bind}");
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
axum::serve(listener, app)
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("serve {bind}: {e}"));
|
||||
}
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
//! AFI fixture — Rust port of `tests/afi/fixture.py`.
|
||||
//!
|
||||
//! Same 7 functions, same 5 shared types, same context+affects+merge graph.
|
||||
//! The KDL emitted by `build_ir()` against this registry is byte-identical
|
||||
//! to the canonical Python-emitted `protocol/mizan-codegen/tests/fixtures/
|
||||
//! afi_ir.kdl` — gated by the three-way codegen-parity test.
|
||||
//! The AFI fixture for the Rust backend: seven client functions over the
|
||||
//! stored rows below, one `user` context reached by `context`/`affects`/
|
||||
//! `merge`, and two channels over their payload shapes.
|
||||
|
||||
use mizan_core as mizan;
|
||||
use mizan_core::prelude::*;
|
||||
@@ -39,9 +36,65 @@ pub struct StatusOutput {
|
||||
pub ok: bool,
|
||||
}
|
||||
|
||||
#[derive(Mizan, Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct ChatParams {
|
||||
pub room_id: String,
|
||||
}
|
||||
|
||||
#[derive(Mizan, Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct ChatClientMessage {
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
#[derive(Mizan, Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct ChatServerMessage {
|
||||
pub text: String,
|
||||
pub from_user: String,
|
||||
}
|
||||
|
||||
#[derive(Mizan, Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct UserAlertsServerMessage {
|
||||
pub body: String,
|
||||
pub unread: i64,
|
||||
}
|
||||
|
||||
/// The rows the fixture serves, as `(user id, display name)`.
|
||||
const STORED_PROFILES: &[(i64, &str)] = &[(5, "Ada Lovelace"), (6, "Grace Hopper")];
|
||||
|
||||
const STORED_ORDERS: &[OrderOutput] = &[
|
||||
OrderOutput {
|
||||
id: 1,
|
||||
user_id: 5,
|
||||
total: 1200,
|
||||
},
|
||||
OrderOutput {
|
||||
id: 2,
|
||||
user_id: 5,
|
||||
total: 350,
|
||||
},
|
||||
];
|
||||
|
||||
fn stored_name(user_id: i64) -> Option<&'static str> {
|
||||
STORED_PROFILES
|
||||
.iter()
|
||||
.find(|(stored_id, _)| *stored_id == user_id)
|
||||
.map(|(_, name)| *name)
|
||||
}
|
||||
|
||||
#[mizan::context("user")]
|
||||
pub struct UserCtx;
|
||||
|
||||
#[mizan::channel(
|
||||
"chat",
|
||||
params = ChatParams,
|
||||
client_message = ChatClientMessage,
|
||||
server_message = ChatServerMessage
|
||||
)]
|
||||
pub struct ChatChannel;
|
||||
|
||||
#[mizan::channel("user_alerts", server_message = UserAlertsServerMessage)]
|
||||
pub struct UserAlertsChannel;
|
||||
|
||||
#[mizan::client]
|
||||
pub async fn echo(_req: &RequestHandle<'_>, text: String) -> EchoOutput {
|
||||
EchoOutput {
|
||||
@@ -59,29 +112,41 @@ pub async fn whoami(_req: &RequestHandle<'_>) -> WhoamiOutput {
|
||||
|
||||
#[mizan::client(context = UserCtx)]
|
||||
pub async fn user_profile(_req: &RequestHandle<'_>, user_id: i64) -> ProfileOutput {
|
||||
ProfileOutput {
|
||||
user_id,
|
||||
name: "placeholder".into(),
|
||||
}
|
||||
// A context member always answers, so an unstored id reads back under a
|
||||
// name derived from the id rather than an absence.
|
||||
let name = match stored_name(user_id) {
|
||||
Some(stored) => stored.to_string(),
|
||||
None => format!("user {user_id}"),
|
||||
};
|
||||
ProfileOutput { user_id, name }
|
||||
}
|
||||
|
||||
#[mizan::client(context = UserCtx)]
|
||||
pub async fn user_orders(_req: &RequestHandle<'_>, _user_id: i64) -> Vec<OrderOutput> {
|
||||
vec![]
|
||||
pub async fn user_orders(_req: &RequestHandle<'_>, user_id: i64) -> Vec<OrderOutput> {
|
||||
STORED_ORDERS
|
||||
.iter()
|
||||
.filter(|order| order.user_id == user_id)
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[mizan::client(affects = UserCtx)]
|
||||
pub async fn update_profile(
|
||||
_req: &RequestHandle<'_>,
|
||||
_user_id: i64,
|
||||
_name: String,
|
||||
user_id: i64,
|
||||
name: String,
|
||||
) -> StatusOutput {
|
||||
StatusOutput { ok: true }
|
||||
StatusOutput {
|
||||
ok: !name.is_empty() && stored_name(user_id).is_some(),
|
||||
}
|
||||
}
|
||||
|
||||
#[mizan::client]
|
||||
pub async fn find_user(_req: &RequestHandle<'_>, _user_id: i64) -> Option<ProfileOutput> {
|
||||
None
|
||||
pub async fn find_user(_req: &RequestHandle<'_>, user_id: i64) -> Option<ProfileOutput> {
|
||||
stored_name(user_id).map(|stored| ProfileOutput {
|
||||
user_id,
|
||||
name: stored.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
#[mizan::client(merge = UserCtx)]
|
||||
@@ -90,5 +155,7 @@ pub async fn rename_user(
|
||||
user_id: i64,
|
||||
name: String,
|
||||
) -> ProfileOutput {
|
||||
// The merge target's return value is what the kernel splices into the
|
||||
// `user` context, so the renamed profile is the whole result.
|
||||
ProfileOutput { user_id, name }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user