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:
1
protocol/mizan-codegen/.gitignore
vendored
Normal file
1
protocol/mizan-codegen/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
target/
|
||||
470
protocol/mizan-codegen/Cargo.lock
generated
Normal file
470
protocol/mizan-codegen/Cargo.lock
generated
Normal file
@@ -0,0 +1,470 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"anstyle-parse",
|
||||
"anstyle-query",
|
||||
"anstyle-wincon",
|
||||
"colorchoice",
|
||||
"is_terminal_polyfill",
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle"
|
||||
version = "1.0.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-parse"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
|
||||
dependencies = [
|
||||
"utf8parse",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-query"
|
||||
version = "1.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
|
||||
dependencies = [
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstyle-wincon"
|
||||
version = "3.0.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
|
||||
dependencies = [
|
||||
"anstyle",
|
||||
"once_cell_polyfill",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.102"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7f202df86484c868dbad7eaa557ef785d5c66295e41b460ef922eca0723b842c"
|
||||
|
||||
[[package]]
|
||||
name = "askama"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b79091df18a97caea757e28cd2d5fda49c6cd4bd01ddffd7ff01ace0c0ad2c28"
|
||||
dependencies = [
|
||||
"askama_derive",
|
||||
"askama_escape",
|
||||
"humansize",
|
||||
"num-traits",
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "askama_derive"
|
||||
version = "0.12.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "19fe8d6cb13c4714962c072ea496f3392015f0989b1a2847bb4b2d9effd71d83"
|
||||
dependencies = [
|
||||
"askama_parser",
|
||||
"basic-toml",
|
||||
"mime",
|
||||
"mime_guess",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "askama_escape"
|
||||
version = "0.10.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341"
|
||||
|
||||
[[package]]
|
||||
name = "askama_parser"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "acb1161c6b64d1c3d83108213c2a2533a342ac225aabd0bda218278c2ddb00c0"
|
||||
dependencies = [
|
||||
"nom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
|
||||
|
||||
[[package]]
|
||||
name = "basic-toml"
|
||||
version = "0.1.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba62675e8242a4c4e806d12f11d136e626e6c8361d6b829310732241652a178a"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51"
|
||||
dependencies = [
|
||||
"clap_builder",
|
||||
"clap_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_builder"
|
||||
version = "4.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
"clap_lex",
|
||||
"strsim",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_lex"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
|
||||
|
||||
[[package]]
|
||||
name = "colorchoice"
|
||||
version = "1.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.17.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "humansize"
|
||||
version = "2.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6cb51c9a029ddc91b07a787f1d86b53ccfa49b0e86688c946ebe8d3555685dd7"
|
||||
dependencies = [
|
||||
"libm",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "2.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9"
|
||||
dependencies = [
|
||||
"equivalent",
|
||||
"hashbrown",
|
||||
"serde",
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "is_terminal_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||
|
||||
[[package]]
|
||||
name = "libm"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6d2cec3eae94f9f509c767b45932f1ada8350c4bdb85af2fcab4a3c14807981"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||
|
||||
[[package]]
|
||||
name = "mime"
|
||||
version = "0.3.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
|
||||
|
||||
[[package]]
|
||||
name = "mime_guess"
|
||||
version = "2.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e"
|
||||
dependencies = [
|
||||
"mime",
|
||||
"unicase",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||
|
||||
[[package]]
|
||||
name = "mizan-codegen"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"askama",
|
||||
"clap",
|
||||
"indexmap",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "7.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"minimal-lexical",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell_polyfill"
|
||||
version = "1.70.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.149"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_spanned"
|
||||
version = "0.6.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bf41e0cfaf7226dca15e8197172c295a782857fcb97fad1808a166870dee75a3"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.117"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.8.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc1beb996b9d83529a9e75c17a1686767d148d70663143c7854d8b4a09ced362"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"toml_edit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_datetime"
|
||||
version = "0.6.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22cddaf88f4fbc13c51aebbf5f8eceb5c7c5a9da2ac40a13519eb5b0a0e8f11c"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_edit"
|
||||
version = "0.22.27"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41fe8c660ae4257887cf66394862d21dbca4a6ddd26f04a3560410406a2f819a"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"serde",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"toml_write",
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_write"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5d99f8c9a7727884afe522e9bd5edbfc91a3312b36a77b5fb8926e4c31a41801"
|
||||
|
||||
[[package]]
|
||||
name = "unicase"
|
||||
version = "2.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "utf8parse"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.61.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "0.7.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df79d97927682d2fd8adb29682d1140b343be4ac0f08fd68b7765d9c059d3945"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
|
||||
22
protocol/mizan-codegen/Cargo.toml
Normal file
22
protocol/mizan-codegen/Cargo.toml
Normal file
@@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "mizan-codegen"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
description = "Mizan codegen substrate — consumes Mizan IR; emits typed clients for React/Vue/Svelte/Rust/Python."
|
||||
license = "MIT"
|
||||
|
||||
[[bin]]
|
||||
name = "mizan-generate"
|
||||
path = "src/main.rs"
|
||||
|
||||
[lib]
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
askama = "0.12"
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = { version = "1", features = ["preserve_order"] }
|
||||
toml = "0.8"
|
||||
anyhow = "1"
|
||||
indexmap = { version = "2", features = ["serde"] }
|
||||
119
protocol/mizan-codegen/src/config.rs
Normal file
119
protocol/mizan-codegen/src/config.rs
Normal file
@@ -0,0 +1,119 @@
|
||||
//! Codegen configuration — deserialized from `mizan.toml` at the consumer
|
||||
//! project root. Replaces the JS substrate's `mizan.config.mjs`.
|
||||
//!
|
||||
//! Example:
|
||||
//!
|
||||
//! ```toml
|
||||
//! project_id = "blazr-studio"
|
||||
//! output = "src/api"
|
||||
//! targets = ["react"]
|
||||
//!
|
||||
//! [source.fastapi]
|
||||
//! module = "blazr_session.handlers"
|
||||
//! cwd = "../.."
|
||||
//! command = ["uv", "run", "python"]
|
||||
//!
|
||||
//! [rust_kernel]
|
||||
//! path = "../../mizan/frontends/mizan-rust"
|
||||
//! ```
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
#[serde(default)]
|
||||
pub project_id: Option<String>,
|
||||
|
||||
#[serde(default = "default_output")]
|
||||
pub output: PathBuf,
|
||||
|
||||
#[serde(default = "default_targets")]
|
||||
pub targets: Vec<String>,
|
||||
|
||||
#[serde(default)]
|
||||
pub source: SourceConfig,
|
||||
|
||||
#[serde(default)]
|
||||
pub rust_kernel: Option<RustKernelSpec>,
|
||||
|
||||
#[serde(default)]
|
||||
pub rust_crate_name: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
fn default_output() -> PathBuf {
|
||||
PathBuf::from("src/api")
|
||||
}
|
||||
|
||||
|
||||
fn default_targets() -> Vec<String> {
|
||||
vec!["react".to_string()]
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize, Default)]
|
||||
pub struct SourceConfig {
|
||||
#[serde(default)]
|
||||
pub fastapi: Option<FastapiSource>,
|
||||
|
||||
#[serde(default)]
|
||||
pub django: Option<DjangoSource>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct FastapiSource {
|
||||
pub module: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub cwd: Option<PathBuf>,
|
||||
|
||||
#[serde(default)]
|
||||
pub python: Option<String>,
|
||||
|
||||
#[serde(default)]
|
||||
pub command: Option<Vec<String>>,
|
||||
|
||||
#[serde(default)]
|
||||
pub env: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct DjangoSource {
|
||||
pub manage_path: PathBuf,
|
||||
|
||||
#[serde(default)]
|
||||
pub python: Option<String>,
|
||||
|
||||
#[serde(default)]
|
||||
pub command: Option<Vec<String>>,
|
||||
|
||||
#[serde(default)]
|
||||
pub env: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
#[serde(untagged)]
|
||||
pub enum RustKernelSpec {
|
||||
Path {
|
||||
path: String,
|
||||
},
|
||||
Git {
|
||||
git: String,
|
||||
#[serde(default)]
|
||||
tag: Option<String>,
|
||||
#[serde(default)]
|
||||
rev: Option<String>,
|
||||
#[serde(default)]
|
||||
branch: Option<String>,
|
||||
},
|
||||
Version {
|
||||
version: String,
|
||||
},
|
||||
}
|
||||
137
protocol/mizan-codegen/src/emit/casing.rs
Normal file
137
protocol/mizan-codegen/src/emit/casing.rs
Normal file
@@ -0,0 +1,137 @@
|
||||
//! Casing transforms — port of `protocol/mizan-generate/generator/lib/casing.mjs`.
|
||||
//!
|
||||
//! The Mizan IR uses snake_case names (`user_id`, `update_profile`). Per-target
|
||||
//! identifier conventions vary: TypeScript wants `pascalCase`/`camelCase`,
|
||||
//! Rust wants `snake_case` (with `r#`-escaping for keywords). These helpers
|
||||
//! pin the conversion so emit-targets share one vocabulary.
|
||||
|
||||
|
||||
fn split_parts(s: &str) -> Vec<&str> {
|
||||
s.split(|c: char| c == '.' || c == '-' || c == '_')
|
||||
.filter(|p| !p.is_empty())
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
||||
fn uppercase_first(s: &str) -> String {
|
||||
let mut chars = s.chars();
|
||||
match chars.next() {
|
||||
Some(first) => first.to_uppercase().chain(chars).collect(),
|
||||
None => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn lowercase_first(s: &str) -> String {
|
||||
let mut chars = s.chars();
|
||||
match chars.next() {
|
||||
Some(first) => first.to_lowercase().chain(chars).collect(),
|
||||
None => String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn pascal_case(s: &str) -> String {
|
||||
split_parts(s).into_iter().map(uppercase_first).collect()
|
||||
}
|
||||
|
||||
|
||||
pub fn camel_case(s: &str) -> String {
|
||||
let pascal = pascal_case(s);
|
||||
lowercase_first(&pascal)
|
||||
}
|
||||
|
||||
|
||||
/// Insert underscores at lowercase/digit-to-uppercase boundaries, unify with
|
||||
/// the existing `.`/`-`/`_` separators, then lowercase + join.
|
||||
pub fn snake_case(s: &str) -> String {
|
||||
let mut with_boundaries = String::with_capacity(s.len() + 4);
|
||||
let mut prev: Option<char> = None;
|
||||
for c in s.chars() {
|
||||
if let Some(p) = prev {
|
||||
if (p.is_ascii_lowercase() || p.is_ascii_digit()) && c.is_ascii_uppercase() {
|
||||
with_boundaries.push('_');
|
||||
}
|
||||
}
|
||||
with_boundaries.push(c);
|
||||
prev = Some(c);
|
||||
}
|
||||
split_parts(&with_boundaries)
|
||||
.into_iter()
|
||||
.map(|p| p.to_ascii_lowercase())
|
||||
.collect::<Vec<_>>()
|
||||
.join("_")
|
||||
}
|
||||
|
||||
|
||||
/// Rust reserved words that can be escaped via `r#` (excludes `crate`, `self`,
|
||||
/// `Self`, `super`, `extern`, which can't be raw-escaped on stable).
|
||||
const RUST_RAW_KEYWORDS: &[&str] = &[
|
||||
"as", "break", "const", "continue", "else", "enum", "false", "fn", "for",
|
||||
"if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub",
|
||||
"ref", "return", "static", "struct", "trait", "true", "type", "unsafe",
|
||||
"use", "where", "while", "async", "await", "dyn", "abstract", "become",
|
||||
"box", "do", "final", "macro", "override", "priv", "typeof", "unsized",
|
||||
"virtual", "yield", "try", "union",
|
||||
];
|
||||
|
||||
const RUST_HARD_RESERVED: &[&str] = &["crate", "self", "Self", "super", "extern"];
|
||||
|
||||
|
||||
pub fn rust_ident(name: &str) -> String {
|
||||
let snake = snake_case(name);
|
||||
if RUST_HARD_RESERVED.contains(&snake.as_str()) {
|
||||
format!("{snake}_")
|
||||
} else if RUST_RAW_KEYWORDS.contains(&snake.as_str()) {
|
||||
format!("r#{snake}")
|
||||
} else {
|
||||
snake
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn rust_type_ident(name: &str) -> String {
|
||||
let pascal = pascal_case(name);
|
||||
if RUST_HARD_RESERVED.contains(&pascal.as_str()) {
|
||||
format!("{pascal}_")
|
||||
} else if RUST_RAW_KEYWORDS.contains(&pascal.as_str()) {
|
||||
format!("r#{pascal}")
|
||||
} else {
|
||||
pascal
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn pascal_case_matches_js_codegen() {
|
||||
assert_eq!(pascal_case("user_profile"), "UserProfile");
|
||||
assert_eq!(pascal_case("find-user"), "FindUser");
|
||||
assert_eq!(pascal_case("api.v1.users"), "ApiV1Users");
|
||||
assert_eq!(pascal_case(""), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn camel_case_matches_js_codegen() {
|
||||
assert_eq!(camel_case("user_profile"), "userProfile");
|
||||
assert_eq!(camel_case("UpdateProfile"), "updateProfile");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snake_case_inserts_pascal_boundaries() {
|
||||
assert_eq!(snake_case("UserProfile"), "user_profile");
|
||||
assert_eq!(snake_case("camelCase"), "camel_case");
|
||||
assert_eq!(snake_case("already_snake"), "already_snake");
|
||||
assert_eq!(snake_case("HTTPResponse"), "httpresponse"); // matches JS behavior
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rust_ident_escapes_keywords() {
|
||||
assert_eq!(rust_ident("type"), "r#type");
|
||||
assert_eq!(rust_ident("normal"), "normal");
|
||||
assert_eq!(rust_ident("self"), "self_");
|
||||
}
|
||||
}
|
||||
163
protocol/mizan-codegen/src/emit/channels.rs
Normal file
163
protocol/mizan-codegen/src/emit/channels.rs
Normal file
@@ -0,0 +1,163 @@
|
||||
//! Channels target — emits `channels.ts` (typed message envelopes + channel
|
||||
//! registry) and `channels.hooks.tsx` (`useXChannel` React hooks) from the
|
||||
//! `x-mizan-channels` extension. Django-only feature; the FastAPI backend's
|
||||
//! IR carries an empty channels list and this target emits nothing.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use askama::Template;
|
||||
use indexmap::IndexMap;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::emit::CodegenTarget;
|
||||
use crate::emit::EmittedFile;
|
||||
use crate::ir::{JsonSchema, MizanChannel, MizanIR};
|
||||
|
||||
|
||||
pub struct ChannelsTarget;
|
||||
|
||||
|
||||
impl CodegenTarget for ChannelsTarget {
|
||||
fn name(&self) -> &'static str { "channels" }
|
||||
|
||||
fn emit(&self, ir: &MizanIR, _config: &Config) -> Vec<EmittedFile> {
|
||||
if ir.channels.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let schemas_block = emit_channel_schemas(&ir.channels, &ir.components.schemas);
|
||||
|
||||
let types_content = ChannelsTypes {
|
||||
channels: ir.channels.iter().map(ChannelView::from_ir).collect(),
|
||||
schemas_block,
|
||||
}.render().expect("channels.ts renders");
|
||||
|
||||
let mut type_imports: Vec<String> = Vec::new();
|
||||
for ch in &ir.channels {
|
||||
if ch.has_params { if let Some(t) = &ch.params_type { type_imports.push(t.clone()); } }
|
||||
if ch.has_react_message { if let Some(t) = &ch.react_message_type { type_imports.push(t.clone()); } }
|
||||
if ch.has_django_message { if let Some(t) = &ch.django_message_type { type_imports.push(t.clone()); } }
|
||||
}
|
||||
|
||||
let hooks_content = ChannelsHooks {
|
||||
channels: ir.channels.iter().map(ChannelView::from_ir).collect(),
|
||||
type_imports,
|
||||
}.render().expect("channels.hooks.tsx renders");
|
||||
|
||||
vec![
|
||||
EmittedFile::new(PathBuf::from("channels.ts"), types_content),
|
||||
EmittedFile::new(PathBuf::from("channels.hooks.tsx"), hooks_content),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "channels/channels.ts.j2", escape = "none")]
|
||||
struct ChannelsTypes<'a> {
|
||||
channels: Vec<ChannelView<'a>>,
|
||||
schemas_block: String,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "channels/channels.hooks.tsx.j2", escape = "none")]
|
||||
struct ChannelsHooks<'a> {
|
||||
channels: Vec<ChannelView<'a>>,
|
||||
type_imports: Vec<String>,
|
||||
}
|
||||
|
||||
|
||||
struct ChannelView<'a> {
|
||||
name: &'a str,
|
||||
pascal_name: &'a str,
|
||||
has_params: bool,
|
||||
has_react_message: bool,
|
||||
has_django_message: bool,
|
||||
params_type: String,
|
||||
react_message_type: String,
|
||||
django_message_type: String,
|
||||
params_type_or_record: String,
|
||||
react_msg_type_or_never: String,
|
||||
django_msg_type_or_never: String,
|
||||
}
|
||||
|
||||
|
||||
impl<'a> ChannelView<'a> {
|
||||
fn from_ir(ch: &'a MizanChannel) -> Self {
|
||||
let params_type = ch.params_type.clone().unwrap_or_default();
|
||||
let react_message_type = ch.react_message_type.clone().unwrap_or_default();
|
||||
let django_message_type = ch.django_message_type.clone().unwrap_or_default();
|
||||
|
||||
Self {
|
||||
name: &ch.name,
|
||||
pascal_name: &ch.pascal_name,
|
||||
has_params: ch.has_params,
|
||||
has_react_message: ch.has_react_message,
|
||||
has_django_message: ch.has_django_message,
|
||||
params_type_or_record: if ch.has_params { params_type.clone() } else { "Record<string, never>".to_string() },
|
||||
react_msg_type_or_never: if ch.has_react_message { react_message_type.clone() } else { "never".to_string() },
|
||||
django_msg_type_or_never: if ch.has_django_message { django_message_type.clone() } else { "never".to_string() },
|
||||
params_type,
|
||||
react_message_type,
|
||||
django_message_type,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn emit_channel_schemas(
|
||||
channels: &[MizanChannel],
|
||||
schemas: &IndexMap<String, JsonSchema>,
|
||||
) -> String {
|
||||
let mut blocks: Vec<String> = Vec::new();
|
||||
for ch in channels {
|
||||
for ty in [&ch.params_type, &ch.react_message_type, &ch.django_message_type].iter().filter_map(|t| t.as_ref()) {
|
||||
if let Some(schema) = schemas.get(ty) {
|
||||
blocks.push(emit_schema_as_ts(ty, schema));
|
||||
}
|
||||
}
|
||||
}
|
||||
blocks.join("\n\n")
|
||||
}
|
||||
|
||||
|
||||
fn emit_schema_as_ts(name: &str, schema: &JsonSchema) -> String {
|
||||
if let Some(props) = &schema.properties {
|
||||
let required: std::collections::HashSet<&str> =
|
||||
schema.required.iter().map(String::as_str).collect();
|
||||
let fields = props.iter()
|
||||
.map(|(field_name, field_schema)| {
|
||||
let opt = if required.contains(field_name.as_str()) { "" } else { "?" };
|
||||
let ty = ts_type_expression(field_schema);
|
||||
format!(" {field_name}{opt}: {ty}")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
if fields.is_empty() {
|
||||
format!("export interface {name} {{}}")
|
||||
} else {
|
||||
format!("export interface {name} {{\n{fields}\n}}")
|
||||
}
|
||||
} else {
|
||||
format!("export type {name} = {}", ts_type_expression(schema))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn ts_type_expression(schema: &JsonSchema) -> String {
|
||||
if let Some(ref_name) = schema.ref_name() {
|
||||
return ref_name.to_string();
|
||||
}
|
||||
match schema.ty.as_deref() {
|
||||
Some("integer") | Some("number") => "number".to_string(),
|
||||
Some("boolean") => "boolean".to_string(),
|
||||
Some("string") => "string".to_string(),
|
||||
Some("array") => {
|
||||
let elem = ts_type_expression(schema.items.as_deref().unwrap_or(&JsonSchema::default()));
|
||||
format!("{elem}[]")
|
||||
}
|
||||
Some("object") => "Record<string, unknown>".to_string(),
|
||||
_ => "unknown".to_string(),
|
||||
}
|
||||
}
|
||||
67
protocol/mizan-codegen/src/emit/mod.rs
Normal file
67
protocol/mizan-codegen/src/emit/mod.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
//! Emit substrate — per-target codegen lives here.
|
||||
//!
|
||||
//! Every target implements `CodegenTarget` and returns the same shape:
|
||||
//! a `Vec<EmittedFile>`. The dispatcher in `main.rs` iterates one target
|
||||
//! per `--target` flag and writes each `EmittedFile` to disk under the
|
||||
//! configured output directory.
|
||||
//!
|
||||
//! Targets land in subsequent phases; Phase 2 establishes the trait so
|
||||
//! the dispatch surface is settled before any target's emit logic is
|
||||
//! written.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::ir::MizanIR;
|
||||
|
||||
pub mod casing;
|
||||
pub mod channels;
|
||||
pub mod python;
|
||||
pub mod react;
|
||||
pub mod rust;
|
||||
pub mod stage1;
|
||||
pub mod svelte;
|
||||
pub mod vue;
|
||||
|
||||
|
||||
pub trait CodegenTarget {
|
||||
/// Stable identifier — matches the `--target` flag value and the
|
||||
/// `targets = [...]` entry in `mizan.toml`.
|
||||
fn name(&self) -> &'static str;
|
||||
|
||||
/// Walk the IR and produce the per-target file set. Each path is
|
||||
/// relative to the consumer's configured `output` directory.
|
||||
fn emit(&self, ir: &MizanIR, config: &Config) -> Vec<EmittedFile>;
|
||||
}
|
||||
|
||||
|
||||
pub struct EmittedFile {
|
||||
pub rel_path: PathBuf,
|
||||
pub content: String,
|
||||
}
|
||||
|
||||
|
||||
impl EmittedFile {
|
||||
pub fn new(rel_path: impl Into<PathBuf>, content: impl Into<String>) -> Self {
|
||||
Self {
|
||||
rel_path: rel_path.into(),
|
||||
content: content.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Look up a registered target by name. Returns `None` for unknown
|
||||
/// targets so the CLI can warn instead of panicking.
|
||||
pub fn target_by_name(name: &str) -> Option<Box<dyn CodegenTarget>> {
|
||||
match name {
|
||||
"stage1" => Some(Box::new(stage1::Stage1)),
|
||||
"rust" => Some(Box::new(rust::RustCrate)),
|
||||
"python" => Some(Box::new(python::PythonClient)),
|
||||
"react" => Some(Box::new(react::ReactAdapter)),
|
||||
"vue" => Some(Box::new(vue::VueAdapter)),
|
||||
"svelte" => Some(Box::new(svelte::SvelteAdapter)),
|
||||
"channels" => Some(Box::new(channels::ChannelsTarget)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
330
protocol/mizan-codegen/src/emit/python.rs
Normal file
330
protocol/mizan-codegen/src/emit/python.rs
Normal file
@@ -0,0 +1,330 @@
|
||||
//! Python target — emits a Pydantic-typed client wrapping the PyO3
|
||||
//! extension exposed by `mizan-rust`.
|
||||
//!
|
||||
//! Output shape lives at `templates/python/*.j2`. Per-method bodies are
|
||||
//! pre-rendered in Rust before passing into `client.py.j2` so the template
|
||||
//! only owns top-level section layout, not Python method-signature details.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use askama::Template;
|
||||
use indexmap::IndexMap;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::emit::CodegenTarget;
|
||||
use crate::emit::EmittedFile;
|
||||
use crate::emit::casing::{pascal_case, rust_ident, snake_case};
|
||||
use crate::ir::{IsContext, JsonSchema, MizanContext, MizanFunction, MizanIR};
|
||||
|
||||
|
||||
pub struct PythonClient;
|
||||
|
||||
|
||||
impl CodegenTarget for PythonClient {
|
||||
fn name(&self) -> &'static str { "python" }
|
||||
|
||||
fn emit(&self, ir: &MizanIR, _config: &Config) -> Vec<EmittedFile> {
|
||||
let schemas_block = ir.components.schemas.iter()
|
||||
.map(|(name, schema)| emit_schema_block(name, schema))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n\n");
|
||||
|
||||
let types_py = TypesTemplate { schemas_block }.render().expect("types.py renders");
|
||||
let client_py = build_client_template(ir).render().expect("client.py renders");
|
||||
let init_py = InitTemplate {}.render().expect("__init__.py renders");
|
||||
|
||||
vec![
|
||||
EmittedFile::new(PathBuf::from("types.py"), types_py),
|
||||
EmittedFile::new(PathBuf::from("client.py"), client_py),
|
||||
EmittedFile::new(PathBuf::from("__init__.py"), init_py),
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "python/__init__.py.j2", escape = "none")]
|
||||
struct InitTemplate {}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "python/types.py.j2", escape = "none")]
|
||||
struct TypesTemplate {
|
||||
schemas_block: String,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "python/client.py.j2", escape = "none")]
|
||||
struct ClientTemplate {
|
||||
ctx_methods_block: String,
|
||||
call_methods_block: String,
|
||||
data_classes_block: String,
|
||||
}
|
||||
|
||||
|
||||
// ─── types.py schema bodies ────────────────────────────────────────────────
|
||||
|
||||
|
||||
fn emit_schema_block(raw_name: &str, schema: &JsonSchema) -> String {
|
||||
let name = pascal_case(raw_name);
|
||||
|
||||
if let Some(values) = &schema.r#enum {
|
||||
if schema.ty.as_deref() == Some("string") {
|
||||
let literal = values.iter()
|
||||
.filter_map(|v| v.as_str())
|
||||
.map(|v| format!("\"{v}\""))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
return format!("{name} = Literal[{literal}]");
|
||||
}
|
||||
}
|
||||
|
||||
if schema.ty.as_deref() == Some("array") {
|
||||
let elem = py_type_from_schema(schema.items.as_deref().unwrap_or(&JsonSchema::default()));
|
||||
return format!("{name} = list[{elem}]");
|
||||
}
|
||||
|
||||
if schema.ty.as_deref() == Some("object") {
|
||||
if let Some(props) = &schema.properties {
|
||||
return emit_pydantic_class(&name, schema, props);
|
||||
}
|
||||
}
|
||||
|
||||
let ty = py_type_from_schema(schema);
|
||||
format!("{name} = {ty}")
|
||||
}
|
||||
|
||||
|
||||
fn emit_pydantic_class(
|
||||
name: &str,
|
||||
schema: &JsonSchema,
|
||||
properties: &IndexMap<String, JsonSchema>,
|
||||
) -> String {
|
||||
if properties.is_empty() {
|
||||
return format!("class {name}(BaseModel):\n pass");
|
||||
}
|
||||
let required: std::collections::HashSet<&str> =
|
||||
schema.required.iter().map(String::as_str).collect();
|
||||
|
||||
let field_lines = properties.iter()
|
||||
.map(|(field_raw, field_schema)| {
|
||||
let mut ty = py_type_from_schema(field_schema);
|
||||
let is_required = required.contains(field_raw.as_str())
|
||||
|| field_schema.default.is_some();
|
||||
if !is_required {
|
||||
if !ty.ends_with(" | None") {
|
||||
ty = format!("{ty} | None");
|
||||
}
|
||||
format!(" {}: {ty} = None", rust_ident(field_raw))
|
||||
} else {
|
||||
format!(" {}: {ty}", rust_ident(field_raw))
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
format!("class {name}(BaseModel):\n{field_lines}")
|
||||
}
|
||||
|
||||
|
||||
fn py_type_from_schema(schema: &JsonSchema) -> String {
|
||||
if let Some(ref_name) = schema.ref_name() {
|
||||
return pascal_case(ref_name);
|
||||
}
|
||||
|
||||
if let Some(any_of) = &schema.any_of {
|
||||
let has_null = any_of.iter().any(|s| s.ty.as_deref() == Some("null"));
|
||||
let non_null: Vec<&JsonSchema> = any_of
|
||||
.iter()
|
||||
.filter(|s| s.ty.as_deref() != Some("null"))
|
||||
.collect();
|
||||
if has_null && non_null.len() == 1 {
|
||||
return format!("{} | None", py_type_from_schema(non_null[0]));
|
||||
}
|
||||
}
|
||||
|
||||
let nullable = schema.nullable;
|
||||
let inner = inner_py_type(schema);
|
||||
if nullable {
|
||||
format!("{inner} | None")
|
||||
} else {
|
||||
inner
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn inner_py_type(schema: &JsonSchema) -> String {
|
||||
if let Some(values) = &schema.r#enum {
|
||||
if schema.ty.as_deref() == Some("string") {
|
||||
let parts = values.iter()
|
||||
.filter_map(|v| v.as_str())
|
||||
.map(|v| format!("\"{v}\""))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
return format!("Literal[{parts}]");
|
||||
}
|
||||
}
|
||||
match schema.ty.as_deref() {
|
||||
Some("integer") => "int".to_string(),
|
||||
Some("number") => "float".to_string(),
|
||||
Some("boolean") => "bool".to_string(),
|
||||
Some("string") => "str".to_string(),
|
||||
Some("array") => {
|
||||
let elem = py_type_from_schema(schema.items.as_deref().unwrap_or(&JsonSchema::default()));
|
||||
format!("list[{elem}]")
|
||||
}
|
||||
Some("object") => {
|
||||
if schema.properties.is_some() { "Any".to_string() }
|
||||
else { "dict[str, Any]".to_string() }
|
||||
}
|
||||
_ => "Any".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ─── client.py method blocks ───────────────────────────────────────────────
|
||||
|
||||
|
||||
fn build_client_template(ir: &MizanIR) -> ClientTemplate {
|
||||
let ctx_methods_block = ir.contexts.iter()
|
||||
.map(|(ctx_name, ctx_meta)| {
|
||||
let fetch = emit_fetch_method(ctx_name, ctx_meta);
|
||||
let subscribe = emit_subscribe_method(ctx_name, ctx_meta);
|
||||
format!("{fetch}{subscribe}")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
let call_methods_block = ir.functions.iter()
|
||||
.filter(|f| matches!(f.is_context, IsContext::No) && !f.is_form)
|
||||
.map(emit_call_method)
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
let data_classes_block = ir.contexts.iter()
|
||||
.map(|(ctx_name, _)| {
|
||||
let ctx_fns: Vec<&MizanFunction> = ir.functions.iter()
|
||||
.filter(|f| f.is_context.as_str() == Some(ctx_name))
|
||||
.collect();
|
||||
emit_context_data_class(ctx_name, &ctx_fns)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
ClientTemplate { ctx_methods_block, call_methods_block, data_classes_block }
|
||||
}
|
||||
|
||||
|
||||
fn py_arg_type(json_ty: &str) -> &'static str {
|
||||
match json_ty {
|
||||
"integer" => "int",
|
||||
"number" => "float",
|
||||
"boolean" => "bool",
|
||||
_ => "str",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn emit_fetch_method(ctx_name: &str, ctx_meta: &MizanContext) -> String {
|
||||
let method_name = format!("fetch_{}_context", snake_case(ctx_name));
|
||||
let param_args = ctx_meta.params.iter()
|
||||
.map(|(n, m)| {
|
||||
let ident = rust_ident(n);
|
||||
let ty = py_arg_type(&m.ty);
|
||||
if m.required { format!("{ident}: {ty}") }
|
||||
else { format!("{ident}: {ty} | None = None") }
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
let param_dict = if ctx_meta.params.is_empty() {
|
||||
"{}".to_string()
|
||||
} else {
|
||||
let pairs = ctx_meta.params.iter()
|
||||
.map(|(n, _)| format!("\"{n}\": {}", rust_ident(n)))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
format!("{{{pairs}}}")
|
||||
};
|
||||
let data_class = format!("{}ContextData", pascal_case(ctx_name));
|
||||
let arg_sig = if param_args.is_empty() { String::new() } else { format!(", {param_args}") };
|
||||
|
||||
format!(
|
||||
" def {method_name}(self{arg_sig}) -> \"{data_class}\":\n raw = self._inner.fetch_context(\"{ctx_name}\", {param_dict})\n return {data_class}(**raw)\n",
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fn emit_subscribe_method(ctx_name: &str, ctx_meta: &MizanContext) -> String {
|
||||
let param_args = ctx_meta.params.iter()
|
||||
.map(|(n, m)| {
|
||||
let ident = rust_ident(n);
|
||||
let ty = py_arg_type(&m.ty);
|
||||
if m.required { format!("{ident}: {ty}") }
|
||||
else { format!("{ident}: {ty} | None = None") }
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
let param_dict = if ctx_meta.params.is_empty() {
|
||||
"{}".to_string()
|
||||
} else {
|
||||
let pairs = ctx_meta.params.iter()
|
||||
.map(|(n, _)| format!("\"{n}\": {}", rust_ident(n)))
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
format!("{{{pairs}}}")
|
||||
};
|
||||
let arg_sig = if param_args.is_empty() { String::new() } else { format!(", {param_args}") };
|
||||
let snake = snake_case(ctx_name);
|
||||
let indent_39 = " ".repeat(39);
|
||||
|
||||
format!(
|
||||
" def subscribe_{snake}_context(self{arg_sig},\n{indent_39}callback: Callable[[dict[str, Any]], None]) -> PyContextSubscription:\n return self._inner.subscribe_context(\"{ctx_name}\", {param_dict}, callback)\n",
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fn emit_call_method(fn_meta: &MizanFunction) -> String {
|
||||
let method_name = format!("call_{}", snake_case(&fn_meta.name));
|
||||
let pascal_output = pascal_case(&fn_meta.output_type);
|
||||
|
||||
let input_arg = if fn_meta.has_input {
|
||||
let it = fn_meta.input_type.as_deref().unwrap_or("");
|
||||
format!(", args: {}", pascal_case(it))
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
let args_expr = if fn_meta.has_input { "args.model_dump()" } else { "{}" };
|
||||
let return_type = if fn_meta.output_nullable {
|
||||
format!("{pascal_output} | None")
|
||||
} else {
|
||||
pascal_output.clone()
|
||||
};
|
||||
let decode_expr = if fn_meta.output_nullable {
|
||||
format!("{pascal_output}(**raw) if raw is not None else None")
|
||||
} else {
|
||||
format!("{pascal_output}(**raw)")
|
||||
};
|
||||
|
||||
format!(
|
||||
" def {method_name}(self{input_arg}) -> {return_type}:\n raw = self._inner.call(\"{wire}\", {args_expr})\n return {decode_expr}\n",
|
||||
wire = fn_meta.name,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fn emit_context_data_class(ctx_name: &str, ctx_fns: &[&MizanFunction]) -> String {
|
||||
let class_name = format!("{}ContextData", pascal_case(ctx_name));
|
||||
let field_lines = ctx_fns.iter()
|
||||
.map(|fn_meta| {
|
||||
let pascal_out = pascal_case(&fn_meta.output_type);
|
||||
let ty = if fn_meta.output_nullable { format!("{pascal_out} | None") } else { pascal_out };
|
||||
format!(" {}: {ty}", rust_ident(&fn_meta.name))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
format!(
|
||||
"class {class_name}(BaseModel):\n \"\"\"Bundled return of fetch_{snake}_context.\"\"\"\n{field_lines}\n",
|
||||
snake = snake_case(ctx_name),
|
||||
)
|
||||
}
|
||||
142
protocol/mizan-codegen/src/emit/react.rs
Normal file
142
protocol/mizan-codegen/src/emit/react.rs
Normal file
@@ -0,0 +1,142 @@
|
||||
//! React target — Stage 2 emit on top of Stage 1. Wraps each registered
|
||||
//! context in a React Provider so kernel subscription happens once per
|
||||
//! provider mount; consumer hooks read from React Context.
|
||||
//!
|
||||
//! Output shape lives at `templates/react/react.tsx.j2`.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use askama::Template;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::emit::CodegenTarget;
|
||||
use crate::emit::EmittedFile;
|
||||
use crate::emit::casing::pascal_case;
|
||||
use crate::ir::{IsContext, MizanFunction, MizanIR};
|
||||
|
||||
|
||||
pub struct ReactAdapter;
|
||||
|
||||
|
||||
impl CodegenTarget for ReactAdapter {
|
||||
fn name(&self) -> &'static str { "react" }
|
||||
|
||||
fn emit(&self, ir: &MizanIR, _config: &Config) -> Vec<EmittedFile> {
|
||||
let content = build_template(ir).render().expect("react template renders");
|
||||
vec![EmittedFile::new(PathBuf::from("react.tsx"), content)]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "react/react.tsx.j2", escape = "none")]
|
||||
struct ReactTemplate<'a> {
|
||||
has_global: bool,
|
||||
stage1_imports: Vec<String>,
|
||||
global_fns: Vec<HookRender<'a>>,
|
||||
named_contexts: Vec<CtxRender<'a>>,
|
||||
calls: Vec<CallRender>,
|
||||
}
|
||||
|
||||
|
||||
struct HookRender<'a> {
|
||||
pascal: String,
|
||||
output_type: &'a str,
|
||||
name: &'a str,
|
||||
}
|
||||
|
||||
|
||||
struct CtxRender<'a> {
|
||||
pascal: String,
|
||||
name: &'a str,
|
||||
has_params: bool,
|
||||
fns: Vec<HookRender<'a>>,
|
||||
}
|
||||
|
||||
|
||||
struct CallRender {
|
||||
pascal: String,
|
||||
has_input: bool,
|
||||
}
|
||||
|
||||
|
||||
fn dedupe_preserving_order(items: impl IntoIterator<Item = String>) -> Vec<String> {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
items.into_iter().filter(|s| seen.insert(s.clone())).collect()
|
||||
}
|
||||
|
||||
|
||||
fn build_template(ir: &MizanIR) -> ReactTemplate<'_> {
|
||||
let has_global = ir.contexts.contains_key("global");
|
||||
|
||||
let global_fns: Vec<HookRender> = ir.functions.iter()
|
||||
.filter(|f| f.is_context.as_str() == Some("global"))
|
||||
.map(|f| HookRender {
|
||||
pascal: pascal_case(&f.camel_name),
|
||||
output_type: &f.output_type,
|
||||
name: &f.name,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let named_contexts: Vec<CtxRender> = ir.contexts.iter()
|
||||
.filter(|(n, _)| n.as_str() != "global")
|
||||
.map(|(ctx_name, ctx_meta)| {
|
||||
let ctx_fns: Vec<HookRender> = ir.functions.iter()
|
||||
.filter(|f| f.is_context.as_str() == Some(ctx_name.as_str()))
|
||||
.map(|f| HookRender {
|
||||
pascal: pascal_case(&f.camel_name),
|
||||
output_type: &f.output_type,
|
||||
name: &f.name,
|
||||
})
|
||||
.collect();
|
||||
CtxRender {
|
||||
pascal: pascal_case(ctx_name),
|
||||
name: ctx_name,
|
||||
has_params: !ctx_meta.params.is_empty(),
|
||||
fns: ctx_fns,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mutations: Vec<&MizanFunction> = ir.functions.iter()
|
||||
.filter(|f| matches!(f.is_context, IsContext::No) && !f.is_form && !f.affects.is_empty())
|
||||
.collect();
|
||||
let plain_fns: Vec<&MizanFunction> = ir.functions.iter()
|
||||
.filter(|f| matches!(f.is_context, IsContext::No) && !f.is_form && f.affects.is_empty())
|
||||
.collect();
|
||||
|
||||
let calls: Vec<CallRender> = mutations.iter().chain(plain_fns.iter())
|
||||
.map(|f| CallRender {
|
||||
pascal: pascal_case(&f.camel_name),
|
||||
has_input: f.has_input,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut stage1: Vec<String> = Vec::new();
|
||||
for ctx_name in ir.contexts.keys() {
|
||||
let p = pascal_case(ctx_name);
|
||||
stage1.push(format!("fetch{p}Context"));
|
||||
stage1.push(format!("type {p}ContextData"));
|
||||
stage1.push(format!("type {p}ContextParams"));
|
||||
}
|
||||
for fn_meta in mutations.iter().chain(plain_fns.iter()) {
|
||||
stage1.push(format!("call{}", pascal_case(&fn_meta.camel_name)));
|
||||
}
|
||||
let context_fns: Vec<&MizanFunction> = ir.functions.iter()
|
||||
.filter(|f| !matches!(f.is_context, IsContext::No))
|
||||
.collect();
|
||||
let output_types = dedupe_preserving_order(
|
||||
context_fns.iter().map(|f| f.output_type.clone()),
|
||||
);
|
||||
for t in output_types {
|
||||
stage1.push(format!("type {t}"));
|
||||
}
|
||||
|
||||
ReactTemplate {
|
||||
has_global,
|
||||
stage1_imports: stage1,
|
||||
global_fns,
|
||||
named_contexts,
|
||||
calls,
|
||||
}
|
||||
}
|
||||
474
protocol/mizan-codegen/src/emit/rust.rs
Normal file
474
protocol/mizan-codegen/src/emit/rust.rs
Normal file
@@ -0,0 +1,474 @@
|
||||
//! Rust target — emits a complete Cargo crate consuming the
|
||||
//! `mizan-rust` kernel. Output shape lives at `templates/rust/*.j2`.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use askama::Template;
|
||||
use indexmap::IndexMap;
|
||||
|
||||
use crate::config::{Config, RustKernelSpec};
|
||||
use crate::emit::CodegenTarget;
|
||||
use crate::emit::EmittedFile;
|
||||
use crate::emit::casing::{pascal_case, rust_ident, rust_type_ident, snake_case};
|
||||
use crate::ir::{IsContext, JsonSchema, MizanContext, MizanFunction, MizanIR};
|
||||
|
||||
|
||||
pub struct RustCrate;
|
||||
|
||||
|
||||
impl CodegenTarget for RustCrate {
|
||||
fn name(&self) -> &'static str { "rust" }
|
||||
|
||||
fn emit(&self, ir: &MizanIR, config: &Config) -> Vec<EmittedFile> {
|
||||
let crate_name = config
|
||||
.rust_crate_name
|
||||
.clone()
|
||||
.unwrap_or_else(|| "mizan_client".to_string());
|
||||
|
||||
let kernel_dep = format_kernel_dep(config.rust_kernel.as_ref());
|
||||
|
||||
let mut out: Vec<EmittedFile> = Vec::new();
|
||||
|
||||
out.push(EmittedFile::new(
|
||||
"Cargo.toml",
|
||||
CargoTemplate { crate_name: &crate_name, kernel_dep: &kernel_dep }
|
||||
.render().expect("Cargo.toml renders"),
|
||||
));
|
||||
|
||||
out.push(EmittedFile::new("src/types.rs", emit_types_rs(&ir.components.schemas)));
|
||||
|
||||
let mut context_modules: Vec<String> = Vec::new();
|
||||
for (ctx_name, ctx_meta) in &ir.contexts {
|
||||
let module_name = snake_case(ctx_name);
|
||||
out.push(EmittedFile::new(
|
||||
PathBuf::from("src/contexts").join(format!("{module_name}.rs")),
|
||||
emit_context_file(ctx_name, ctx_meta, &ir.functions),
|
||||
));
|
||||
context_modules.push(module_name);
|
||||
}
|
||||
if !context_modules.is_empty() {
|
||||
out.push(EmittedFile::new("src/contexts/mod.rs", emit_mod_file(&context_modules)));
|
||||
}
|
||||
|
||||
let mut mutation_modules: Vec<String> = Vec::new();
|
||||
let mut function_modules: Vec<String> = Vec::new();
|
||||
for fn_meta in &ir.functions {
|
||||
if !matches!(fn_meta.is_context, IsContext::No) || fn_meta.is_form { continue; }
|
||||
let is_mutation = !fn_meta.affects.is_empty();
|
||||
let kind = if is_mutation { "mutations" } else { "functions" };
|
||||
let module_name = snake_case(&fn_meta.camel_name);
|
||||
out.push(EmittedFile::new(
|
||||
PathBuf::from(format!("src/{kind}")).join(format!("{module_name}.rs")),
|
||||
emit_call_file(fn_meta),
|
||||
));
|
||||
if is_mutation {
|
||||
mutation_modules.push(module_name);
|
||||
} else {
|
||||
function_modules.push(module_name);
|
||||
}
|
||||
}
|
||||
if !mutation_modules.is_empty() {
|
||||
out.push(EmittedFile::new("src/mutations/mod.rs", emit_mod_file(&mutation_modules)));
|
||||
}
|
||||
if !function_modules.is_empty() {
|
||||
out.push(EmittedFile::new("src/functions/mod.rs", emit_mod_file(&function_modules)));
|
||||
}
|
||||
|
||||
out.push(EmittedFile::new(
|
||||
"src/lib.rs",
|
||||
LibTemplate {
|
||||
has_contexts: !context_modules.is_empty(),
|
||||
has_mutations: !mutation_modules.is_empty(),
|
||||
has_functions: !function_modules.is_empty(),
|
||||
}.render().expect("lib.rs renders"),
|
||||
));
|
||||
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "rust/Cargo.toml.j2", escape = "none")]
|
||||
struct CargoTemplate<'a> {
|
||||
crate_name: &'a str,
|
||||
kernel_dep: &'a str,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "rust/lib.rs.j2", escape = "none")]
|
||||
struct LibTemplate {
|
||||
has_contexts: bool,
|
||||
has_mutations: bool,
|
||||
has_functions: bool,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "rust/mod.rs.j2", escape = "none")]
|
||||
struct ModTemplate {
|
||||
modules: Vec<String>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "rust/context.rs.j2", escape = "none")]
|
||||
struct ContextTemplate<'a> {
|
||||
pascal: String,
|
||||
snake: String,
|
||||
ctx_name: &'a str,
|
||||
type_imports: Vec<String>,
|
||||
data_fields: Vec<StructField>,
|
||||
params: Vec<StructField>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "rust/call.rs.j2", escape = "none")]
|
||||
struct CallTemplate<'a> {
|
||||
snake: String,
|
||||
name: &'a str,
|
||||
return_type: String,
|
||||
type_imports: Vec<String>,
|
||||
input_param: String,
|
||||
args_value: &'static str,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "rust/types.rs.j2", escape = "none")]
|
||||
struct TypesTemplate {
|
||||
schemas_block: String,
|
||||
hoisted_enums_block: String,
|
||||
}
|
||||
|
||||
|
||||
struct StructField {
|
||||
raw_name: String,
|
||||
ident: String,
|
||||
ty: String,
|
||||
has_rename: bool,
|
||||
}
|
||||
|
||||
|
||||
fn dedupe_preserving_order(items: impl IntoIterator<Item = String>) -> Vec<String> {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
items.into_iter().filter(|s| seen.insert(s.clone())).collect()
|
||||
}
|
||||
|
||||
|
||||
// ─── Cargo.toml ────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
fn format_kernel_dep(spec: Option<&RustKernelSpec>) -> String {
|
||||
match spec {
|
||||
Some(RustKernelSpec::Path { path }) => format!("{{ path = {} }}", json_str(path)),
|
||||
Some(RustKernelSpec::Git { git, tag, rev, branch }) => {
|
||||
let mut parts = vec![format!("git = {}", json_str(git))];
|
||||
if let Some(t) = tag { parts.push(format!("tag = {}", json_str(t))); }
|
||||
if let Some(r) = rev { parts.push(format!("rev = {}", json_str(r))); }
|
||||
if let Some(b) = branch { parts.push(format!("branch = {}", json_str(b))); }
|
||||
format!("{{ {} }}", parts.join(", "))
|
||||
}
|
||||
Some(RustKernelSpec::Version { version }) => format!("{{ version = {} }}", json_str(version)),
|
||||
None => "{ version = \"0.1\" }".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn json_str(s: &str) -> String {
|
||||
serde_json::to_string(s).expect("string literal serializes")
|
||||
}
|
||||
|
||||
|
||||
// ─── mod.rs ────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
fn emit_mod_file(module_names: &[String]) -> String {
|
||||
let mut sorted = module_names.to_vec();
|
||||
sorted.sort();
|
||||
ModTemplate { modules: sorted }.render().expect("mod.rs renders")
|
||||
}
|
||||
|
||||
|
||||
// ─── Context file ──────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
fn emit_context_file(
|
||||
ctx_name: &str,
|
||||
ctx_meta: &MizanContext,
|
||||
all_functions: &[MizanFunction],
|
||||
) -> String {
|
||||
let pascal = pascal_case(ctx_name);
|
||||
let snake = snake_case(ctx_name);
|
||||
|
||||
let ctx_fns: Vec<&MizanFunction> = all_functions
|
||||
.iter()
|
||||
.filter(|f| f.is_context.as_str() == Some(ctx_name))
|
||||
.collect();
|
||||
|
||||
let type_imports = dedupe_preserving_order(
|
||||
ctx_fns.iter().map(|f| rust_type_ident(&f.output_type)),
|
||||
);
|
||||
|
||||
let data_fields: Vec<StructField> = ctx_fns.iter()
|
||||
.map(|f| {
|
||||
let ident = rust_ident(&f.name);
|
||||
StructField {
|
||||
has_rename: ident != f.name,
|
||||
raw_name: f.name.clone(),
|
||||
ident,
|
||||
ty: rust_type_ident(&f.output_type),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let params: Vec<StructField> = ctx_meta.params.iter()
|
||||
.map(|(p_name, p_meta)| {
|
||||
let ident = rust_ident(p_name);
|
||||
let base = param_rust_type(&p_meta.ty);
|
||||
let ty = if p_meta.required { base.to_string() } else { format!("Option<{base}>") };
|
||||
StructField {
|
||||
has_rename: ident != *p_name,
|
||||
raw_name: p_name.clone(),
|
||||
ident,
|
||||
ty,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
ContextTemplate {
|
||||
pascal,
|
||||
snake,
|
||||
ctx_name,
|
||||
type_imports,
|
||||
data_fields,
|
||||
params,
|
||||
}.render().expect("context.rs renders")
|
||||
}
|
||||
|
||||
|
||||
fn param_rust_type(json_ty: &str) -> &'static str {
|
||||
match json_ty {
|
||||
"integer" => "i64",
|
||||
"number" => "f64",
|
||||
"boolean" => "bool",
|
||||
_ => "String",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ─── Call file ─────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
fn emit_call_file(fn_meta: &MizanFunction) -> String {
|
||||
let output_type = rust_type_ident(&fn_meta.output_type);
|
||||
let return_type = if fn_meta.output_nullable {
|
||||
format!("Option<{output_type}>")
|
||||
} else {
|
||||
output_type.clone()
|
||||
};
|
||||
|
||||
let input_type = fn_meta.input_type.as_deref().map(rust_type_ident);
|
||||
|
||||
let mut used_seed: Vec<String> = vec![output_type.clone()];
|
||||
if let Some(t) = &input_type { used_seed.push(t.clone()); }
|
||||
let type_imports = dedupe_preserving_order(used_seed);
|
||||
|
||||
let (input_param, args_value) = if fn_meta.has_input {
|
||||
let it = input_type.as_deref().unwrap_or("");
|
||||
(
|
||||
format!(", args: &{it}"),
|
||||
"serde_json::to_value(args).unwrap_or(Value::Object(Default::default()))",
|
||||
)
|
||||
} else {
|
||||
(
|
||||
String::new(),
|
||||
"Value::Object(Default::default())",
|
||||
)
|
||||
};
|
||||
|
||||
CallTemplate {
|
||||
snake: snake_case(&fn_meta.name),
|
||||
name: &fn_meta.name,
|
||||
return_type,
|
||||
type_imports,
|
||||
input_param,
|
||||
args_value,
|
||||
}.render().expect("call.rs renders")
|
||||
}
|
||||
|
||||
|
||||
// ─── types.rs ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
struct EnumCtx {
|
||||
hoisted: Vec<(String, Vec<serde_json::Value>)>,
|
||||
depth: usize,
|
||||
enum_name: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
fn emit_types_rs(schemas: &IndexMap<String, JsonSchema>) -> String {
|
||||
let mut ctx = EnumCtx { hoisted: Vec::new(), depth: 0, enum_name: None };
|
||||
|
||||
let schemas_block = schemas.iter()
|
||||
.map(|(raw_name, schema)| {
|
||||
let name = rust_type_ident(raw_name);
|
||||
if let Some(values) = &schema.r#enum {
|
||||
if schema.ty.as_deref() == Some("string") {
|
||||
return emit_string_enum(&name, values);
|
||||
}
|
||||
}
|
||||
if schema.ty.as_deref() == Some("array") {
|
||||
return emit_transparent_array(&name, schema, &mut ctx);
|
||||
}
|
||||
if schema.ty.as_deref() == Some("object") {
|
||||
if let Some(props) = &schema.properties {
|
||||
return emit_struct(&name, schema, props, &mut ctx);
|
||||
}
|
||||
}
|
||||
emit_type_alias(&name, schema, &mut ctx)
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
let hoisted_enums_block = ctx.hoisted.iter()
|
||||
.map(|(n, v)| emit_string_enum(n, v))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
TypesTemplate { schemas_block, hoisted_enums_block }
|
||||
.render().expect("types.rs renders")
|
||||
}
|
||||
|
||||
|
||||
fn emit_string_enum(name: &str, variants: &[serde_json::Value]) -> String {
|
||||
let body = variants.iter()
|
||||
.filter_map(|v| v.as_str())
|
||||
.map(|v| {
|
||||
let ident = pascal_case(v);
|
||||
let rename = if ident == v {
|
||||
String::new()
|
||||
} else {
|
||||
format!(" #[serde(rename = {})]\n", json_str(v))
|
||||
};
|
||||
format!("{rename} {ident},")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
format!(
|
||||
"#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]\npub enum {name} {{\n{body}\n}}\n",
|
||||
name = rust_type_ident(name),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fn emit_transparent_array(name: &str, schema: &JsonSchema, ctx: &mut EnumCtx) -> String {
|
||||
ctx.depth = 1;
|
||||
ctx.enum_name = None;
|
||||
let inner = rust_type_from_schema(schema.items.as_deref().unwrap_or(&JsonSchema::default()), ctx);
|
||||
format!(
|
||||
"#[derive(Debug, Clone, Serialize, Deserialize)]\n#[serde(transparent)]\npub struct {name}(pub Vec<{inner}>);\n",
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fn emit_struct(
|
||||
name: &str,
|
||||
schema: &JsonSchema,
|
||||
properties: &IndexMap<String, JsonSchema>,
|
||||
ctx: &mut EnumCtx,
|
||||
) -> String {
|
||||
let required: std::collections::HashSet<&str> =
|
||||
schema.required.iter().map(String::as_str).collect();
|
||||
|
||||
let fields = properties.iter()
|
||||
.map(|(field_raw, field_schema)| {
|
||||
let field_name = rust_ident(field_raw);
|
||||
ctx.depth = 1;
|
||||
ctx.enum_name = Some(format!("{name}_{}", pascal_case(field_raw)));
|
||||
let mut ty = rust_type_from_schema(field_schema, ctx);
|
||||
let is_required = required.contains(field_raw.as_str())
|
||||
|| field_schema.default.is_some();
|
||||
if !is_required && !ty.starts_with("Option<") {
|
||||
ty = format!("Option<{ty}>");
|
||||
}
|
||||
let rename = if field_name == *field_raw {
|
||||
String::new()
|
||||
} else {
|
||||
format!(" #[serde(rename = \"{field_raw}\")]\n")
|
||||
};
|
||||
format!("{rename} pub {field_name}: {ty},")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
format!(
|
||||
"#[derive(Debug, Clone, Serialize, Deserialize)]\npub struct {name} {{\n{fields}\n}}\n",
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
fn emit_type_alias(name: &str, schema: &JsonSchema, ctx: &mut EnumCtx) -> String {
|
||||
ctx.depth = 0;
|
||||
ctx.enum_name = Some(name.to_string());
|
||||
let ty = rust_type_from_schema(schema, ctx);
|
||||
format!("pub type {name} = {ty};\n")
|
||||
}
|
||||
|
||||
|
||||
fn rust_type_from_schema(schema: &JsonSchema, ctx: &mut EnumCtx) -> String {
|
||||
if let Some(r) = schema.ref_name() {
|
||||
return rust_type_ident(r);
|
||||
}
|
||||
|
||||
if let Some(any_of) = &schema.any_of {
|
||||
let has_null = any_of.iter().any(|s| s.ty.as_deref() == Some("null"));
|
||||
let non_null: Vec<&JsonSchema> = any_of
|
||||
.iter()
|
||||
.filter(|s| s.ty.as_deref() != Some("null"))
|
||||
.collect();
|
||||
if has_null && non_null.len() == 1 {
|
||||
ctx.enum_name = None;
|
||||
return format!("Option<{}>", rust_type_from_schema(non_null[0], ctx));
|
||||
}
|
||||
}
|
||||
|
||||
let nullable = schema.nullable;
|
||||
let inner = inner_rust_type(schema, ctx);
|
||||
if nullable {
|
||||
format!("Option<{inner}>")
|
||||
} else {
|
||||
inner
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn inner_rust_type(schema: &JsonSchema, ctx: &mut EnumCtx) -> String {
|
||||
if let Some(values) = &schema.r#enum {
|
||||
if schema.ty.as_deref() == Some("string") {
|
||||
let enum_name = ctx
|
||||
.enum_name
|
||||
.clone()
|
||||
.unwrap_or_else(|| format!("Enum_{}", ctx.depth));
|
||||
ctx.hoisted.push((enum_name.clone(), values.clone()));
|
||||
return enum_name;
|
||||
}
|
||||
}
|
||||
match schema.ty.as_deref() {
|
||||
Some("integer") => "i64".to_string(),
|
||||
Some("number") => "f64".to_string(),
|
||||
Some("boolean") => "bool".to_string(),
|
||||
Some("string") => "String".to_string(),
|
||||
Some("array") => {
|
||||
ctx.depth += 1;
|
||||
ctx.enum_name = None;
|
||||
let inner = rust_type_from_schema(schema.items.as_deref().unwrap_or(&JsonSchema::default()), ctx);
|
||||
format!("Vec<{inner}>")
|
||||
}
|
||||
Some("object") => "serde_json::Value".to_string(),
|
||||
_ => "serde_json::Value".to_string(),
|
||||
}
|
||||
}
|
||||
369
protocol/mizan-codegen/src/emit/stage1.rs
Normal file
369
protocol/mizan-codegen/src/emit/stage1.rs
Normal file
@@ -0,0 +1,369 @@
|
||||
//! Stage 1 — framework-agnostic TypeScript emission.
|
||||
//!
|
||||
//! Output mirrors `protocol/mizan-generate/generator/lib/stage1.mjs`:
|
||||
//!
|
||||
//! types.ts — typed declarations for every Pydantic model
|
||||
//! contexts/<name>.ts — `fetch<Name>Context(params)` per context group
|
||||
//! mutations/<name>.ts — `call<Name>(args)` per mutation
|
||||
//! functions/<name>.ts — `call<Name>(args)` per plain function
|
||||
//! index.ts — re-exports
|
||||
//!
|
||||
//! The deterministic per-function/per-context files match the JS codegen
|
||||
//! byte-for-byte against an identical IR; types.ts emits Pydantic schemas
|
||||
//! directly as TS interfaces instead of routing through openapi-typescript.
|
||||
//! Consumers import by name from index.ts so the structural shape of
|
||||
//! types.ts is not load-bearing — only the named exports are.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use askama::Template;
|
||||
use indexmap::IndexMap;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::ir::{IsContext, JsonSchema, MizanContext, MizanFunction, MizanIR};
|
||||
use crate::emit::CodegenTarget;
|
||||
use crate::emit::EmittedFile;
|
||||
use crate::emit::casing::pascal_case;
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "stage1/call.ts.j2", escape = "none")]
|
||||
struct CallTemplate<'a> {
|
||||
pascal: &'a str,
|
||||
name: &'a str,
|
||||
has_input: bool,
|
||||
input_type: &'a str,
|
||||
output_type: &'a str,
|
||||
type_imports: Vec<String>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "stage1/context.ts.j2", escape = "none")]
|
||||
struct ContextTemplate<'a> {
|
||||
pascal: &'a str,
|
||||
ctx_name: &'a str,
|
||||
type_imports: Vec<String>,
|
||||
data_fields: Vec<ContextDataField<'a>>,
|
||||
has_params: bool,
|
||||
params: Vec<ContextParamField<'a>>,
|
||||
}
|
||||
|
||||
|
||||
struct ContextDataField<'a> {
|
||||
name: &'a str,
|
||||
output_type: &'a str,
|
||||
}
|
||||
|
||||
|
||||
struct ContextParamField<'a> {
|
||||
name: &'a str,
|
||||
ts_type: &'static str,
|
||||
required: bool,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "stage1/index.ts.j2", escape = "none")]
|
||||
struct IndexTemplate<'a> {
|
||||
contexts: Vec<IndexContext<'a>>,
|
||||
calls: Vec<IndexCall<'a>>,
|
||||
framework_adapters: Vec<&'static str>,
|
||||
}
|
||||
|
||||
|
||||
struct IndexContext<'a> {
|
||||
pascal: String,
|
||||
name: &'a str,
|
||||
}
|
||||
|
||||
|
||||
struct IndexCall<'a> {
|
||||
pascal: String,
|
||||
camel_name: &'a str,
|
||||
dir: &'static str,
|
||||
}
|
||||
|
||||
|
||||
pub struct Stage1;
|
||||
|
||||
|
||||
impl CodegenTarget for Stage1 {
|
||||
fn name(&self) -> &'static str { "stage1" }
|
||||
|
||||
fn emit(&self, ir: &MizanIR, config: &Config) -> Vec<EmittedFile> {
|
||||
let mut out: Vec<EmittedFile> = Vec::new();
|
||||
|
||||
out.push(EmittedFile::new("types.ts", emit_types(&ir.components.schemas)));
|
||||
|
||||
for (ctx_name, ctx_meta) in &ir.contexts {
|
||||
let content = emit_context_file(ctx_name, ctx_meta, &ir.functions);
|
||||
out.push(EmittedFile::new(
|
||||
PathBuf::from("contexts").join(format!("{ctx_name}.ts")),
|
||||
content,
|
||||
));
|
||||
}
|
||||
|
||||
for fn_meta in regular_functions(&ir.functions) {
|
||||
let dir = if fn_meta.affects.is_empty() { "functions" } else { "mutations" };
|
||||
let content = emit_call_file(fn_meta);
|
||||
out.push(EmittedFile::new(
|
||||
PathBuf::from(dir).join(format!("{}.ts", fn_meta.camel_name)),
|
||||
content,
|
||||
));
|
||||
}
|
||||
|
||||
out.push(EmittedFile::new("index.ts", emit_stage1_index(ir, config)));
|
||||
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn regular_functions(functions: &[MizanFunction]) -> impl Iterator<Item = &MizanFunction> {
|
||||
functions.iter().filter(|f| matches!(f.is_context, IsContext::No) && !f.is_form)
|
||||
}
|
||||
|
||||
|
||||
fn dedupe_preserving_order(items: impl IntoIterator<Item = String>) -> Vec<String> {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
items.into_iter().filter(|s| seen.insert(s.clone())).collect()
|
||||
}
|
||||
|
||||
|
||||
// ─── Per-context file ──────────────────────────────────────────────────────
|
||||
|
||||
|
||||
fn emit_context_file(
|
||||
ctx_name: &str,
|
||||
ctx_meta: &MizanContext,
|
||||
all_functions: &[MizanFunction],
|
||||
) -> String {
|
||||
let pascal = pascal_case(ctx_name);
|
||||
let ctx_fns: Vec<&MizanFunction> = all_functions
|
||||
.iter()
|
||||
.filter(|f| f.is_context.as_str() == Some(ctx_name))
|
||||
.collect();
|
||||
|
||||
let type_imports = dedupe_preserving_order(
|
||||
ctx_fns.iter().map(|f| f.output_type.clone()),
|
||||
);
|
||||
|
||||
let data_fields: Vec<ContextDataField> = ctx_fns
|
||||
.iter()
|
||||
.map(|f| ContextDataField { name: &f.name, output_type: &f.output_type })
|
||||
.collect();
|
||||
|
||||
let params: Vec<ContextParamField> = ctx_meta.params.iter()
|
||||
.map(|(name, meta)| ContextParamField {
|
||||
name,
|
||||
ts_type: json_ty_to_ts(&meta.ty),
|
||||
required: meta.required,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let template = ContextTemplate {
|
||||
pascal: &pascal,
|
||||
ctx_name,
|
||||
type_imports,
|
||||
data_fields,
|
||||
has_params: !ctx_meta.params.is_empty(),
|
||||
params,
|
||||
};
|
||||
template.render().expect("context template renders")
|
||||
}
|
||||
|
||||
|
||||
fn json_ty_to_ts(json_ty: &str) -> &'static str {
|
||||
match json_ty {
|
||||
"integer" | "number" => "number",
|
||||
"boolean" => "boolean",
|
||||
_ => "string",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ─── Per-function (call) file — same shape for mutations + plain ──────────
|
||||
|
||||
|
||||
fn emit_call_file(fn_meta: &MizanFunction) -> String {
|
||||
let pascal = pascal_case(&fn_meta.camel_name);
|
||||
|
||||
let mut imports: Vec<String> = Vec::new();
|
||||
if fn_meta.has_input {
|
||||
if let Some(t) = &fn_meta.input_type { imports.push(t.clone()); }
|
||||
}
|
||||
imports.push(fn_meta.output_type.clone());
|
||||
let type_imports = dedupe_preserving_order(imports);
|
||||
|
||||
let template = CallTemplate {
|
||||
pascal: &pascal,
|
||||
name: &fn_meta.name,
|
||||
has_input: fn_meta.has_input,
|
||||
input_type: fn_meta.input_type.as_deref().unwrap_or(""),
|
||||
output_type: &fn_meta.output_type,
|
||||
type_imports,
|
||||
};
|
||||
template.render().expect("call template renders")
|
||||
}
|
||||
|
||||
|
||||
// ─── Stage 1 index ─────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
fn emit_stage1_index(ir: &MizanIR, config: &Config) -> String {
|
||||
let contexts: Vec<IndexContext> = ir.contexts.keys()
|
||||
.map(|ctx_name| IndexContext { pascal: pascal_case(ctx_name), name: ctx_name })
|
||||
.collect();
|
||||
|
||||
let calls: Vec<IndexCall> = regular_functions(&ir.functions)
|
||||
.map(|fn_meta| IndexCall {
|
||||
pascal: pascal_case(&fn_meta.camel_name),
|
||||
camel_name: &fn_meta.camel_name,
|
||||
dir: if fn_meta.affects.is_empty() { "functions" } else { "mutations" },
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Stage 2 single-file frontend adapters get re-exported from index.ts so
|
||||
// consumers can `import { MizanContext, useEcho } from './api'`.
|
||||
let framework_adapters: Vec<&'static str> = ["react", "vue", "svelte"].iter()
|
||||
.copied()
|
||||
.filter(|t| config.targets.iter().any(|cfg_t| cfg_t == t))
|
||||
.collect();
|
||||
|
||||
IndexTemplate { contexts, calls, framework_adapters }
|
||||
.render().expect("index template renders")
|
||||
}
|
||||
|
||||
|
||||
// ─── types.ts ──────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
fn emit_types(schemas: &IndexMap<String, JsonSchema>) -> String {
|
||||
let mut out = String::new();
|
||||
out.push_str("// AUTO-GENERATED by mizan — do not edit\n\n");
|
||||
for (raw_name, schema) in schemas {
|
||||
out.push_str(&emit_schema_decl(raw_name, schema));
|
||||
out.push('\n');
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
|
||||
fn emit_schema_decl(name: &str, schema: &JsonSchema) -> String {
|
||||
// String enum → union of string literals.
|
||||
if let Some(values) = &schema.r#enum {
|
||||
if schema.ty.as_deref() == Some("string") {
|
||||
let union = values
|
||||
.iter()
|
||||
.filter_map(|v| v.as_str())
|
||||
.map(|s| format!("\"{s}\""))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" | ");
|
||||
return format!("export type {name} = {union}\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Top-level array → array alias.
|
||||
if schema.ty.as_deref() == Some("array") {
|
||||
let elem = ts_type_expression(schema.items.as_deref().unwrap_or(&JsonSchema::default()));
|
||||
return format!("export type {name} = {elem}[]\n");
|
||||
}
|
||||
|
||||
// Object with properties → interface declaration.
|
||||
if schema.ty.as_deref() == Some("object") {
|
||||
if let Some(props) = &schema.properties {
|
||||
return emit_interface(name, schema, props);
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback — alias to a structural expression.
|
||||
let expr = ts_type_expression(schema);
|
||||
format!("export type {name} = {expr}\n")
|
||||
}
|
||||
|
||||
|
||||
fn emit_interface(
|
||||
name: &str,
|
||||
schema: &JsonSchema,
|
||||
properties: &IndexMap<String, JsonSchema>,
|
||||
) -> String {
|
||||
let required: std::collections::HashSet<&str> =
|
||||
schema.required.iter().map(String::as_str).collect();
|
||||
|
||||
let fields = properties
|
||||
.iter()
|
||||
.map(|(field_name, field_schema)| {
|
||||
// Fields are non-optional if they're explicitly required OR
|
||||
// if they carry a default value (server always populates).
|
||||
let is_required = required.contains(field_name.as_str())
|
||||
|| field_schema.default.is_some();
|
||||
let opt = if is_required { "" } else { "?" };
|
||||
let ty = ts_type_expression(field_schema);
|
||||
format!(" {field_name}{opt}: {ty}")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
if fields.is_empty() {
|
||||
format!("export interface {name} {{}}\n")
|
||||
} else {
|
||||
format!("export interface {name} {{\n{fields}\n}}\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn ts_type_expression(schema: &JsonSchema) -> String {
|
||||
// `$ref` → bare type name reference into components.schemas.
|
||||
if let Some(ref_name) = schema.ref_name() {
|
||||
return ref_name.to_string();
|
||||
}
|
||||
|
||||
// `anyOf` with a null variant → `T | null`.
|
||||
if let Some(any_of) = &schema.any_of {
|
||||
let has_null = any_of.iter().any(|s| s.ty.as_deref() == Some("null"));
|
||||
let non_null: Vec<&JsonSchema> = any_of
|
||||
.iter()
|
||||
.filter(|s| s.ty.as_deref() != Some("null"))
|
||||
.collect();
|
||||
if has_null && non_null.len() == 1 {
|
||||
return format!("{} | null", ts_type_expression(non_null[0]));
|
||||
}
|
||||
let union = any_of
|
||||
.iter()
|
||||
.map(ts_type_expression)
|
||||
.collect::<Vec<_>>()
|
||||
.join(" | ");
|
||||
return union;
|
||||
}
|
||||
|
||||
if let Some(values) = &schema.r#enum {
|
||||
if schema.ty.as_deref() == Some("string") {
|
||||
return values
|
||||
.iter()
|
||||
.filter_map(|v| v.as_str())
|
||||
.map(|s| format!("\"{s}\""))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" | ");
|
||||
}
|
||||
}
|
||||
|
||||
let base = match schema.ty.as_deref() {
|
||||
Some("integer") | Some("number") => "number".to_string(),
|
||||
Some("boolean") => "boolean".to_string(),
|
||||
Some("string") => "string".to_string(),
|
||||
Some("array") => {
|
||||
let elem = ts_type_expression(schema.items.as_deref().unwrap_or(&JsonSchema::default()));
|
||||
format!("{elem}[]")
|
||||
}
|
||||
Some("object") => "Record<string, unknown>".to_string(),
|
||||
Some("null") => "null".to_string(),
|
||||
_ => "unknown".to_string(),
|
||||
};
|
||||
|
||||
if schema.nullable {
|
||||
format!("{base} | null")
|
||||
} else {
|
||||
base
|
||||
}
|
||||
}
|
||||
78
protocol/mizan-codegen/src/emit/svelte.rs
Normal file
78
protocol/mizan-codegen/src/emit/svelte.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
//! Svelte target — readable store per context, re-export per call.
|
||||
//! Output shape lives at `templates/svelte/svelte.ts.j2`.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use askama::Template;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::emit::CodegenTarget;
|
||||
use crate::emit::EmittedFile;
|
||||
use crate::emit::casing::pascal_case;
|
||||
use crate::ir::{IsContext, MizanIR};
|
||||
|
||||
|
||||
pub struct SvelteAdapter;
|
||||
|
||||
|
||||
impl CodegenTarget for SvelteAdapter {
|
||||
fn name(&self) -> &'static str { "svelte" }
|
||||
|
||||
fn emit(&self, ir: &MizanIR, _config: &Config) -> Vec<EmittedFile> {
|
||||
let content = build_template(ir).render().expect("svelte template renders");
|
||||
vec![EmittedFile::new(PathBuf::from("svelte.ts"), content)]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "svelte/svelte.ts.j2", escape = "none")]
|
||||
struct SvelteTemplate<'a> {
|
||||
stage1_imports: Vec<String>,
|
||||
contexts: Vec<CtxRender<'a>>,
|
||||
call_exports: Vec<String>,
|
||||
}
|
||||
|
||||
|
||||
struct CtxRender<'a> {
|
||||
pascal: String,
|
||||
name: &'a str,
|
||||
has_params: bool,
|
||||
params_arg: &'static str,
|
||||
}
|
||||
|
||||
|
||||
fn build_template(ir: &MizanIR) -> SvelteTemplate<'_> {
|
||||
let contexts: Vec<CtxRender> = ir.contexts.iter()
|
||||
.map(|(ctx_name, ctx_meta)| {
|
||||
let has_params = !ctx_meta.params.is_empty();
|
||||
CtxRender {
|
||||
pascal: pascal_case(ctx_name),
|
||||
name: ctx_name,
|
||||
has_params,
|
||||
params_arg: if has_params { "params" } else { "{} as any" },
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mutations = ir.functions.iter()
|
||||
.filter(|f| matches!(f.is_context, IsContext::No) && !f.is_form && !f.affects.is_empty());
|
||||
let plain_fns = ir.functions.iter()
|
||||
.filter(|f| matches!(f.is_context, IsContext::No) && !f.is_form && f.affects.is_empty());
|
||||
let call_exports: Vec<String> = mutations.chain(plain_fns)
|
||||
.map(|f| pascal_case(&f.camel_name))
|
||||
.collect();
|
||||
|
||||
let mut stage1: Vec<String> = Vec::new();
|
||||
for ctx_name in ir.contexts.keys() {
|
||||
let p = pascal_case(ctx_name);
|
||||
stage1.push(format!("fetch{p}Context"));
|
||||
stage1.push(format!("type {p}ContextData"));
|
||||
stage1.push(format!("type {p}ContextParams"));
|
||||
}
|
||||
for c in &call_exports {
|
||||
stage1.push(format!("call{c}"));
|
||||
}
|
||||
|
||||
SvelteTemplate { stage1_imports: stage1, contexts, call_exports }
|
||||
}
|
||||
107
protocol/mizan-codegen/src/emit/vue.rs
Normal file
107
protocol/mizan-codegen/src/emit/vue.rs
Normal file
@@ -0,0 +1,107 @@
|
||||
//! Vue target — composable per context + composable per call.
|
||||
//! Output shape lives at `templates/vue/vue.ts.j2`.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use askama::Template;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::emit::CodegenTarget;
|
||||
use crate::emit::EmittedFile;
|
||||
use crate::emit::casing::pascal_case;
|
||||
use crate::ir::{IsContext, MizanFunction, MizanIR};
|
||||
|
||||
|
||||
pub struct VueAdapter;
|
||||
|
||||
|
||||
impl CodegenTarget for VueAdapter {
|
||||
fn name(&self) -> &'static str { "vue" }
|
||||
|
||||
fn emit(&self, ir: &MizanIR, _config: &Config) -> Vec<EmittedFile> {
|
||||
let content = build_template(ir).render().expect("vue template renders");
|
||||
vec![EmittedFile::new(PathBuf::from("vue.ts"), content)]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "vue/vue.ts.j2", escape = "none")]
|
||||
struct VueTemplate<'a> {
|
||||
stage1_imports: Vec<String>,
|
||||
contexts: Vec<CtxRender<'a>>,
|
||||
calls: Vec<CallRender>,
|
||||
}
|
||||
|
||||
|
||||
struct CtxRender<'a> {
|
||||
pascal: String,
|
||||
name: &'a str,
|
||||
has_params: bool,
|
||||
params_arg: &'static str,
|
||||
fns: Vec<FnRender<'a>>,
|
||||
}
|
||||
|
||||
|
||||
struct FnRender<'a> {
|
||||
camel_name: &'a str,
|
||||
name: &'a str,
|
||||
output_type: &'a str,
|
||||
}
|
||||
|
||||
|
||||
struct CallRender {
|
||||
pascal: String,
|
||||
has_input: bool,
|
||||
}
|
||||
|
||||
|
||||
fn build_template(ir: &MizanIR) -> VueTemplate<'_> {
|
||||
let contexts: Vec<CtxRender> = ir.contexts.iter()
|
||||
.map(|(ctx_name, ctx_meta)| {
|
||||
let has_params = !ctx_meta.params.is_empty();
|
||||
let ctx_fns: Vec<FnRender> = ir.functions.iter()
|
||||
.filter(|f| f.is_context.as_str() == Some(ctx_name.as_str()))
|
||||
.map(|f| FnRender {
|
||||
camel_name: &f.camel_name,
|
||||
name: &f.name,
|
||||
output_type: &f.output_type,
|
||||
})
|
||||
.collect();
|
||||
CtxRender {
|
||||
pascal: pascal_case(ctx_name),
|
||||
name: ctx_name,
|
||||
has_params,
|
||||
params_arg: if has_params { "params" } else { "{} as any" },
|
||||
fns: ctx_fns,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mutations: Vec<&MizanFunction> = ir.functions.iter()
|
||||
.filter(|f| matches!(f.is_context, IsContext::No) && !f.is_form && !f.affects.is_empty())
|
||||
.collect();
|
||||
let plain_fns: Vec<&MizanFunction> = ir.functions.iter()
|
||||
.filter(|f| matches!(f.is_context, IsContext::No) && !f.is_form && f.affects.is_empty())
|
||||
.collect();
|
||||
|
||||
let calls: Vec<CallRender> = mutations.iter().chain(plain_fns.iter())
|
||||
.map(|f| CallRender {
|
||||
pascal: pascal_case(&f.camel_name),
|
||||
has_input: f.has_input,
|
||||
})
|
||||
.collect();
|
||||
|
||||
let mut stage1: Vec<String> = Vec::new();
|
||||
for ctx_name in ir.contexts.keys() {
|
||||
let p = pascal_case(ctx_name);
|
||||
stage1.push(format!("fetch{p}Context"));
|
||||
stage1.push(format!("type {p}ContextData"));
|
||||
stage1.push(format!("type {p}ContextParams"));
|
||||
}
|
||||
for fn_meta in mutations.iter().chain(plain_fns.iter()) {
|
||||
stage1.push(format!("call{}", pascal_case(&fn_meta.camel_name)));
|
||||
}
|
||||
|
||||
VueTemplate { stage1_imports: stage1, contexts, calls }
|
||||
}
|
||||
149
protocol/mizan-codegen/src/fetch.rs
Normal file
149
protocol/mizan-codegen/src/fetch.rs
Normal file
@@ -0,0 +1,149 @@
|
||||
//! Schema fetching — spawns the configured backend's schema-export command
|
||||
//! and deserializes its stdout into a typed `MizanIR`.
|
||||
//!
|
||||
//! Two backends recognized today:
|
||||
//! - FastAPI: `python -m mizan_fastapi.cli <module>`
|
||||
//! - Django: `python manage.py export_mizan_schema --indent 0`
|
||||
//!
|
||||
//! The fetcher reads stdout, skips any banner text before the first `{`,
|
||||
//! and parses the remainder as JSON.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
|
||||
use crate::config::{Config, DjangoSource, FastapiSource};
|
||||
use crate::ir::MizanIR;
|
||||
|
||||
|
||||
pub fn fetch_schema(config: &Config, config_dir: &Path) -> Result<MizanIR> {
|
||||
let raw = if let Some(fa) = &config.source.fastapi {
|
||||
run_fastapi(fa, config_dir)?
|
||||
} else if let Some(dj) = &config.source.django {
|
||||
run_django(dj, config_dir)?
|
||||
} else {
|
||||
return Err(anyhow!(
|
||||
"config.source must declare either [source.fastapi] or [source.django]"
|
||||
));
|
||||
};
|
||||
|
||||
parse_ir(&raw)
|
||||
}
|
||||
|
||||
|
||||
fn run_fastapi(src: &FastapiSource, config_dir: &Path) -> Result<String> {
|
||||
let cwd = match &src.cwd {
|
||||
Some(rel) => config_dir.join(rel),
|
||||
None => config_dir.to_path_buf(),
|
||||
};
|
||||
|
||||
let (program, mut args) = resolve_command(&src.command, &src.python);
|
||||
args.extend([
|
||||
"-m".to_string(),
|
||||
"mizan_fastapi.cli".to_string(),
|
||||
src.module.clone(),
|
||||
]);
|
||||
|
||||
run_subprocess(&program, &args, &cwd, &src.env, "FastAPI schema export")
|
||||
}
|
||||
|
||||
|
||||
fn run_django(src: &DjangoSource, config_dir: &Path) -> Result<String> {
|
||||
let manage_path = config_dir.join(&src.manage_path);
|
||||
let manage_dir = manage_path
|
||||
.parent()
|
||||
.ok_or_else(|| anyhow!("django manage_path has no parent: {}", manage_path.display()))?
|
||||
.to_path_buf();
|
||||
|
||||
let (program, mut args) = resolve_command(&src.command, &src.python);
|
||||
|
||||
// If the user supplied an explicit command (e.g. `uv run python`), they
|
||||
// expect to invoke from the manage_dir without a path prefix on manage.py.
|
||||
// Otherwise we pass the absolute manage_path so the python interpreter
|
||||
// doesn't depend on cwd.
|
||||
if src.command.is_some() {
|
||||
args.push("manage.py".to_string());
|
||||
} else {
|
||||
args.push(manage_path.to_string_lossy().into_owned());
|
||||
}
|
||||
args.extend([
|
||||
"export_mizan_schema".to_string(),
|
||||
"--indent".to_string(),
|
||||
"0".to_string(),
|
||||
]);
|
||||
|
||||
run_subprocess(&program, &args, &manage_dir, &src.env, "Django schema export")
|
||||
}
|
||||
|
||||
|
||||
fn resolve_command(
|
||||
explicit: &Option<Vec<String>>,
|
||||
python_override: &Option<String>,
|
||||
) -> (String, Vec<String>) {
|
||||
if let Some(cmd) = explicit {
|
||||
let (head, tail) = cmd.split_first().expect("command must be non-empty");
|
||||
return (head.clone(), tail.to_vec());
|
||||
}
|
||||
let python = python_override.as_deref().unwrap_or("python");
|
||||
(python.to_string(), Vec::new())
|
||||
}
|
||||
|
||||
|
||||
fn run_subprocess(
|
||||
program: &str,
|
||||
args: &[String],
|
||||
cwd: &Path,
|
||||
env: &std::collections::BTreeMap<String, String>,
|
||||
label: &str,
|
||||
) -> Result<String> {
|
||||
let mut cmd = Command::new(program);
|
||||
cmd.args(args).current_dir(cwd);
|
||||
for (k, v) in env {
|
||||
cmd.env(k, v);
|
||||
}
|
||||
|
||||
let output = cmd
|
||||
.output()
|
||||
.with_context(|| format!("spawning {label} ({program})"))?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
return Err(anyhow!(
|
||||
"{label} failed (exit {:?})\n--- stderr ---\n{stderr}\n--- stdout ---\n{stdout}",
|
||||
output.status.code(),
|
||||
));
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8(output.stdout)
|
||||
.with_context(|| format!("{label}: non-UTF-8 stdout"))?;
|
||||
Ok(stdout)
|
||||
}
|
||||
|
||||
|
||||
fn parse_ir(raw: &str) -> Result<MizanIR> {
|
||||
let json_start = raw
|
||||
.find('{')
|
||||
.ok_or_else(|| anyhow!("no JSON object found in schema-export output"))?;
|
||||
serde_json::from_str(&raw[json_start..]).context("deserializing Mizan IR from schema JSON")
|
||||
}
|
||||
|
||||
|
||||
/// Library helper for tests: deserialize an IR from a pre-fetched JSON string
|
||||
/// (no subprocess). Mirrors `parse_ir` but exposed for crate-external callers.
|
||||
pub fn parse_ir_from_str(json: &str) -> Result<MizanIR> {
|
||||
parse_ir(json)
|
||||
}
|
||||
|
||||
|
||||
/// Library helper: resolve a path relative to the config directory, returning
|
||||
/// an absolute path. Consumers may want this when constructing output paths.
|
||||
pub fn resolve_path(config_dir: &Path, p: impl Into<PathBuf>) -> PathBuf {
|
||||
let p = p.into();
|
||||
if p.is_absolute() {
|
||||
p
|
||||
} else {
|
||||
config_dir.join(p)
|
||||
}
|
||||
}
|
||||
248
protocol/mizan-codegen/src/ir.rs
Normal file
248
protocol/mizan-codegen/src/ir.rs
Normal file
@@ -0,0 +1,248 @@
|
||||
//! Mizan IR — strongly-typed deserialization of the backends' schema export.
|
||||
//!
|
||||
//! Every Mizan backend (Django, FastAPI, mizan-ts) emits the same OpenAPI
|
||||
//! document with three load-bearing extension fields:
|
||||
//! - `x-mizan-functions` — array of function entries
|
||||
//! - `x-mizan-contexts` — map of context groups
|
||||
//! - `components.schemas` — OpenAPI Pydantic→JSONSchema per Input/Output
|
||||
//!
|
||||
//! The structs here deserialize that JSON envelope into typed Rust values
|
||||
//! the emit targets walk. The OpenAPI document body (paths, info, etc.) is
|
||||
//! intentionally not modeled — the codegen consumes only the extensions.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use serde::Deserialize;
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct MizanIR {
|
||||
#[serde(rename = "x-mizan-functions", default)]
|
||||
pub functions: Vec<MizanFunction>,
|
||||
|
||||
#[serde(rename = "x-mizan-contexts", default)]
|
||||
pub contexts: IndexMap<String, MizanContext>,
|
||||
|
||||
/// Django-only channel registrations. FastAPI backends emit an empty list.
|
||||
#[serde(rename = "x-mizan-channels", default)]
|
||||
pub channels: Vec<MizanChannel>,
|
||||
|
||||
#[serde(default)]
|
||||
pub components: Components,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct MizanChannel {
|
||||
pub name: String,
|
||||
#[serde(rename = "pascalName")]
|
||||
pub pascal_name: String,
|
||||
#[serde(rename = "hasParams", default)]
|
||||
pub has_params: bool,
|
||||
#[serde(rename = "hasReactMessage", default)]
|
||||
pub has_react_message: bool,
|
||||
#[serde(rename = "hasDjangoMessage", default)]
|
||||
pub has_django_message: bool,
|
||||
#[serde(rename = "paramsType", default)]
|
||||
pub params_type: Option<String>,
|
||||
#[serde(rename = "reactMessageType", default)]
|
||||
pub react_message_type: Option<String>,
|
||||
#[serde(rename = "djangoMessageType", default)]
|
||||
pub django_message_type: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct MizanFunction {
|
||||
pub name: String,
|
||||
|
||||
#[serde(rename = "camelName")]
|
||||
pub camel_name: String,
|
||||
|
||||
#[serde(rename = "hasInput")]
|
||||
pub has_input: bool,
|
||||
|
||||
#[serde(rename = "inputType")]
|
||||
pub input_type: Option<String>,
|
||||
|
||||
#[serde(rename = "outputType")]
|
||||
pub output_type: String,
|
||||
|
||||
#[serde(rename = "outputNullable", default)]
|
||||
pub output_nullable: bool,
|
||||
|
||||
pub transport: Transport,
|
||||
|
||||
#[serde(rename = "isContext", default)]
|
||||
pub is_context: IsContext,
|
||||
|
||||
#[serde(rename = "isForm", default)]
|
||||
pub is_form: bool,
|
||||
|
||||
#[serde(rename = "formName", default)]
|
||||
pub form_name: Option<String>,
|
||||
|
||||
#[serde(rename = "formRole", default)]
|
||||
pub form_role: Option<String>,
|
||||
|
||||
#[serde(default)]
|
||||
pub affects: Vec<AffectTarget>,
|
||||
|
||||
/// Names of contexts whose state is patched by this function's return
|
||||
/// body via the kernel's `splice_slot` merger. Empty when the function
|
||||
/// is not a merge target.
|
||||
#[serde(default)]
|
||||
pub merge: Vec<String>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize, Default, Clone, Copy, PartialEq, Eq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Transport {
|
||||
#[default]
|
||||
Http,
|
||||
Websocket,
|
||||
Both,
|
||||
}
|
||||
|
||||
|
||||
/// IR-level `isContext` value. The backends emit `false` for non-context
|
||||
/// functions and a string (`"global"`, `"user"`, …) for context-grouped
|
||||
/// functions. Custom Deserialize bridges the boolean/string union into a
|
||||
/// typed Rust enum.
|
||||
#[derive(Debug, Default, Clone, PartialEq, Eq)]
|
||||
pub enum IsContext {
|
||||
#[default]
|
||||
No,
|
||||
Yes(String),
|
||||
}
|
||||
|
||||
impl IsContext {
|
||||
pub fn as_str(&self) -> Option<&str> {
|
||||
match self {
|
||||
IsContext::No => None,
|
||||
IsContext::Yes(s) => Some(s.as_str()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for IsContext {
|
||||
fn deserialize<D>(de: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let v = serde_json::Value::deserialize(de)?;
|
||||
match v {
|
||||
serde_json::Value::Bool(false) => Ok(IsContext::No),
|
||||
serde_json::Value::Bool(true) => Err(serde::de::Error::custom(
|
||||
"isContext: bare `true` is not a valid context name",
|
||||
)),
|
||||
serde_json::Value::String(s) => Ok(IsContext::Yes(s)),
|
||||
serde_json::Value::Null => Ok(IsContext::No),
|
||||
other => Err(serde::de::Error::custom(format!(
|
||||
"isContext: expected `false` or string, got {other:?}"
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct AffectTarget {
|
||||
#[serde(rename = "type")]
|
||||
pub kind: AffectKind,
|
||||
pub name: String,
|
||||
#[serde(default)]
|
||||
pub context: Option<String>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize, Clone, Copy, PartialEq, Eq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum AffectKind {
|
||||
Context,
|
||||
Function,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize, Default, Clone)]
|
||||
pub struct MizanContext {
|
||||
#[serde(default)]
|
||||
pub functions: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub params: IndexMap<String, ContextParam>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct ContextParam {
|
||||
#[serde(rename = "type")]
|
||||
pub ty: String,
|
||||
|
||||
pub required: bool,
|
||||
|
||||
#[serde(rename = "sharedBy", default)]
|
||||
pub shared_by: Vec<String>,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Debug, Deserialize, Default)]
|
||||
pub struct Components {
|
||||
#[serde(default)]
|
||||
pub schemas: IndexMap<String, JsonSchema>,
|
||||
}
|
||||
|
||||
|
||||
/// JSON Schema subset used by the emit targets. Mirrors the surface the
|
||||
/// existing JS adapters traverse (`$ref`, `anyOf`, `enum`, `type`, `items`,
|
||||
/// `properties`, `required`, `nullable`). Unknown fields are stashed in
|
||||
/// `extra` so backends can include schema annotations the codegen ignores.
|
||||
#[derive(Debug, Deserialize, Default, Clone)]
|
||||
pub struct JsonSchema {
|
||||
#[serde(rename = "type", default)]
|
||||
pub ty: Option<String>,
|
||||
|
||||
#[serde(rename = "$ref", default)]
|
||||
pub r#ref: Option<String>,
|
||||
|
||||
#[serde(rename = "enum", default)]
|
||||
pub r#enum: Option<Vec<serde_json::Value>>,
|
||||
|
||||
#[serde(rename = "anyOf", default)]
|
||||
pub any_of: Option<Vec<JsonSchema>>,
|
||||
|
||||
#[serde(default)]
|
||||
pub nullable: bool,
|
||||
|
||||
#[serde(default)]
|
||||
pub items: Option<Box<JsonSchema>>,
|
||||
|
||||
#[serde(default)]
|
||||
pub properties: Option<IndexMap<String, JsonSchema>>,
|
||||
|
||||
#[serde(default)]
|
||||
pub required: Vec<String>,
|
||||
|
||||
#[serde(rename = "additionalProperties", default)]
|
||||
pub additional_properties: Option<serde_json::Value>,
|
||||
|
||||
/// Presence of this field means the schema has a default — the server
|
||||
/// always populates it. Consumers can treat the field as non-optional
|
||||
/// even if it's absent from `required`.
|
||||
#[serde(default)]
|
||||
pub default: Option<serde_json::Value>,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub extra: BTreeMap<String, serde_json::Value>,
|
||||
}
|
||||
|
||||
|
||||
impl JsonSchema {
|
||||
/// `$ref: "#/components/schemas/Foo"` → `Some("Foo")`.
|
||||
pub fn ref_name(&self) -> Option<&str> {
|
||||
self.r#ref
|
||||
.as_deref()
|
||||
.and_then(|s| s.strip_prefix("#/components/schemas/"))
|
||||
}
|
||||
}
|
||||
10
protocol/mizan-codegen/src/lib.rs
Normal file
10
protocol/mizan-codegen/src/lib.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
//! Mizan codegen — library surface for tests and tooling.
|
||||
//!
|
||||
//! The binary `mizan-generate` (src/main.rs) is the consumer entry point;
|
||||
//! the library re-exports IR / config / fetch / emit so integration tests
|
||||
//! can drive the substrate without spawning the binary.
|
||||
|
||||
pub mod config;
|
||||
pub mod emit;
|
||||
pub mod fetch;
|
||||
pub mod ir;
|
||||
164
protocol/mizan-codegen/src/main.rs
Normal file
164
protocol/mizan-codegen/src/main.rs
Normal file
@@ -0,0 +1,164 @@
|
||||
//! `mizan-generate` — Rust codegen binary.
|
||||
//!
|
||||
//! Replaces the Node-based `protocol/mizan-generate/generator/cli.mjs`.
|
||||
//! Reads `mizan.toml`, spawns the configured backend to fetch the IR, and
|
||||
//! dispatches each `--target` to its `CodegenTarget` impl. Per-target file
|
||||
//! emission writes under the configured `output` directory.
|
||||
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use clap::Parser;
|
||||
|
||||
use mizan_codegen::{config, emit, fetch};
|
||||
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(
|
||||
name = "mizan-generate",
|
||||
about = "Mizan code generator — consumes Mizan IR; emits typed clients.",
|
||||
)]
|
||||
struct Cli {
|
||||
/// Path to the codegen config file.
|
||||
#[arg(short, long, default_value = "mizan.toml")]
|
||||
config: PathBuf,
|
||||
|
||||
/// Output directory (overrides `output` in config).
|
||||
#[arg(short, long)]
|
||||
output: Option<PathBuf>,
|
||||
|
||||
/// Comma-separated list of targets (overrides `targets` in config).
|
||||
#[arg(short, long)]
|
||||
target: Option<String>,
|
||||
|
||||
/// Read the IR from a JSON file instead of spawning the backend's
|
||||
/// schema-export command. The fixture path used by integration tests.
|
||||
#[arg(long)]
|
||||
from_json: Option<PathBuf>,
|
||||
}
|
||||
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let config_exists = cli.config.exists();
|
||||
let mut config: config::Config = if config_exists {
|
||||
let config_text = fs::read_to_string(&cli.config)
|
||||
.with_context(|| format!("reading config: {}", cli.config.display()))?;
|
||||
toml::from_str(&config_text)
|
||||
.with_context(|| format!("parsing TOML: {}", cli.config.display()))?
|
||||
} else if cli.from_json.is_some() {
|
||||
// --from-json bypasses the fetcher, so a missing config is fine —
|
||||
// CLI flags supply output + targets.
|
||||
config::Config {
|
||||
project_id: None,
|
||||
output: PathBuf::from("."),
|
||||
targets: vec![],
|
||||
source: Default::default(),
|
||||
rust_kernel: None,
|
||||
rust_crate_name: None,
|
||||
}
|
||||
} else {
|
||||
return Err(anyhow::anyhow!(
|
||||
"config not found: {} (pass --from-json to skip fetch)",
|
||||
cli.config.display(),
|
||||
));
|
||||
};
|
||||
|
||||
if let Some(o) = cli.output {
|
||||
config.output = o;
|
||||
}
|
||||
if let Some(t) = cli.target {
|
||||
config.targets = t.split(',').map(|s| s.trim().to_string()).collect();
|
||||
}
|
||||
|
||||
let config_dir = if config_exists {
|
||||
resolve_config_dir(&cli.config)?
|
||||
} else {
|
||||
std::env::current_dir()?
|
||||
};
|
||||
|
||||
let ir = if let Some(json_path) = &cli.from_json {
|
||||
let abs = if json_path.is_absolute() {
|
||||
json_path.clone()
|
||||
} else {
|
||||
config_dir.join(json_path)
|
||||
};
|
||||
eprintln!("[mizan] Reading IR from {}", abs.display());
|
||||
let raw = fs::read_to_string(&abs)
|
||||
.with_context(|| format!("read {}", abs.display()))?;
|
||||
fetch::parse_ir_from_str(&raw)?
|
||||
} else {
|
||||
eprintln!("[mizan] Fetching schema...");
|
||||
fetch::fetch_schema(&config, &config_dir)?
|
||||
};
|
||||
|
||||
eprintln!(
|
||||
"[mizan] Loaded {} function(s), {} context group(s), {} schema(s)",
|
||||
ir.functions.len(),
|
||||
ir.contexts.len(),
|
||||
ir.components.schemas.len(),
|
||||
);
|
||||
|
||||
// Stage 1 is the framework-agnostic foundation that react/vue/svelte
|
||||
// import from. Auto-include it whenever any consumer of `./index`
|
||||
// (the Stage 1 re-export root) is in the target set.
|
||||
let needs_stage1 = config.targets.iter()
|
||||
.any(|t| matches!(t.as_str(), "react" | "vue" | "svelte"));
|
||||
if needs_stage1 && !config.targets.iter().any(|t| t == "stage1") {
|
||||
config.targets.insert(0, "stage1".to_string());
|
||||
}
|
||||
// Channels schema piggybacks on the main schema (x-mizan-channels);
|
||||
// auto-include the channels emit when react is the target and the
|
||||
// schema actually carries channels.
|
||||
if config.targets.iter().any(|t| t == "react")
|
||||
&& !ir.channels.is_empty()
|
||||
&& !config.targets.iter().any(|t| t == "channels")
|
||||
{
|
||||
config.targets.push("channels".to_string());
|
||||
}
|
||||
|
||||
eprintln!("[mizan] Targets: {}", config.targets.join(", "));
|
||||
|
||||
let output_dir = if config.output.is_absolute() {
|
||||
config.output.clone()
|
||||
} else {
|
||||
config_dir.join(&config.output)
|
||||
};
|
||||
|
||||
for target_name in &config.targets {
|
||||
let Some(target) = emit::target_by_name(target_name) else {
|
||||
eprintln!("[mizan] WARN: target '{target_name}' has no emitter yet (Phase 2 scaffold)");
|
||||
continue;
|
||||
};
|
||||
let files = target.emit(&ir, &config);
|
||||
for file in files {
|
||||
let path = output_dir.join(&file.rel_path);
|
||||
write_output(&path, &file.content)?;
|
||||
eprintln!("[mizan] {} -> {}", target.name(), file.rel_path.display());
|
||||
}
|
||||
}
|
||||
|
||||
eprintln!("[mizan] Generation complete.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn resolve_config_dir(config_path: &Path) -> Result<PathBuf> {
|
||||
let abs = fs::canonicalize(config_path)
|
||||
.with_context(|| format!("canonicalize {}", config_path.display()))?;
|
||||
Ok(abs
|
||||
.parent()
|
||||
.map(|p| p.to_path_buf())
|
||||
.unwrap_or_else(|| PathBuf::from(".")))
|
||||
}
|
||||
|
||||
|
||||
fn write_output(path: &Path, content: &str) -> Result<()> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)
|
||||
.with_context(|| format!("mkdir {}", parent.display()))?;
|
||||
}
|
||||
fs::write(path, content).with_context(|| format!("write {}", path.display()))
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
'use client'
|
||||
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { useChannel, type ChannelSubscription } from 'mizan/channels'
|
||||
|
||||
{% if !type_imports.is_empty() -%}
|
||||
import type { {{ type_imports|join(", ") }} } from './channels'
|
||||
|
||||
{% endif -%}
|
||||
// ── Channel Hooks ─────────────────────────────────────────────────────────
|
||||
|
||||
{% for ch in channels -%}
|
||||
/**
|
||||
* Hook for the {{ ch.name }} channel.
|
||||
*/
|
||||
{% if ch.has_params -%}
|
||||
export function use{{ ch.pascal_name }}Channel(params: {{ ch.params_type_or_record }}): ChannelSubscription<{{ ch.params_type_or_record }}, {{ ch.django_msg_type_or_never }}, {{ ch.react_msg_type_or_never }}> {
|
||||
return useChannel('{{ ch.name }}', params)
|
||||
}
|
||||
{% else -%}
|
||||
export function use{{ ch.pascal_name }}Channel(): ChannelSubscription<Record<string, never>, {{ ch.django_msg_type_or_never }}, {{ ch.react_msg_type_or_never }}> {
|
||||
return useChannel('{{ ch.name }}', {})
|
||||
}
|
||||
{% endif %}
|
||||
{% endfor -%}
|
||||
26
protocol/mizan-codegen/templates/channels/channels.ts.j2
Normal file
26
protocol/mizan-codegen/templates/channels/channels.ts.j2
Normal file
@@ -0,0 +1,26 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
{{ schemas_block }}
|
||||
// ── Channel Registry ──────────────────────────────────────────────────────
|
||||
|
||||
export const CHANNELS = {
|
||||
{%- for ch in channels %}
|
||||
{{ ch.name }}: {
|
||||
name: '{{ ch.name }}',
|
||||
pascalName: '{{ ch.pascal_name }}',
|
||||
hasParams: {{ ch.has_params }},
|
||||
hasReactMessage: {{ ch.has_react_message }},
|
||||
hasDjangoMessage: {{ ch.has_django_message }},
|
||||
{%- if ch.has_params %}
|
||||
paramsType: '{{ ch.params_type }}',
|
||||
{%- endif %}
|
||||
{%- if ch.has_react_message %}
|
||||
reactMessageType: '{{ ch.react_message_type }}',
|
||||
{%- endif %}
|
||||
{%- if ch.has_django_message %}
|
||||
djangoMessageType: '{{ ch.django_message_type }}',
|
||||
{%- endif %}
|
||||
},
|
||||
{%- endfor %}
|
||||
} as const
|
||||
|
||||
5
protocol/mizan-codegen/templates/python/__init__.py.j2
Normal file
5
protocol/mizan-codegen/templates/python/__init__.py.j2
Normal file
@@ -0,0 +1,5 @@
|
||||
# AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
from .client import MizanClient # noqa: F401
|
||||
from .types import * # noqa: F401, F403
|
||||
|
||||
39
protocol/mizan-codegen/templates/python/client.py.j2
Normal file
39
protocol/mizan-codegen/templates/python/client.py.j2
Normal file
@@ -0,0 +1,39 @@
|
||||
# AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
|
||||
# Built from frontends/mizan-rust with `maturin develop --features pyo3`.
|
||||
from mizan_rust import PyMizanClient, PyContextSubscription
|
||||
|
||||
from .types import * # noqa: F401, F403
|
||||
from .types import BaseModel # re-import for the synthesized ContextData classes
|
||||
|
||||
|
||||
class MizanClient:
|
||||
"""Typed Python facade over the PyO3 mizan-rust kernel."""
|
||||
|
||||
def __init__(self, base_url: str, *, session: bool = False,
|
||||
csrf_cookie_name: str = "csrftoken",
|
||||
csrf_header_name: str = "X-CSRFToken") -> None:
|
||||
self._inner = PyMizanClient(
|
||||
base_url,
|
||||
session=session,
|
||||
csrf_cookie_name=csrf_cookie_name,
|
||||
csrf_header_name=csrf_header_name,
|
||||
)
|
||||
|
||||
{{ ctx_methods_block }}
|
||||
{{ call_methods_block }}
|
||||
def invalidate(self, context: str) -> None:
|
||||
self._inner.invalidate(context)
|
||||
|
||||
def invalidate_scoped(self, context: str, params: dict[str, Any]) -> None:
|
||||
self._inner.invalidate_scoped(context, params)
|
||||
|
||||
|
||||
# ── Context data shapes (per-context bundle) ──────────────────────────────
|
||||
|
||||
{{ data_classes_block }}
|
||||
10
protocol/mizan-codegen/templates/python/types.py.j2
Normal file
10
protocol/mizan-codegen/templates/python/types.py.j2
Normal file
@@ -0,0 +1,10 @@
|
||||
# AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Literal
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
{{ schemas_block }}
|
||||
|
||||
180
protocol/mizan-codegen/templates/react/react.tsx.j2
Normal file
180
protocol/mizan-codegen/templates/react/react.tsx.j2
Normal file
@@ -0,0 +1,180 @@
|
||||
'use client'
|
||||
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
useSyncExternalStore,
|
||||
type ReactNode,
|
||||
} from 'react'
|
||||
import {
|
||||
configure,
|
||||
initSession,
|
||||
mizanCall,
|
||||
mizanFetch,
|
||||
MizanError,
|
||||
registerContext,
|
||||
type ContextState,
|
||||
} from '@mizan/base'
|
||||
|
||||
{% if !stage1_imports.is_empty() -%}
|
||||
import { {{ stage1_imports|join(", ") }} } from './index'
|
||||
|
||||
{% endif -%}
|
||||
// Internal — runs inside a Provider, registers with the kernel exactly once.
|
||||
function useContextSubscription<T>(
|
||||
name: string,
|
||||
params: Record<string, any>,
|
||||
fetchFn: () => Promise<T>,
|
||||
initialData?: T,
|
||||
): ContextState<T> {
|
||||
const ref = useRef<ReturnType<typeof registerContext> | null>(null)
|
||||
if (!ref.current) {
|
||||
ref.current = registerContext(name, params, fetchFn, initialData)
|
||||
}
|
||||
const handle = ref.current
|
||||
|
||||
useEffect(() => {
|
||||
if (handle.getState().status === 'idle') handle.refetch()
|
||||
return () => handle.unregister()
|
||||
}, [handle])
|
||||
|
||||
return useSyncExternalStore(handle.subscribe, handle.getState, handle.getState)
|
||||
}
|
||||
|
||||
// Internal — wraps an imperative call() with isPending / error state.
|
||||
interface MutationHook<TArgs, TResult> {
|
||||
mutate: (args: TArgs) => Promise<TResult>
|
||||
isPending: boolean
|
||||
error: Error | null
|
||||
}
|
||||
|
||||
function useMutation<TArgs, TResult>(
|
||||
callFn: (args: TArgs) => Promise<TResult>,
|
||||
): MutationHook<TArgs, TResult> {
|
||||
const [isPending, setIsPending] = useState(false)
|
||||
const [error, setError] = useState<Error | null>(null)
|
||||
|
||||
const mutate = useCallback(async (args: TArgs) => {
|
||||
setIsPending(true)
|
||||
setError(null)
|
||||
try {
|
||||
return await callFn(args)
|
||||
} catch (e) {
|
||||
setError(e as Error)
|
||||
throw e
|
||||
} finally {
|
||||
setIsPending(false)
|
||||
}
|
||||
}, [callFn])
|
||||
|
||||
return { mutate, isPending, error }
|
||||
}
|
||||
{% if has_global %}
|
||||
// ── Global Context ──
|
||||
|
||||
const GlobalCtx = createContext<ContextState<GlobalContextData> | null>(null)
|
||||
|
||||
export function GlobalContextProvider({ children }: { children: ReactNode }) {
|
||||
const ssrData = typeof window !== 'undefined' ? (window as any).__MIZAN_SSR_DATA__ : undefined
|
||||
const state = useContextSubscription('global', {}, () => fetchGlobalContext({} as any), ssrData)
|
||||
return <GlobalCtx.Provider value={state}>{children}</GlobalCtx.Provider>
|
||||
}
|
||||
|
||||
export function useGlobalContext(): ContextState<GlobalContextData> {
|
||||
const ctx = useContext(GlobalCtx)
|
||||
if (!ctx) throw new Error('useGlobalContext requires <MizanContext> or <GlobalContextProvider>')
|
||||
return ctx
|
||||
}
|
||||
{% for fn in global_fns %}
|
||||
export function use{{ fn.pascal }}(): {{ fn.output_type }} | null {
|
||||
return useGlobalContext().data?.{{ fn.name }} ?? null
|
||||
}
|
||||
{% endfor -%}
|
||||
{% endif -%}
|
||||
{% for ctx in named_contexts %}
|
||||
// ── {{ ctx.pascal }} Context ──
|
||||
|
||||
const {{ ctx.pascal }}Ctx = createContext<ContextState<{{ ctx.pascal }}ContextData> | null>(null)
|
||||
|
||||
{% if ctx.has_params -%}
|
||||
export function {{ ctx.pascal }}Context({ children, ...params }: {{ ctx.pascal }}ContextParams & { children: ReactNode }) {
|
||||
const state = useContextSubscription('{{ ctx.name }}', params, () => fetch{{ ctx.pascal }}Context(params))
|
||||
return <{{ ctx.pascal }}Ctx.Provider value={state}>{children}</{{ ctx.pascal }}Ctx.Provider>
|
||||
}
|
||||
{% else -%}
|
||||
export function {{ ctx.pascal }}Context({ children }: { children: ReactNode }) {
|
||||
const state = useContextSubscription('{{ ctx.name }}', {}, () => fetch{{ ctx.pascal }}Context({} as any))
|
||||
return <{{ ctx.pascal }}Ctx.Provider value={state}>{children}</{{ ctx.pascal }}Ctx.Provider>
|
||||
}
|
||||
{% endif %}
|
||||
export function use{{ ctx.pascal }}Context(): ContextState<{{ ctx.pascal }}ContextData> {
|
||||
const ctx = useContext({{ ctx.pascal }}Ctx)
|
||||
if (!ctx) throw new Error('use{{ ctx.pascal }}Context requires <{{ ctx.pascal }}Context>')
|
||||
return ctx
|
||||
}
|
||||
{% for fn in ctx.fns %}
|
||||
export function use{{ fn.pascal }}(): {{ fn.output_type }} | null {
|
||||
return use{{ ctx.pascal }}Context().data?.{{ fn.name }} ?? null
|
||||
}
|
||||
{% endfor -%}
|
||||
{% endfor -%}
|
||||
{% for call in calls %}
|
||||
{% if call.has_input -%}
|
||||
export function use{{ call.pascal }}() {
|
||||
return useMutation<Parameters<typeof call{{ call.pascal }}>[0], Awaited<ReturnType<typeof call{{ call.pascal }}>>>(call{{ call.pascal }})
|
||||
}
|
||||
{% else -%}
|
||||
export function use{{ call.pascal }}() {
|
||||
return useMutation<void, Awaited<ReturnType<typeof call{{ call.pascal }}>>>(() => call{{ call.pascal }}() as any)
|
||||
}
|
||||
{% endif -%}
|
||||
{% endfor %}
|
||||
// ── MizanContext root provider ──
|
||||
|
||||
export interface MizanContextProps {
|
||||
/** Base URL for protocol endpoints. Defaults to "/api/mizan". */
|
||||
baseUrl?: string
|
||||
/** Set to `false` for backends without a `/session/` endpoint (e.g. FastAPI). */
|
||||
session?: boolean
|
||||
children: ReactNode
|
||||
}
|
||||
|
||||
/**
|
||||
* Root provider — calls configure() once and mounts the global context (if defined).
|
||||
* Must wrap any component using Mizan-generated hooks.
|
||||
*/
|
||||
export function MizanContext({ baseUrl, session, children }: MizanContextProps) {
|
||||
const configured = useRef(false)
|
||||
if (!configured.current) {
|
||||
const opts: Parameters<typeof configure>[0] = {}
|
||||
if (baseUrl !== undefined) opts.baseUrl = baseUrl
|
||||
if (session !== undefined) opts.session = session
|
||||
if (Object.keys(opts).length > 0) configure(opts)
|
||||
configured.current = true
|
||||
}
|
||||
{%- if has_global %}
|
||||
return <GlobalContextProvider>{children}</GlobalContextProvider>
|
||||
{%- else %}
|
||||
return <>{children}</>
|
||||
{%- endif %}
|
||||
}
|
||||
|
||||
// ── Imperative escape hatch ──
|
||||
|
||||
/**
|
||||
* Returns the imperative kernel API. For test harnesses or rare cases where
|
||||
* a typed generated hook does not fit. Most app code should use the typed hooks.
|
||||
*/
|
||||
export function useMizan() {
|
||||
return { call: mizanCall, fetch: mizanFetch }
|
||||
}
|
||||
|
||||
export type { ContextState } from '@mizan/base'
|
||||
export { configure, initSession, MizanError } from '@mizan/base'
|
||||
|
||||
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 }}
|
||||
17
protocol/mizan-codegen/templates/stage1/call.ts.j2
Normal file
17
protocol/mizan-codegen/templates/stage1/call.ts.j2
Normal file
@@ -0,0 +1,17 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/base'
|
||||
|
||||
{% if !type_imports.is_empty() -%}
|
||||
import type { {{ type_imports|join(", ") }} } from '../types'
|
||||
|
||||
{% endif -%}
|
||||
{% if has_input -%}
|
||||
export function call{{ pascal }}(args: {{ input_type }}): Promise<{{ output_type }}> {
|
||||
return mizanCall('{{ name }}', args)
|
||||
}
|
||||
{% else -%}
|
||||
export function call{{ pascal }}(): Promise<{{ output_type }}> {
|
||||
return mizanCall('{{ name }}', {})
|
||||
}
|
||||
{% endif %}
|
||||
28
protocol/mizan-codegen/templates/stage1/context.ts.j2
Normal file
28
protocol/mizan-codegen/templates/stage1/context.ts.j2
Normal file
@@ -0,0 +1,28 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanFetch } from '@mizan/base'
|
||||
|
||||
{% if !type_imports.is_empty() -%}
|
||||
import type { {{ type_imports|join(", ") }} } from '../types'
|
||||
|
||||
{% endif -%}
|
||||
export interface {{ pascal }}ContextData {
|
||||
{%- for field in data_fields %}
|
||||
{{ field.name }}: {{ field.output_type }}
|
||||
{%- endfor %}
|
||||
}
|
||||
|
||||
{% if has_params -%}
|
||||
export interface {{ pascal }}ContextParams {
|
||||
{%- for p in params %}
|
||||
{{ p.name }}{% if !p.required %}?{% endif %}: {{ p.ts_type }}
|
||||
{%- endfor %}
|
||||
}
|
||||
{%- else -%}
|
||||
export type {{ pascal }}ContextParams = Record<string, never>
|
||||
{%- endif %}
|
||||
|
||||
export function fetch{{ pascal }}Context(params: {{ pascal }}ContextParams): Promise<{{ pascal }}ContextData> {
|
||||
return mizanFetch('{{ ctx_name }}', params)
|
||||
}
|
||||
|
||||
20
protocol/mizan-codegen/templates/stage1/index.ts.j2
Normal file
20
protocol/mizan-codegen/templates/stage1/index.ts.j2
Normal file
@@ -0,0 +1,20 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
export * from './types'
|
||||
{% if !contexts.is_empty() %}
|
||||
{%- for ctx in contexts %}
|
||||
export { fetch{{ ctx.pascal }}Context, type {{ ctx.pascal }}ContextData, type {{ ctx.pascal }}ContextParams } from './contexts/{{ ctx.name }}'
|
||||
{%- endfor %}
|
||||
{% endif -%}
|
||||
{% if !calls.is_empty() %}
|
||||
{%- for call in calls %}
|
||||
export { call{{ call.pascal }} } from './{{ call.dir }}/{{ call.camel_name }}'
|
||||
{%- endfor %}
|
||||
{% endif -%}
|
||||
{% if !framework_adapters.is_empty() %}
|
||||
// Stage 2 framework adapter
|
||||
{%- for name in framework_adapters %}
|
||||
export * from './{{ name }}'
|
||||
{%- endfor %}
|
||||
{% endif -%}
|
||||
|
||||
31
protocol/mizan-codegen/templates/svelte/svelte.ts.j2
Normal file
31
protocol/mizan-codegen/templates/svelte/svelte.ts.j2
Normal file
@@ -0,0 +1,31 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { readable, type Readable } from 'svelte/store'
|
||||
import { registerContext, type ContextState } from '@mizan/base'
|
||||
|
||||
{% if !stage1_imports.is_empty() -%}
|
||||
import { {{ stage1_imports|join(", ") }} } from '../index'
|
||||
|
||||
{% endif -%}
|
||||
{% for ctx in contexts -%}
|
||||
export function create{{ ctx.pascal }}Context({% if ctx.has_params %}params: {{ ctx.pascal }}ContextParams{% endif %}) {
|
||||
const store = readable<ContextState<{{ ctx.pascal }}ContextData>>(
|
||||
{ data: null, status: 'idle', error: null },
|
||||
(set) => {
|
||||
const handle = registerContext('{{ ctx.name }}', {{ ctx.params_arg }}, () => fetch{{ ctx.pascal }}Context({{ ctx.params_arg }}))
|
||||
const unsub = handle.subscribe(() => set(handle.getState()))
|
||||
handle.refetch()
|
||||
return () => { unsub(); handle.unregister() }
|
||||
},
|
||||
)
|
||||
|
||||
return store
|
||||
}
|
||||
|
||||
{% endfor -%}
|
||||
{% for call in call_exports -%}
|
||||
export { call{{ call }} } from '../index'
|
||||
{% endfor %}
|
||||
export type { ContextState } from '@mizan/base'
|
||||
export { configure, initSession, MizanError } from '@mizan/base'
|
||||
|
||||
65
protocol/mizan-codegen/templates/vue/vue.ts.j2
Normal file
65
protocol/mizan-codegen/templates/vue/vue.ts.j2
Normal file
@@ -0,0 +1,65 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { ref, computed, onMounted, onUnmounted, onServerPrefetch, type ComputedRef } from 'vue'
|
||||
import { registerContext, type ContextState } from '@mizan/base'
|
||||
|
||||
{% if !stage1_imports.is_empty() -%}
|
||||
import { {{ stage1_imports|join(", ") }} } from '../index'
|
||||
|
||||
{% endif -%}
|
||||
{% for ctx in contexts -%}
|
||||
export function use{{ ctx.pascal }}Context({% if ctx.has_params %}params: {{ ctx.pascal }}ContextParams{% endif %}) {
|
||||
const state = ref<ContextState<{{ ctx.pascal }}ContextData>>({ data: null, status: 'idle', error: null })
|
||||
let handle: ReturnType<typeof registerContext> | null = null
|
||||
|
||||
onMounted(() => {
|
||||
handle = registerContext('{{ ctx.name }}', {{ ctx.params_arg }}, () => fetch{{ ctx.pascal }}Context({{ ctx.params_arg }}))
|
||||
handle.subscribe(() => { state.value = handle!.getState() })
|
||||
handle.refetch()
|
||||
})
|
||||
|
||||
onServerPrefetch(async () => {
|
||||
handle = registerContext('{{ ctx.name }}', {{ ctx.params_arg }}, () => fetch{{ ctx.pascal }}Context({{ ctx.params_arg }}))
|
||||
await handle.refetch()
|
||||
state.value = handle.getState()
|
||||
})
|
||||
|
||||
onUnmounted(() => { handle?.unregister() })
|
||||
|
||||
return {
|
||||
state,
|
||||
{%- for fn in ctx.fns %}
|
||||
{{ fn.camel_name }}: computed(() => state.value.data?.{{ fn.name }} ?? null) as ComputedRef<{{ fn.output_type }} | null>,
|
||||
{%- endfor %}
|
||||
loading: computed(() => state.value.status === 'loading'),
|
||||
error: computed(() => state.value.error),
|
||||
}
|
||||
}
|
||||
|
||||
{% endfor -%}
|
||||
{% for call in calls -%}
|
||||
export function use{{ call.pascal }}() {
|
||||
const isPending = ref(false)
|
||||
const error = ref<Error | null>(null)
|
||||
{%- if call.has_input %}
|
||||
async function mutate(args: Parameters<typeof call{{ call.pascal }}>[0]) {
|
||||
isPending.value = true; error.value = null
|
||||
try { return await call{{ call.pascal }}(args) }
|
||||
catch (e) { error.value = e as Error; throw e }
|
||||
finally { isPending.value = false }
|
||||
}
|
||||
{%- else %}
|
||||
async function mutate() {
|
||||
isPending.value = true; error.value = null
|
||||
try { return await call{{ call.pascal }}() }
|
||||
catch (e) { error.value = e as Error; throw e }
|
||||
finally { isPending.value = false }
|
||||
}
|
||||
{%- endif %}
|
||||
return { mutate, isPending, error }
|
||||
}
|
||||
|
||||
{% endfor -%}
|
||||
export type { ContextState } from '@mizan/base'
|
||||
export { configure, initSession, MizanError } from '@mizan/base'
|
||||
|
||||
80
protocol/mizan-codegen/tests/channels_smoke.rs
Normal file
80
protocol/mizan-codegen/tests/channels_smoke.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
//! Smoke test for the channels target against a synthetic fixture.
|
||||
//! The JS channels.mjs runs types through `openapi-typescript` which the
|
||||
//! Rust codegen replaces with direct interface emission; byte-equivalence
|
||||
//! against the JS baseline is intentionally not the gate. Instead this
|
||||
//! test checks structural properties of the emitted output.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use mizan_codegen::config::{Config, SourceConfig};
|
||||
use mizan_codegen::emit::CodegenTarget;
|
||||
use mizan_codegen::emit::channels::ChannelsTarget;
|
||||
use mizan_codegen::fetch::parse_ir_from_str;
|
||||
|
||||
|
||||
fn fixture_config() -> Config {
|
||||
Config {
|
||||
project_id: None,
|
||||
output: PathBuf::from("/tmp"),
|
||||
targets: vec!["channels".to_string()],
|
||||
source: SourceConfig { fastapi: None, django: None },
|
||||
rust_kernel: None,
|
||||
rust_crate_name: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn channels_target_emits_expected_files() {
|
||||
let raw = std::fs::read_to_string(
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/channels_schema.json"),
|
||||
).unwrap();
|
||||
let ir = parse_ir_from_str(&raw).unwrap();
|
||||
|
||||
let files = ChannelsTarget.emit(&ir, &fixture_config());
|
||||
assert_eq!(files.len(), 2, "channels target emits 2 files when channels present");
|
||||
|
||||
let by_path: BTreeMap<PathBuf, &str> =
|
||||
files.iter().map(|f| (f.rel_path.clone(), f.content.as_str())).collect();
|
||||
|
||||
let ts = by_path.get(&PathBuf::from("channels.ts"))
|
||||
.expect("channels.ts emitted");
|
||||
for expected in [
|
||||
"export interface ChatChannelParams",
|
||||
"export interface ChatReactMessage",
|
||||
"export interface ChatDjangoMessage",
|
||||
"export interface NotificationsDjangoMessage",
|
||||
"export const CHANNELS = {",
|
||||
"chat: {",
|
||||
"notifications: {",
|
||||
"hasParams: true",
|
||||
"hasParams: false",
|
||||
] {
|
||||
assert!(ts.contains(expected), "channels.ts must contain {expected:?}");
|
||||
}
|
||||
|
||||
let hooks = by_path.get(&PathBuf::from("channels.hooks.tsx"))
|
||||
.expect("channels.hooks.tsx emitted");
|
||||
for expected in [
|
||||
"import { useChannel, type ChannelSubscription } from 'mizan/channels'",
|
||||
"export function useChatChannel(params: ChatChannelParams)",
|
||||
"export function useNotificationsChannel()",
|
||||
"ChannelSubscription<ChatChannelParams, ChatDjangoMessage, ChatReactMessage>",
|
||||
"ChannelSubscription<Record<string, never>, NotificationsDjangoMessage, never>",
|
||||
] {
|
||||
assert!(hooks.contains(expected), "channels.hooks.tsx must contain {expected:?}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn channels_target_emits_nothing_when_empty() {
|
||||
// AFI fixture has no channels — target should produce zero files.
|
||||
let raw = std::fs::read_to_string(
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/afi_schema.json"),
|
||||
).unwrap();
|
||||
let ir = parse_ir_from_str(&raw).unwrap();
|
||||
let files = ChannelsTarget.emit(&ir, &fixture_config());
|
||||
assert!(files.is_empty(), "no channels → no files");
|
||||
}
|
||||
685
protocol/mizan-codegen/tests/fixtures/afi_schema.json
vendored
Normal file
685
protocol/mizan-codegen/tests/fixtures/afi_schema.json
vendored
Normal file
@@ -0,0 +1,685 @@
|
||||
{
|
||||
"openapi": "3.1.0",
|
||||
"info": {
|
||||
"title": "mizan Server Functions",
|
||||
"description": "Auto-generated schema for mizan server functions",
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"paths": {
|
||||
"/mizan/echo": {
|
||||
"post": {
|
||||
"summary": "Echoes the input back.",
|
||||
"operationId": "echo",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/echoInput"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/echoOutput"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-mizan": {
|
||||
"transport": "http",
|
||||
"isContext": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"/mizan/whoami": {
|
||||
"post": {
|
||||
"summary": "Returns the current user identity.",
|
||||
"operationId": "whoami",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/whoamiOutput"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-mizan": {
|
||||
"transport": "http",
|
||||
"isContext": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"/mizan/user_profile": {
|
||||
"post": {
|
||||
"summary": "One half of the user context.",
|
||||
"operationId": "userProfile",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/userProfileInput"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/userProfileOutput"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-mizan": {
|
||||
"transport": "http",
|
||||
"isContext": "user"
|
||||
}
|
||||
}
|
||||
},
|
||||
"/mizan/user_orders": {
|
||||
"post": {
|
||||
"summary": "Other half of the user context \u2014 same param, proves param elevation.",
|
||||
"operationId": "userOrders",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/userOrdersInput"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/userOrdersOutput"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-mizan": {
|
||||
"transport": "http",
|
||||
"isContext": "user"
|
||||
}
|
||||
}
|
||||
},
|
||||
"/mizan/update_profile": {
|
||||
"post": {
|
||||
"summary": "Mutation declaring affects on the user context.",
|
||||
"operationId": "updateProfile",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/updateProfileInput"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/updateProfileOutput"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-mizan": {
|
||||
"transport": "http",
|
||||
"isContext": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"/mizan/find_user": {
|
||||
"post": {
|
||||
"summary": "Optional return \u2014 exercises Pydantic `T | None` schema introspection.",
|
||||
"operationId": "findUser",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/findUserInput"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"anyOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/findUserOutput"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
],
|
||||
"title": "Response Finduser"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-mizan": {
|
||||
"transport": "http",
|
||||
"isContext": false
|
||||
}
|
||||
}
|
||||
},
|
||||
"/mizan/rename_user": {
|
||||
"post": {
|
||||
"summary": "Merge target \u2014 kernel splices return value into the user context.",
|
||||
"operationId": "renameUser",
|
||||
"requestBody": {
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/renameUserInput"
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/renameUserOutput"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"422": {
|
||||
"description": "Validation Error",
|
||||
"content": {
|
||||
"application/json": {
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/HTTPValidationError"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-mizan": {
|
||||
"transport": "http",
|
||||
"isContext": false
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"schemas": {
|
||||
"HTTPValidationError": {
|
||||
"properties": {
|
||||
"detail": {
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/ValidationError"
|
||||
},
|
||||
"type": "array",
|
||||
"title": "Detail"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"title": "HTTPValidationError"
|
||||
},
|
||||
"OrderOutput": {
|
||||
"properties": {
|
||||
"id": {
|
||||
"type": "integer",
|
||||
"title": "Id"
|
||||
},
|
||||
"user_id": {
|
||||
"type": "integer",
|
||||
"title": "User Id"
|
||||
},
|
||||
"total": {
|
||||
"type": "integer",
|
||||
"title": "Total"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"id",
|
||||
"user_id",
|
||||
"total"
|
||||
],
|
||||
"title": "OrderOutput"
|
||||
},
|
||||
"ValidationError": {
|
||||
"properties": {
|
||||
"loc": {
|
||||
"items": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"type": "integer"
|
||||
}
|
||||
]
|
||||
},
|
||||
"type": "array",
|
||||
"title": "Location"
|
||||
},
|
||||
"msg": {
|
||||
"type": "string",
|
||||
"title": "Message"
|
||||
},
|
||||
"type": {
|
||||
"type": "string",
|
||||
"title": "Error Type"
|
||||
},
|
||||
"input": {
|
||||
"title": "Input"
|
||||
},
|
||||
"ctx": {
|
||||
"type": "object",
|
||||
"title": "Context"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"loc",
|
||||
"msg",
|
||||
"type"
|
||||
],
|
||||
"title": "ValidationError"
|
||||
},
|
||||
"echoInput": {
|
||||
"properties": {
|
||||
"text": {
|
||||
"type": "string",
|
||||
"title": "Text"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"text"
|
||||
],
|
||||
"title": "echoInput"
|
||||
},
|
||||
"echoOutput": {
|
||||
"properties": {
|
||||
"message": {
|
||||
"type": "string",
|
||||
"title": "Message"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"message"
|
||||
],
|
||||
"title": "echoOutput"
|
||||
},
|
||||
"findUserInput": {
|
||||
"properties": {
|
||||
"user_id": {
|
||||
"type": "integer",
|
||||
"title": "User Id"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"user_id"
|
||||
],
|
||||
"title": "findUserInput"
|
||||
},
|
||||
"findUserOutput": {
|
||||
"properties": {
|
||||
"user_id": {
|
||||
"type": "integer",
|
||||
"title": "User Id"
|
||||
},
|
||||
"name": {
|
||||
"type": "string",
|
||||
"title": "Name"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"user_id",
|
||||
"name"
|
||||
],
|
||||
"title": "findUserOutput"
|
||||
},
|
||||
"renameUserInput": {
|
||||
"properties": {
|
||||
"user_id": {
|
||||
"type": "integer",
|
||||
"title": "User Id"
|
||||
},
|
||||
"name": {
|
||||
"type": "string",
|
||||
"title": "Name"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"user_id",
|
||||
"name"
|
||||
],
|
||||
"title": "renameUserInput"
|
||||
},
|
||||
"renameUserOutput": {
|
||||
"properties": {
|
||||
"user_id": {
|
||||
"type": "integer",
|
||||
"title": "User Id"
|
||||
},
|
||||
"name": {
|
||||
"type": "string",
|
||||
"title": "Name"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"user_id",
|
||||
"name"
|
||||
],
|
||||
"title": "renameUserOutput"
|
||||
},
|
||||
"updateProfileInput": {
|
||||
"properties": {
|
||||
"user_id": {
|
||||
"type": "integer",
|
||||
"title": "User Id"
|
||||
},
|
||||
"name": {
|
||||
"type": "string",
|
||||
"title": "Name"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"user_id",
|
||||
"name"
|
||||
],
|
||||
"title": "updateProfileInput"
|
||||
},
|
||||
"updateProfileOutput": {
|
||||
"properties": {
|
||||
"ok": {
|
||||
"type": "boolean",
|
||||
"title": "Ok"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"ok"
|
||||
],
|
||||
"title": "updateProfileOutput"
|
||||
},
|
||||
"userOrdersInput": {
|
||||
"properties": {
|
||||
"user_id": {
|
||||
"type": "integer",
|
||||
"title": "User Id"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"user_id"
|
||||
],
|
||||
"title": "userOrdersInput"
|
||||
},
|
||||
"userOrdersOutput": {
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/OrderOutput"
|
||||
},
|
||||
"type": "array",
|
||||
"title": "userOrdersOutput"
|
||||
},
|
||||
"userProfileInput": {
|
||||
"properties": {
|
||||
"user_id": {
|
||||
"type": "integer",
|
||||
"title": "User Id"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"user_id"
|
||||
],
|
||||
"title": "userProfileInput"
|
||||
},
|
||||
"userProfileOutput": {
|
||||
"properties": {
|
||||
"user_id": {
|
||||
"type": "integer",
|
||||
"title": "User Id"
|
||||
},
|
||||
"name": {
|
||||
"type": "string",
|
||||
"title": "Name"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"user_id",
|
||||
"name"
|
||||
],
|
||||
"title": "userProfileOutput"
|
||||
},
|
||||
"whoamiOutput": {
|
||||
"properties": {
|
||||
"email": {
|
||||
"type": "string",
|
||||
"title": "Email"
|
||||
},
|
||||
"authenticated": {
|
||||
"type": "boolean",
|
||||
"title": "Authenticated"
|
||||
}
|
||||
},
|
||||
"type": "object",
|
||||
"required": [
|
||||
"email",
|
||||
"authenticated"
|
||||
],
|
||||
"title": "whoamiOutput"
|
||||
}
|
||||
}
|
||||
},
|
||||
"x-mizan-functions": [
|
||||
{
|
||||
"name": "echo",
|
||||
"camelName": "echo",
|
||||
"hasInput": true,
|
||||
"inputType": "echoInput",
|
||||
"outputType": "echoOutput",
|
||||
"outputNullable": false,
|
||||
"transport": "http",
|
||||
"isContext": false,
|
||||
"isForm": false,
|
||||
"formName": null,
|
||||
"formRole": null
|
||||
},
|
||||
{
|
||||
"name": "whoami",
|
||||
"camelName": "whoami",
|
||||
"hasInput": false,
|
||||
"inputType": null,
|
||||
"outputType": "whoamiOutput",
|
||||
"outputNullable": false,
|
||||
"transport": "http",
|
||||
"isContext": false,
|
||||
"isForm": false,
|
||||
"formName": null,
|
||||
"formRole": null
|
||||
},
|
||||
{
|
||||
"name": "user_profile",
|
||||
"camelName": "userProfile",
|
||||
"hasInput": true,
|
||||
"inputType": "userProfileInput",
|
||||
"outputType": "userProfileOutput",
|
||||
"outputNullable": false,
|
||||
"transport": "http",
|
||||
"isContext": "user",
|
||||
"isForm": false,
|
||||
"formName": null,
|
||||
"formRole": null
|
||||
},
|
||||
{
|
||||
"name": "user_orders",
|
||||
"camelName": "userOrders",
|
||||
"hasInput": true,
|
||||
"inputType": "userOrdersInput",
|
||||
"outputType": "userOrdersOutput",
|
||||
"outputNullable": false,
|
||||
"transport": "http",
|
||||
"isContext": "user",
|
||||
"isForm": false,
|
||||
"formName": null,
|
||||
"formRole": null
|
||||
},
|
||||
{
|
||||
"name": "update_profile",
|
||||
"camelName": "updateProfile",
|
||||
"hasInput": true,
|
||||
"inputType": "updateProfileInput",
|
||||
"outputType": "updateProfileOutput",
|
||||
"outputNullable": false,
|
||||
"transport": "http",
|
||||
"isContext": false,
|
||||
"isForm": false,
|
||||
"formName": null,
|
||||
"formRole": null,
|
||||
"affects": [
|
||||
{
|
||||
"type": "context",
|
||||
"name": "user"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "find_user",
|
||||
"camelName": "findUser",
|
||||
"hasInput": true,
|
||||
"inputType": "findUserInput",
|
||||
"outputType": "findUserOutput",
|
||||
"outputNullable": true,
|
||||
"transport": "http",
|
||||
"isContext": false,
|
||||
"isForm": false,
|
||||
"formName": null,
|
||||
"formRole": null
|
||||
},
|
||||
{
|
||||
"name": "rename_user",
|
||||
"camelName": "renameUser",
|
||||
"hasInput": true,
|
||||
"inputType": "renameUserInput",
|
||||
"outputType": "renameUserOutput",
|
||||
"outputNullable": false,
|
||||
"transport": "http",
|
||||
"isContext": false,
|
||||
"isForm": false,
|
||||
"formName": null,
|
||||
"formRole": null,
|
||||
"merge": [
|
||||
"user"
|
||||
]
|
||||
}
|
||||
],
|
||||
"x-mizan-contexts": {
|
||||
"user": {
|
||||
"functions": [
|
||||
"user_profile",
|
||||
"user_orders"
|
||||
],
|
||||
"params": {
|
||||
"user_id": {
|
||||
"type": "integer",
|
||||
"sharedBy": [
|
||||
"user_profile",
|
||||
"user_orders"
|
||||
],
|
||||
"required": true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
55
protocol/mizan-codegen/tests/fixtures/channels_schema.json
vendored
Normal file
55
protocol/mizan-codegen/tests/fixtures/channels_schema.json
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
{
|
||||
"x-mizan-channels": [
|
||||
{
|
||||
"name": "chat",
|
||||
"pascalName": "Chat",
|
||||
"hasParams": true,
|
||||
"hasReactMessage": true,
|
||||
"hasDjangoMessage": true,
|
||||
"paramsType": "ChatChannelParams",
|
||||
"reactMessageType": "ChatReactMessage",
|
||||
"djangoMessageType": "ChatDjangoMessage"
|
||||
},
|
||||
{
|
||||
"name": "notifications",
|
||||
"pascalName": "Notifications",
|
||||
"hasParams": false,
|
||||
"hasReactMessage": false,
|
||||
"hasDjangoMessage": true,
|
||||
"djangoMessageType": "NotificationsDjangoMessage"
|
||||
}
|
||||
],
|
||||
"components": {
|
||||
"schemas": {
|
||||
"ChatChannelParams": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"room_id": { "type": "string" }
|
||||
},
|
||||
"required": ["room_id"]
|
||||
},
|
||||
"ChatReactMessage": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"text": { "type": "string" }
|
||||
},
|
||||
"required": ["text"]
|
||||
},
|
||||
"ChatDjangoMessage": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"text": { "type": "string" },
|
||||
"from_user": { "type": "string" }
|
||||
},
|
||||
"required": ["text", "from_user"]
|
||||
},
|
||||
"NotificationsDjangoMessage": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"body": { "type": "string" }
|
||||
},
|
||||
"required": ["body"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
4
protocol/mizan-codegen/tests/fixtures/js_python/__init__.py
vendored
Normal file
4
protocol/mizan-codegen/tests/fixtures/js_python/__init__.py
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
# AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
from .client import MizanClient # noqa: F401
|
||||
from .types import * # noqa: F401, F403
|
||||
67
protocol/mizan-codegen/tests/fixtures/js_python/client.py
vendored
Normal file
67
protocol/mizan-codegen/tests/fixtures/js_python/client.py
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
# AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
|
||||
# Built from frontends/mizan-rust with `maturin develop --features pyo3`.
|
||||
from mizan_rust import PyMizanClient, PyContextSubscription
|
||||
|
||||
from .types import * # noqa: F401, F403
|
||||
from .types import BaseModel # re-import for the synthesized ContextData classes
|
||||
|
||||
|
||||
class MizanClient:
|
||||
"""Typed Python facade over the PyO3 mizan-rust kernel."""
|
||||
|
||||
def __init__(self, base_url: str, *, session: bool = False,
|
||||
csrf_cookie_name: str = "csrftoken",
|
||||
csrf_header_name: str = "X-CSRFToken") -> None:
|
||||
self._inner = PyMizanClient(
|
||||
base_url,
|
||||
session=session,
|
||||
csrf_cookie_name=csrf_cookie_name,
|
||||
csrf_header_name=csrf_header_name,
|
||||
)
|
||||
|
||||
def fetch_user_context(self, user_id: int) -> "UserContextData":
|
||||
raw = self._inner.fetch_context("user", {"user_id": user_id})
|
||||
return UserContextData(**raw)
|
||||
def subscribe_user_context(self, user_id: int,
|
||||
callback: Callable[[dict[str, Any]], None]) -> PyContextSubscription:
|
||||
return self._inner.subscribe_context("user", {"user_id": user_id}, callback)
|
||||
|
||||
def call_echo(self, args: EchoInput) -> EchoOutput:
|
||||
raw = self._inner.call("echo", args.model_dump())
|
||||
return EchoOutput(**raw)
|
||||
|
||||
def call_whoami(self) -> WhoamiOutput:
|
||||
raw = self._inner.call("whoami", {})
|
||||
return WhoamiOutput(**raw)
|
||||
|
||||
def call_update_profile(self, args: UpdateProfileInput) -> UpdateProfileOutput:
|
||||
raw = self._inner.call("update_profile", args.model_dump())
|
||||
return UpdateProfileOutput(**raw)
|
||||
|
||||
def call_find_user(self, args: FindUserInput) -> FindUserOutput | None:
|
||||
raw = self._inner.call("find_user", args.model_dump())
|
||||
return FindUserOutput(**raw) if raw is not None else None
|
||||
|
||||
def call_rename_user(self, args: RenameUserInput) -> RenameUserOutput:
|
||||
raw = self._inner.call("rename_user", args.model_dump())
|
||||
return RenameUserOutput(**raw)
|
||||
|
||||
def invalidate(self, context: str) -> None:
|
||||
self._inner.invalidate(context)
|
||||
|
||||
def invalidate_scoped(self, context: str, params: dict[str, Any]) -> None:
|
||||
self._inner.invalidate_scoped(context, params)
|
||||
|
||||
|
||||
# ── Context data shapes (per-context bundle) ──────────────────────────────
|
||||
|
||||
class UserContextData(BaseModel):
|
||||
"""Bundled return of fetch_user_context."""
|
||||
user_profile: UserProfileOutput
|
||||
user_orders: UserOrdersOutput
|
||||
66
protocol/mizan-codegen/tests/fixtures/js_python/types.py
vendored
Normal file
66
protocol/mizan-codegen/tests/fixtures/js_python/types.py
vendored
Normal file
@@ -0,0 +1,66 @@
|
||||
# AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Literal
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
class HTTPValidationError(BaseModel):
|
||||
detail: list[ValidationError] | None = None
|
||||
|
||||
class OrderOutput(BaseModel):
|
||||
id: int
|
||||
user_id: int
|
||||
total: int
|
||||
|
||||
class ValidationError(BaseModel):
|
||||
loc: list[Any]
|
||||
msg: str
|
||||
r#type: str
|
||||
input: Any | None = None
|
||||
ctx: dict[str, Any] | None = None
|
||||
|
||||
class EchoInput(BaseModel):
|
||||
text: str
|
||||
|
||||
class EchoOutput(BaseModel):
|
||||
message: str
|
||||
|
||||
class FindUserInput(BaseModel):
|
||||
user_id: int
|
||||
|
||||
class FindUserOutput(BaseModel):
|
||||
user_id: int
|
||||
name: str
|
||||
|
||||
class RenameUserInput(BaseModel):
|
||||
user_id: int
|
||||
name: str
|
||||
|
||||
class RenameUserOutput(BaseModel):
|
||||
user_id: int
|
||||
name: str
|
||||
|
||||
class UpdateProfileInput(BaseModel):
|
||||
user_id: int
|
||||
name: str
|
||||
|
||||
class UpdateProfileOutput(BaseModel):
|
||||
ok: bool
|
||||
|
||||
class UserOrdersInput(BaseModel):
|
||||
user_id: int
|
||||
|
||||
UserOrdersOutput = list[OrderOutput]
|
||||
|
||||
class UserProfileInput(BaseModel):
|
||||
user_id: int
|
||||
|
||||
class UserProfileOutput(BaseModel):
|
||||
user_id: int
|
||||
name: str
|
||||
|
||||
class WhoamiOutput(BaseModel):
|
||||
email: str
|
||||
authenticated: bool
|
||||
157
protocol/mizan-codegen/tests/fixtures/js_react/react.tsx
vendored
Normal file
157
protocol/mizan-codegen/tests/fixtures/js_react/react.tsx
vendored
Normal file
@@ -0,0 +1,157 @@
|
||||
'use client'
|
||||
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
useSyncExternalStore,
|
||||
type ReactNode,
|
||||
} from 'react'
|
||||
import {
|
||||
configure,
|
||||
initSession,
|
||||
mizanCall,
|
||||
mizanFetch,
|
||||
MizanError,
|
||||
registerContext,
|
||||
type ContextState,
|
||||
} from '@mizan/base'
|
||||
|
||||
import { fetchUserContext, type UserContextData, type UserContextParams, callUpdateProfile, callEcho, callWhoami, callFindUser, callRenameUser, type userProfileOutput, type userOrdersOutput } from './index'
|
||||
|
||||
// Internal — runs inside a Provider, registers with the kernel exactly once.
|
||||
function useContextSubscription<T>(
|
||||
name: string,
|
||||
params: Record<string, any>,
|
||||
fetchFn: () => Promise<T>,
|
||||
initialData?: T,
|
||||
): ContextState<T> {
|
||||
const ref = useRef<ReturnType<typeof registerContext> | null>(null)
|
||||
if (!ref.current) {
|
||||
ref.current = registerContext(name, params, fetchFn, initialData)
|
||||
}
|
||||
const handle = ref.current
|
||||
|
||||
useEffect(() => {
|
||||
if (handle.getState().status === 'idle') handle.refetch()
|
||||
return () => handle.unregister()
|
||||
}, [handle])
|
||||
|
||||
return useSyncExternalStore(handle.subscribe, handle.getState, handle.getState)
|
||||
}
|
||||
|
||||
// Internal — wraps an imperative call() with isPending / error state.
|
||||
interface MutationHook<TArgs, TResult> {
|
||||
mutate: (args: TArgs) => Promise<TResult>
|
||||
isPending: boolean
|
||||
error: Error | null
|
||||
}
|
||||
|
||||
function useMutation<TArgs, TResult>(
|
||||
callFn: (args: TArgs) => Promise<TResult>,
|
||||
): MutationHook<TArgs, TResult> {
|
||||
const [isPending, setIsPending] = useState(false)
|
||||
const [error, setError] = useState<Error | null>(null)
|
||||
|
||||
const mutate = useCallback(async (args: TArgs) => {
|
||||
setIsPending(true)
|
||||
setError(null)
|
||||
try {
|
||||
return await callFn(args)
|
||||
} catch (e) {
|
||||
setError(e as Error)
|
||||
throw e
|
||||
} finally {
|
||||
setIsPending(false)
|
||||
}
|
||||
}, [callFn])
|
||||
|
||||
return { mutate, isPending, error }
|
||||
}
|
||||
|
||||
// ── User Context ──
|
||||
|
||||
const UserCtx = createContext<ContextState<UserContextData> | null>(null)
|
||||
|
||||
export function UserContext({ children, ...params }: UserContextParams & { children: ReactNode }) {
|
||||
const state = useContextSubscription('user', params, () => fetchUserContext(params))
|
||||
return <UserCtx.Provider value={state}>{children}</UserCtx.Provider>
|
||||
}
|
||||
|
||||
export function useUserContext(): ContextState<UserContextData> {
|
||||
const ctx = useContext(UserCtx)
|
||||
if (!ctx) throw new Error('useUserContext requires <UserContext>')
|
||||
return ctx
|
||||
}
|
||||
|
||||
export function useUserProfile(): userProfileOutput | null {
|
||||
return useUserContext().data?.user_profile ?? null
|
||||
}
|
||||
|
||||
export function useUserOrders(): userOrdersOutput | null {
|
||||
return useUserContext().data?.user_orders ?? null
|
||||
}
|
||||
|
||||
export function useUpdateProfile() {
|
||||
return useMutation<Parameters<typeof callUpdateProfile>[0], Awaited<ReturnType<typeof callUpdateProfile>>>(callUpdateProfile)
|
||||
}
|
||||
|
||||
export function useEcho() {
|
||||
return useMutation<Parameters<typeof callEcho>[0], Awaited<ReturnType<typeof callEcho>>>(callEcho)
|
||||
}
|
||||
|
||||
export function useWhoami() {
|
||||
return useMutation<void, Awaited<ReturnType<typeof callWhoami>>>(() => callWhoami() as any)
|
||||
}
|
||||
|
||||
export function useFindUser() {
|
||||
return useMutation<Parameters<typeof callFindUser>[0], Awaited<ReturnType<typeof callFindUser>>>(callFindUser)
|
||||
}
|
||||
|
||||
export function useRenameUser() {
|
||||
return useMutation<Parameters<typeof callRenameUser>[0], Awaited<ReturnType<typeof callRenameUser>>>(callRenameUser)
|
||||
}
|
||||
|
||||
// ── MizanContext root provider ──
|
||||
|
||||
export interface MizanContextProps {
|
||||
/** Base URL for protocol endpoints. Defaults to "/api/mizan". */
|
||||
baseUrl?: string
|
||||
/** Set to `false` for backends without a `/session/` endpoint (e.g. FastAPI). */
|
||||
session?: boolean
|
||||
children: ReactNode
|
||||
}
|
||||
|
||||
/**
|
||||
* Root provider — calls configure() once and mounts the global context (if defined).
|
||||
* Must wrap any component using Mizan-generated hooks.
|
||||
*/
|
||||
export function MizanContext({ baseUrl, session, children }: MizanContextProps) {
|
||||
const configured = useRef(false)
|
||||
if (!configured.current) {
|
||||
const opts: Parameters<typeof configure>[0] = {}
|
||||
if (baseUrl !== undefined) opts.baseUrl = baseUrl
|
||||
if (session !== undefined) opts.session = session
|
||||
if (Object.keys(opts).length > 0) configure(opts)
|
||||
configured.current = true
|
||||
}
|
||||
return <>{children}</>
|
||||
}
|
||||
|
||||
// ── Imperative escape hatch ──
|
||||
|
||||
/**
|
||||
* Returns the imperative kernel API. For test harnesses or rare cases where
|
||||
* a typed generated hook does not fit. Most app code should use the typed hooks.
|
||||
*/
|
||||
export function useMizan() {
|
||||
return { call: mizanCall, fetch: mizanFetch }
|
||||
}
|
||||
|
||||
export type { ContextState } from '@mizan/base'
|
||||
export { configure, initSession, MizanError } from '@mizan/base'
|
||||
10
protocol/mizan-codegen/tests/fixtures/js_rust/Cargo.toml
vendored
Normal file
10
protocol/mizan-codegen/tests/fixtures/js_rust/Cargo.toml
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "fixture_client"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
mizan-rust = { path = "../../../frontends/mizan-rust" }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
tokio = { version = "1", features = ["rt", "macros"] }
|
||||
3
protocol/mizan-codegen/tests/fixtures/js_rust/src/contexts/mod.rs
vendored
Normal file
3
protocol/mizan-codegen/tests/fixtures/js_rust/src/contexts/mod.rs
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
pub mod user;
|
||||
29
protocol/mizan-codegen/tests/fixtures/js_rust/src/contexts/user.rs
vendored
Normal file
29
protocol/mizan-codegen/tests/fixtures/js_rust/src/contexts/user.rs
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
use mizan_rust::{MizanClient, MizanError};
|
||||
|
||||
use crate::types::{UserProfileOutput, UserOrdersOutput};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UserContextData {
|
||||
pub user_profile: UserProfileOutput,
|
||||
pub user_orders: UserOrdersOutput,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UserContextParams {
|
||||
pub user_id: i64,
|
||||
}
|
||||
|
||||
pub async fn fetch_user_context(
|
||||
client: &MizanClient,
|
||||
params: &UserContextParams,
|
||||
) -> Result<UserContextData, MizanError> {
|
||||
let params_value = serde_json::to_value(params).unwrap_or(Value::Object(Default::default()));
|
||||
let raw = client.fetch_context("user", ¶ms_value).await?;
|
||||
serde_json::from_value(raw)
|
||||
.map_err(|e| MizanError::transport(format!("decode user context: {e}")))
|
||||
}
|
||||
14
protocol/mizan-codegen/tests/fixtures/js_rust/src/functions/echo.rs
vendored
Normal file
14
protocol/mizan-codegen/tests/fixtures/js_rust/src/functions/echo.rs
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use mizan_rust::{MizanClient, MizanError};
|
||||
|
||||
use crate::types::{EchoOutput, EchoInput};
|
||||
|
||||
pub async fn call_echo(client: &MizanClient, args: &EchoInput) -> Result<EchoOutput, MizanError> {
|
||||
let args_value = serde_json::to_value(args).unwrap_or(Value::Object(Default::default()));
|
||||
let raw = client.call("echo", args_value).await?;
|
||||
serde_json::from_value(raw)
|
||||
.map_err(|e| MizanError::transport(format!("decode echo result: {e}")))
|
||||
}
|
||||
14
protocol/mizan-codegen/tests/fixtures/js_rust/src/functions/find_user.rs
vendored
Normal file
14
protocol/mizan-codegen/tests/fixtures/js_rust/src/functions/find_user.rs
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use mizan_rust::{MizanClient, MizanError};
|
||||
|
||||
use crate::types::{FindUserOutput, FindUserInput};
|
||||
|
||||
pub async fn call_find_user(client: &MizanClient, args: &FindUserInput) -> Result<Option<FindUserOutput>, MizanError> {
|
||||
let args_value = serde_json::to_value(args).unwrap_or(Value::Object(Default::default()));
|
||||
let raw = client.call("find_user", args_value).await?;
|
||||
serde_json::from_value(raw)
|
||||
.map_err(|e| MizanError::transport(format!("decode find_user result: {e}")))
|
||||
}
|
||||
6
protocol/mizan-codegen/tests/fixtures/js_rust/src/functions/mod.rs
vendored
Normal file
6
protocol/mizan-codegen/tests/fixtures/js_rust/src/functions/mod.rs
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
pub mod echo;
|
||||
pub mod find_user;
|
||||
pub mod rename_user;
|
||||
pub mod whoami;
|
||||
14
protocol/mizan-codegen/tests/fixtures/js_rust/src/functions/rename_user.rs
vendored
Normal file
14
protocol/mizan-codegen/tests/fixtures/js_rust/src/functions/rename_user.rs
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use mizan_rust::{MizanClient, MizanError};
|
||||
|
||||
use crate::types::{RenameUserOutput, RenameUserInput};
|
||||
|
||||
pub async fn call_rename_user(client: &MizanClient, args: &RenameUserInput) -> Result<RenameUserOutput, MizanError> {
|
||||
let args_value = serde_json::to_value(args).unwrap_or(Value::Object(Default::default()));
|
||||
let raw = client.call("rename_user", args_value).await?;
|
||||
serde_json::from_value(raw)
|
||||
.map_err(|e| MizanError::transport(format!("decode rename_user result: {e}")))
|
||||
}
|
||||
14
protocol/mizan-codegen/tests/fixtures/js_rust/src/functions/whoami.rs
vendored
Normal file
14
protocol/mizan-codegen/tests/fixtures/js_rust/src/functions/whoami.rs
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use mizan_rust::{MizanClient, MizanError};
|
||||
|
||||
use crate::types::{WhoamiOutput};
|
||||
|
||||
pub async fn call_whoami(client: &MizanClient) -> Result<WhoamiOutput, MizanError> {
|
||||
let args_value = Value::Object(Default::default());
|
||||
let raw = client.call("whoami", args_value).await?;
|
||||
serde_json::from_value(raw)
|
||||
.map_err(|e| MizanError::transport(format!("decode whoami result: {e}")))
|
||||
}
|
||||
8
protocol/mizan-codegen/tests/fixtures/js_rust/src/lib.rs
vendored
Normal file
8
protocol/mizan-codegen/tests/fixtures/js_rust/src/lib.rs
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
pub mod types;
|
||||
pub mod contexts;
|
||||
pub mod mutations;
|
||||
pub mod functions;
|
||||
|
||||
pub use mizan_rust::{MizanClient, MizanConfig, MizanError};
|
||||
3
protocol/mizan-codegen/tests/fixtures/js_rust/src/mutations/mod.rs
vendored
Normal file
3
protocol/mizan-codegen/tests/fixtures/js_rust/src/mutations/mod.rs
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
pub mod update_profile;
|
||||
14
protocol/mizan-codegen/tests/fixtures/js_rust/src/mutations/update_profile.rs
vendored
Normal file
14
protocol/mizan-codegen/tests/fixtures/js_rust/src/mutations/update_profile.rs
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use mizan_rust::{MizanClient, MizanError};
|
||||
|
||||
use crate::types::{UpdateProfileOutput, UpdateProfileInput};
|
||||
|
||||
pub async fn call_update_profile(client: &MizanClient, args: &UpdateProfileInput) -> Result<UpdateProfileOutput, MizanError> {
|
||||
let args_value = serde_json::to_value(args).unwrap_or(Value::Object(Default::default()));
|
||||
let raw = client.call("update_profile", args_value).await?;
|
||||
serde_json::from_value(raw)
|
||||
.map_err(|e| MizanError::transport(format!("decode update_profile result: {e}")))
|
||||
}
|
||||
98
protocol/mizan-codegen/tests/fixtures/js_rust/src/types.rs
vendored
Normal file
98
protocol/mizan-codegen/tests/fixtures/js_rust/src/types.rs
vendored
Normal file
@@ -0,0 +1,98 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
#![allow(non_camel_case_types)]
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct HTTPValidationError {
|
||||
pub detail: Option<Vec<ValidationError>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OrderOutput {
|
||||
pub id: i64,
|
||||
pub user_id: i64,
|
||||
pub total: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct ValidationError {
|
||||
pub loc: Vec<serde_json::Value>,
|
||||
pub msg: String,
|
||||
#[serde(rename = "type")]
|
||||
pub r#type: String,
|
||||
pub input: Option<serde_json::Value>,
|
||||
pub ctx: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct EchoInput {
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct EchoOutput {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FindUserInput {
|
||||
pub user_id: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FindUserOutput {
|
||||
pub user_id: i64,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RenameUserInput {
|
||||
pub user_id: i64,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct RenameUserOutput {
|
||||
pub user_id: i64,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UpdateProfileInput {
|
||||
pub user_id: i64,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UpdateProfileOutput {
|
||||
pub ok: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UserOrdersInput {
|
||||
pub user_id: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(transparent)]
|
||||
pub struct UserOrdersOutput(pub Vec<OrderOutput>);
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UserProfileInput {
|
||||
pub user_id: i64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct UserProfileOutput {
|
||||
pub user_id: i64,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct WhoamiOutput {
|
||||
pub email: String,
|
||||
pub authenticated: bool,
|
||||
}
|
||||
|
||||
18
protocol/mizan-codegen/tests/fixtures/js_stage1/contexts/user.ts
vendored
Normal file
18
protocol/mizan-codegen/tests/fixtures/js_stage1/contexts/user.ts
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanFetch } from '@mizan/base'
|
||||
|
||||
import type { userProfileOutput, userOrdersOutput } from '../types'
|
||||
|
||||
export interface UserContextData {
|
||||
user_profile: userProfileOutput
|
||||
user_orders: userOrdersOutput
|
||||
}
|
||||
|
||||
export interface UserContextParams {
|
||||
user_id: number
|
||||
}
|
||||
|
||||
export function fetchUserContext(params: UserContextParams): Promise<UserContextData> {
|
||||
return mizanFetch('user', params)
|
||||
}
|
||||
9
protocol/mizan-codegen/tests/fixtures/js_stage1/functions/echo.ts
vendored
Normal file
9
protocol/mizan-codegen/tests/fixtures/js_stage1/functions/echo.ts
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/base'
|
||||
|
||||
import type { echoInput, echoOutput } from '../types'
|
||||
|
||||
export function callEcho(args: echoInput): Promise<echoOutput> {
|
||||
return mizanCall('echo', args)
|
||||
}
|
||||
9
protocol/mizan-codegen/tests/fixtures/js_stage1/functions/findUser.ts
vendored
Normal file
9
protocol/mizan-codegen/tests/fixtures/js_stage1/functions/findUser.ts
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/base'
|
||||
|
||||
import type { findUserInput, findUserOutput } from '../types'
|
||||
|
||||
export function callFindUser(args: findUserInput): Promise<findUserOutput> {
|
||||
return mizanCall('find_user', args)
|
||||
}
|
||||
9
protocol/mizan-codegen/tests/fixtures/js_stage1/functions/renameUser.ts
vendored
Normal file
9
protocol/mizan-codegen/tests/fixtures/js_stage1/functions/renameUser.ts
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/base'
|
||||
|
||||
import type { renameUserInput, renameUserOutput } from '../types'
|
||||
|
||||
export function callRenameUser(args: renameUserInput): Promise<renameUserOutput> {
|
||||
return mizanCall('rename_user', args)
|
||||
}
|
||||
9
protocol/mizan-codegen/tests/fixtures/js_stage1/functions/whoami.ts
vendored
Normal file
9
protocol/mizan-codegen/tests/fixtures/js_stage1/functions/whoami.ts
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/base'
|
||||
|
||||
import type { whoamiOutput } from '../types'
|
||||
|
||||
export function callWhoami(): Promise<whoamiOutput> {
|
||||
return mizanCall('whoami', {})
|
||||
}
|
||||
11
protocol/mizan-codegen/tests/fixtures/js_stage1/index.ts
vendored
Normal file
11
protocol/mizan-codegen/tests/fixtures/js_stage1/index.ts
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
export * from './types'
|
||||
|
||||
export { fetchUserContext, type UserContextData, type UserContextParams } from './contexts/user'
|
||||
|
||||
export { callEcho } from './functions/echo'
|
||||
export { callWhoami } from './functions/whoami'
|
||||
export { callUpdateProfile } from './mutations/updateProfile'
|
||||
export { callFindUser } from './functions/findUser'
|
||||
export { callRenameUser } from './functions/renameUser'
|
||||
9
protocol/mizan-codegen/tests/fixtures/js_stage1/mutations/updateProfile.ts
vendored
Normal file
9
protocol/mizan-codegen/tests/fixtures/js_stage1/mutations/updateProfile.ts
vendored
Normal file
@@ -0,0 +1,9 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { mizanCall } from '@mizan/base'
|
||||
|
||||
import type { updateProfileInput, updateProfileOutput } from '../types'
|
||||
|
||||
export function callUpdateProfile(args: updateProfileInput): Promise<updateProfileOutput> {
|
||||
return mizanCall('update_profile', args)
|
||||
}
|
||||
29
protocol/mizan-codegen/tests/fixtures/js_svelte/svelte.ts
vendored
Normal file
29
protocol/mizan-codegen/tests/fixtures/js_svelte/svelte.ts
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { readable, type Readable } from 'svelte/store'
|
||||
import { registerContext, type ContextState } from '@mizan/base'
|
||||
|
||||
import { fetchUserContext, type UserContextData, type UserContextParams, callUpdateProfile, callEcho, callWhoami, callFindUser, callRenameUser } from '../index'
|
||||
|
||||
export function createUserContext(params: UserContextParams) {
|
||||
const store = readable<ContextState<UserContextData>>(
|
||||
{ data: null, status: 'idle', error: null },
|
||||
(set) => {
|
||||
const handle = registerContext('user', params, () => fetchUserContext(params))
|
||||
const unsub = handle.subscribe(() => set(handle.getState()))
|
||||
handle.refetch()
|
||||
return () => { unsub(); handle.unregister() }
|
||||
},
|
||||
)
|
||||
|
||||
return store
|
||||
}
|
||||
|
||||
export { callUpdateProfile } from '../index'
|
||||
export { callEcho } from '../index'
|
||||
export { callWhoami } from '../index'
|
||||
export { callFindUser } from '../index'
|
||||
export { callRenameUser } from '../index'
|
||||
|
||||
export type { ContextState } from '@mizan/base'
|
||||
export { configure, initSession, MizanError } from '@mizan/base'
|
||||
96
protocol/mizan-codegen/tests/fixtures/js_vue/vue.ts
vendored
Normal file
96
protocol/mizan-codegen/tests/fixtures/js_vue/vue.ts
vendored
Normal file
@@ -0,0 +1,96 @@
|
||||
// AUTO-GENERATED by mizan — do not edit
|
||||
|
||||
import { ref, computed, onMounted, onUnmounted, onServerPrefetch, type ComputedRef } from 'vue'
|
||||
import { registerContext, type ContextState } from '@mizan/base'
|
||||
|
||||
import { fetchUserContext, type UserContextData, type UserContextParams, callUpdateProfile, callEcho, callWhoami, callFindUser, callRenameUser } from '../index'
|
||||
|
||||
export function useUserContext(params: UserContextParams) {
|
||||
const state = ref<ContextState<UserContextData>>({ data: null, status: 'idle', error: null })
|
||||
let handle: ReturnType<typeof registerContext> | null = null
|
||||
|
||||
onMounted(() => {
|
||||
handle = registerContext('user', params, () => fetchUserContext(params))
|
||||
handle.subscribe(() => { state.value = handle!.getState() })
|
||||
handle.refetch()
|
||||
})
|
||||
|
||||
onServerPrefetch(async () => {
|
||||
handle = registerContext('user', params, () => fetchUserContext(params))
|
||||
await handle.refetch()
|
||||
state.value = handle.getState()
|
||||
})
|
||||
|
||||
onUnmounted(() => { handle?.unregister() })
|
||||
|
||||
return {
|
||||
state,
|
||||
userProfile: computed(() => state.value.data?.user_profile ?? null) as ComputedRef<userProfileOutput | null>,
|
||||
userOrders: computed(() => state.value.data?.user_orders ?? null) as ComputedRef<userOrdersOutput | null>,
|
||||
loading: computed(() => state.value.status === 'loading'),
|
||||
error: computed(() => state.value.error),
|
||||
}
|
||||
}
|
||||
|
||||
export function useUpdateProfile() {
|
||||
const isPending = ref(false)
|
||||
const error = ref<Error | null>(null)
|
||||
async function mutate(args: Parameters<typeof callUpdateProfile>[0]) {
|
||||
isPending.value = true; error.value = null
|
||||
try { return await callUpdateProfile(args) }
|
||||
catch (e) { error.value = e as Error; throw e }
|
||||
finally { isPending.value = false }
|
||||
}
|
||||
return { mutate, isPending, error }
|
||||
}
|
||||
|
||||
export function useEcho() {
|
||||
const isPending = ref(false)
|
||||
const error = ref<Error | null>(null)
|
||||
async function mutate(args: Parameters<typeof callEcho>[0]) {
|
||||
isPending.value = true; error.value = null
|
||||
try { return await callEcho(args) }
|
||||
catch (e) { error.value = e as Error; throw e }
|
||||
finally { isPending.value = false }
|
||||
}
|
||||
return { mutate, isPending, error }
|
||||
}
|
||||
|
||||
export function useWhoami() {
|
||||
const isPending = ref(false)
|
||||
const error = ref<Error | null>(null)
|
||||
async function mutate() {
|
||||
isPending.value = true; error.value = null
|
||||
try { return await callWhoami() }
|
||||
catch (e) { error.value = e as Error; throw e }
|
||||
finally { isPending.value = false }
|
||||
}
|
||||
return { mutate, isPending, error }
|
||||
}
|
||||
|
||||
export function useFindUser() {
|
||||
const isPending = ref(false)
|
||||
const error = ref<Error | null>(null)
|
||||
async function mutate(args: Parameters<typeof callFindUser>[0]) {
|
||||
isPending.value = true; error.value = null
|
||||
try { return await callFindUser(args) }
|
||||
catch (e) { error.value = e as Error; throw e }
|
||||
finally { isPending.value = false }
|
||||
}
|
||||
return { mutate, isPending, error }
|
||||
}
|
||||
|
||||
export function useRenameUser() {
|
||||
const isPending = ref(false)
|
||||
const error = ref<Error | null>(null)
|
||||
async function mutate(args: Parameters<typeof callRenameUser>[0]) {
|
||||
isPending.value = true; error.value = null
|
||||
try { return await callRenameUser(args) }
|
||||
catch (e) { error.value = e as Error; throw e }
|
||||
finally { isPending.value = false }
|
||||
}
|
||||
return { mutate, isPending, error }
|
||||
}
|
||||
|
||||
export type { ContextState } from '@mizan/base'
|
||||
export { configure, initSession, MizanError } from '@mizan/base'
|
||||
103
protocol/mizan-codegen/tests/ir_deserialization.rs
Normal file
103
protocol/mizan-codegen/tests/ir_deserialization.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
//! IR deserialization tests against the AFI fixture schema.
|
||||
//!
|
||||
//! The fixture is captured from the FastAPI backend's `build_schema()`
|
||||
//! against `tests/afi/fixture.py`. Each test exercises a different facet
|
||||
//! of the IR — function set, per-function field decoding, context-param
|
||||
//! elevation, and components.schemas presence — to confirm the typed
|
||||
//! Rust structs match the JSON shape the backends emit.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use mizan_codegen::fetch::parse_ir_from_str;
|
||||
use mizan_codegen::ir::{AffectKind, IsContext, Transport};
|
||||
|
||||
|
||||
fn load_fixture() -> mizan_codegen::ir::MizanIR {
|
||||
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/fixtures/afi_schema.json");
|
||||
let raw = std::fs::read_to_string(&path)
|
||||
.unwrap_or_else(|e| panic!("read {}: {e}", path.display()));
|
||||
parse_ir_from_str(&raw).unwrap_or_else(|e| panic!("parse IR: {e}"))
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn afi_fixture_deserializes_function_set() {
|
||||
let ir = load_fixture();
|
||||
let names: Vec<&str> = ir.functions.iter().map(|f| f.name.as_str()).collect();
|
||||
|
||||
// Seven fixture functions per tests/afi/fixture.py.
|
||||
assert_eq!(ir.functions.len(), 7, "expected 7 functions, got {}: {names:?}", ir.functions.len());
|
||||
|
||||
for expected in [
|
||||
"echo", "whoami",
|
||||
"user_profile", "user_orders",
|
||||
"update_profile", "find_user", "rename_user",
|
||||
] {
|
||||
assert!(names.contains(&expected), "missing function {expected:?} in {names:?}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn afi_fixture_function_field_decode() {
|
||||
let ir = load_fixture();
|
||||
let echo = ir.functions.iter().find(|f| f.name == "echo").unwrap();
|
||||
assert_eq!(echo.camel_name, "echo");
|
||||
assert!(echo.has_input);
|
||||
assert_eq!(echo.input_type.as_deref(), Some("echoInput"));
|
||||
assert_eq!(echo.output_type, "echoOutput");
|
||||
assert!(!echo.output_nullable);
|
||||
assert_eq!(echo.transport, Transport::Http);
|
||||
assert_eq!(echo.is_context, IsContext::No);
|
||||
|
||||
let whoami = ir.functions.iter().find(|f| f.name == "whoami").unwrap();
|
||||
assert!(!whoami.has_input);
|
||||
|
||||
// `find_user` returns `ProfileOutput | None` — outputNullable must be true.
|
||||
let find_user = ir.functions.iter().find(|f| f.name == "find_user").unwrap();
|
||||
assert!(find_user.output_nullable, "find_user must be outputNullable");
|
||||
|
||||
// Context-typed function picks up the context name.
|
||||
let user_profile = ir.functions.iter().find(|f| f.name == "user_profile").unwrap();
|
||||
assert_eq!(user_profile.is_context.as_str(), Some("user"));
|
||||
|
||||
// Mutation with `affects="user"` lands in `affects` as a context target.
|
||||
let update_profile = ir.functions.iter().find(|f| f.name == "update_profile").unwrap();
|
||||
assert_eq!(update_profile.affects.len(), 1);
|
||||
assert_eq!(update_profile.affects[0].kind, AffectKind::Context);
|
||||
assert_eq!(update_profile.affects[0].name, "user");
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn afi_fixture_context_param_elevation() {
|
||||
let ir = load_fixture();
|
||||
let user = ir.contexts.get("user").expect("user context group");
|
||||
|
||||
// Both context functions share `user_id` as a required param.
|
||||
let user_id = user.params.get("user_id").expect("user_id param");
|
||||
assert_eq!(user_id.ty, "integer");
|
||||
assert!(user_id.required, "user_id is required (declared by every fn in the group)");
|
||||
assert!(user_id.shared_by.contains(&"user_profile".to_string()));
|
||||
assert!(user_id.shared_by.contains(&"user_orders".to_string()));
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn afi_fixture_components_schemas_present() {
|
||||
let ir = load_fixture();
|
||||
// Each fixture function pairs with an *Input/Output schema in components.
|
||||
for expected in [
|
||||
"echoInput", "echoOutput",
|
||||
"whoamiOutput",
|
||||
"userProfileInput", "userProfileOutput",
|
||||
"updateProfileInput", "updateProfileOutput",
|
||||
"findUserInput", "findUserOutput",
|
||||
] {
|
||||
assert!(
|
||||
ir.components.schemas.contains_key(expected),
|
||||
"missing schema {expected:?}",
|
||||
);
|
||||
}
|
||||
}
|
||||
75
protocol/mizan-codegen/tests/python_parity.rs
Normal file
75
protocol/mizan-codegen/tests/python_parity.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
//! Byte-equivalence test for the Python target against the JS baseline.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use mizan_codegen::config::{Config, SourceConfig};
|
||||
use mizan_codegen::emit::{CodegenTarget, EmittedFile};
|
||||
use mizan_codegen::emit::python::PythonClient;
|
||||
use mizan_codegen::fetch::parse_ir_from_str;
|
||||
|
||||
|
||||
fn load_ir() -> mizan_codegen::ir::MizanIR {
|
||||
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/afi_schema.json");
|
||||
parse_ir_from_str(&std::fs::read_to_string(&path).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
|
||||
fn fixture_config() -> Config {
|
||||
Config {
|
||||
project_id: None,
|
||||
output: PathBuf::from("/tmp"),
|
||||
targets: vec!["python".to_string()],
|
||||
source: SourceConfig { fastapi: None, django: None },
|
||||
rust_kernel: None,
|
||||
rust_crate_name: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn read_baseline(rel: &str) -> String {
|
||||
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/fixtures/js_python")
|
||||
.join(rel);
|
||||
std::fs::read_to_string(&path)
|
||||
.unwrap_or_else(|e| panic!("read baseline {}: {e}", path.display()))
|
||||
}
|
||||
|
||||
|
||||
fn emit_index(files: &[EmittedFile]) -> BTreeMap<PathBuf, &str> {
|
||||
files.iter().map(|f| (f.rel_path.clone(), f.content.as_str())).collect()
|
||||
}
|
||||
|
||||
|
||||
fn assert_byte_equal(rel: &str, files: &BTreeMap<PathBuf, &str>) {
|
||||
let actual = files
|
||||
.get(&PathBuf::from(rel))
|
||||
.unwrap_or_else(|| panic!("Python target did not produce {rel}"));
|
||||
let expected = read_baseline(rel);
|
||||
if *actual != expected {
|
||||
for (lineno, (a, b)) in actual.lines().zip(expected.lines()).enumerate() {
|
||||
if a != b {
|
||||
panic!(
|
||||
"{rel} diverges at line {}:\n expected: {b:?}\n actual: {a:?}",
|
||||
lineno + 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
panic!(
|
||||
"{rel} diverges in length: actual={} expected={}",
|
||||
actual.len(), expected.len(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn python_target_all_files_match_baseline() {
|
||||
let ir = load_ir();
|
||||
let files = PythonClient.emit(&ir, &fixture_config());
|
||||
let index = emit_index(&files);
|
||||
|
||||
for rel in ["types.py", "client.py", "__init__.py"] {
|
||||
assert_byte_equal(rel, &index);
|
||||
}
|
||||
}
|
||||
54
protocol/mizan-codegen/tests/react_parity.rs
Normal file
54
protocol/mizan-codegen/tests/react_parity.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
//! Byte-equivalence test for the React target against the JS baseline.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use mizan_codegen::config::{Config, SourceConfig};
|
||||
use mizan_codegen::emit::CodegenTarget;
|
||||
use mizan_codegen::emit::react::ReactAdapter;
|
||||
use mizan_codegen::fetch::parse_ir_from_str;
|
||||
|
||||
|
||||
fn load_ir() -> mizan_codegen::ir::MizanIR {
|
||||
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/afi_schema.json");
|
||||
parse_ir_from_str(&std::fs::read_to_string(&path).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
|
||||
fn fixture_config() -> Config {
|
||||
Config {
|
||||
project_id: None,
|
||||
output: PathBuf::from("/tmp"),
|
||||
targets: vec!["react".to_string()],
|
||||
source: SourceConfig { fastapi: None, django: None },
|
||||
rust_kernel: None,
|
||||
rust_crate_name: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn react_target_byte_match() {
|
||||
let ir = load_ir();
|
||||
let files = ReactAdapter.emit(&ir, &fixture_config());
|
||||
assert_eq!(files.len(), 1);
|
||||
let actual = &files[0].content;
|
||||
|
||||
let expected_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/fixtures/js_react/react.tsx");
|
||||
let expected = std::fs::read_to_string(&expected_path).unwrap();
|
||||
|
||||
if *actual != expected {
|
||||
for (lineno, (a, b)) in actual.lines().zip(expected.lines()).enumerate() {
|
||||
if a != b {
|
||||
panic!(
|
||||
"react.tsx diverges at line {}:\n expected: {b:?}\n actual: {a:?}",
|
||||
lineno + 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
panic!(
|
||||
"react.tsx diverges in length: actual={} expected={}",
|
||||
actual.len(), expected.len(),
|
||||
);
|
||||
}
|
||||
}
|
||||
96
protocol/mizan-codegen/tests/rust_parity.rs
Normal file
96
protocol/mizan-codegen/tests/rust_parity.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
//! Byte-equivalence between the Rust target and the JS rust.mjs baseline
|
||||
//! against the AFI fixture. The downstream forcing function is the wire-
|
||||
//! parity drivers under `tests/rust/`; this test catches divergence
|
||||
//! earlier in the cycle without needing to spin a FastAPI fixture up.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use mizan_codegen::config::{Config, RustKernelSpec, SourceConfig};
|
||||
use mizan_codegen::emit::{CodegenTarget, EmittedFile};
|
||||
use mizan_codegen::emit::rust::RustCrate;
|
||||
use mizan_codegen::fetch::parse_ir_from_str;
|
||||
|
||||
|
||||
fn load_ir() -> mizan_codegen::ir::MizanIR {
|
||||
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/fixtures/afi_schema.json");
|
||||
parse_ir_from_str(&std::fs::read_to_string(&path).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
|
||||
fn fixture_config() -> Config {
|
||||
Config {
|
||||
project_id: None,
|
||||
output: PathBuf::from("/tmp"),
|
||||
targets: vec!["rust".to_string()],
|
||||
source: SourceConfig { fastapi: None, django: None },
|
||||
rust_kernel: Some(RustKernelSpec::Path {
|
||||
path: "../../../frontends/mizan-rust".to_string(),
|
||||
}),
|
||||
rust_crate_name: Some("fixture_client".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn read_baseline(rel: &str) -> String {
|
||||
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/fixtures/js_rust")
|
||||
.join(rel);
|
||||
std::fs::read_to_string(&path)
|
||||
.unwrap_or_else(|e| panic!("read baseline {}: {e}", path.display()))
|
||||
}
|
||||
|
||||
|
||||
fn emit_index(files: &[EmittedFile]) -> BTreeMap<PathBuf, &str> {
|
||||
files.iter().map(|f| (f.rel_path.clone(), f.content.as_str())).collect()
|
||||
}
|
||||
|
||||
|
||||
fn assert_byte_equal(rel: &str, files: &BTreeMap<PathBuf, &str>) {
|
||||
let actual = files
|
||||
.get(&PathBuf::from(rel))
|
||||
.unwrap_or_else(|| panic!("Rust target did not produce {rel}"));
|
||||
let expected = read_baseline(rel);
|
||||
if *actual != expected {
|
||||
for (lineno, (a, b)) in actual.lines().zip(expected.lines()).enumerate() {
|
||||
if a != b {
|
||||
panic!(
|
||||
"{rel} diverges at line {}:\n expected: {b:?}\n actual: {a:?}",
|
||||
lineno + 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
panic!(
|
||||
"{rel} diverges in length: actual={} expected={}\n--- actual (last 200) ---\n{}\n--- expected (last 200) ---\n{}",
|
||||
actual.len(), expected.len(),
|
||||
&actual[actual.len().saturating_sub(200)..],
|
||||
&expected[expected.len().saturating_sub(200)..],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn rust_target_all_files_match_baseline() {
|
||||
let ir = load_ir();
|
||||
let files = RustCrate.emit(&ir, &fixture_config());
|
||||
let index = emit_index(&files);
|
||||
|
||||
for rel in [
|
||||
"Cargo.toml",
|
||||
"src/lib.rs",
|
||||
"src/types.rs",
|
||||
"src/contexts/user.rs",
|
||||
"src/contexts/mod.rs",
|
||||
"src/functions/echo.rs",
|
||||
"src/functions/whoami.rs",
|
||||
"src/functions/find_user.rs",
|
||||
"src/functions/rename_user.rs",
|
||||
"src/functions/mod.rs",
|
||||
"src/mutations/update_profile.rs",
|
||||
"src/mutations/mod.rs",
|
||||
] {
|
||||
assert_byte_equal(rel, &index);
|
||||
}
|
||||
}
|
||||
143
protocol/mizan-codegen/tests/stage1_parity.rs
Normal file
143
protocol/mizan-codegen/tests/stage1_parity.rs
Normal file
@@ -0,0 +1,143 @@
|
||||
//! Byte-equivalence tests for the deterministic Stage 1 files (contexts,
|
||||
//! mutations, functions, index). Baseline output captured from the JS
|
||||
//! codegen at `protocol/mizan-generate/generator/lib/stage1.mjs` against
|
||||
//! the AFI fixture schema (`tests/fixtures/afi_schema.json`).
|
||||
//!
|
||||
//! `types.ts` is NOT byte-checked here — the JS codegen routes type
|
||||
//! emission through openapi-typescript while the Rust substrate emits
|
||||
//! Pydantic schemas directly. Equivalence for types.ts is structural
|
||||
//! (named exports present and importable), checked in a separate test.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use mizan_codegen::config::{Config, SourceConfig};
|
||||
use mizan_codegen::emit::{CodegenTarget, EmittedFile};
|
||||
use mizan_codegen::emit::stage1::Stage1;
|
||||
use mizan_codegen::fetch::parse_ir_from_str;
|
||||
|
||||
|
||||
fn load_ir() -> mizan_codegen::ir::MizanIR {
|
||||
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/fixtures/afi_schema.json");
|
||||
let raw = std::fs::read_to_string(&path).unwrap();
|
||||
parse_ir_from_str(&raw).unwrap()
|
||||
}
|
||||
|
||||
|
||||
fn synthetic_config() -> Config {
|
||||
Config {
|
||||
project_id: None,
|
||||
output: PathBuf::from("/tmp"),
|
||||
targets: vec!["stage1".to_string()],
|
||||
source: SourceConfig { fastapi: None, django: None },
|
||||
rust_kernel: None,
|
||||
rust_crate_name: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn emit_index(files: &[EmittedFile]) -> BTreeMap<PathBuf, &str> {
|
||||
files.iter().map(|f| (f.rel_path.clone(), f.content.as_str())).collect()
|
||||
}
|
||||
|
||||
|
||||
fn read_baseline(rel: &str) -> String {
|
||||
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("tests/fixtures/js_stage1")
|
||||
.join(rel);
|
||||
std::fs::read_to_string(&path)
|
||||
.unwrap_or_else(|e| panic!("read baseline {}: {e}", path.display()))
|
||||
}
|
||||
|
||||
|
||||
fn assert_byte_equal(rel: &str, files: &BTreeMap<PathBuf, &str>) {
|
||||
let actual = files
|
||||
.get(&PathBuf::from(rel))
|
||||
.unwrap_or_else(|| panic!("emitter did not produce {rel}"));
|
||||
let expected = read_baseline(rel);
|
||||
if *actual != expected {
|
||||
// Surface a diff-friendly failure message — first divergent line wins.
|
||||
for (lineno, (a, b)) in actual.lines().zip(expected.lines()).enumerate() {
|
||||
if a != b {
|
||||
panic!(
|
||||
"{rel} diverges at line {}:\n expected: {b:?}\n actual: {a:?}",
|
||||
lineno + 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
panic!(
|
||||
"{rel} diverges in length: actual={} expected={}",
|
||||
actual.len(), expected.len(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn stage1_contexts_user_byte_match() {
|
||||
let ir = load_ir();
|
||||
let files = Stage1.emit(&ir, &synthetic_config());
|
||||
let index = emit_index(&files);
|
||||
assert_byte_equal("contexts/user.ts", &index);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn stage1_function_files_byte_match() {
|
||||
let ir = load_ir();
|
||||
let files = Stage1.emit(&ir, &synthetic_config());
|
||||
let index = emit_index(&files);
|
||||
for rel in ["functions/echo.ts", "functions/whoami.ts",
|
||||
"functions/findUser.ts", "functions/renameUser.ts"] {
|
||||
assert_byte_equal(rel, &index);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn stage1_mutation_files_byte_match() {
|
||||
let ir = load_ir();
|
||||
let files = Stage1.emit(&ir, &synthetic_config());
|
||||
let index = emit_index(&files);
|
||||
assert_byte_equal("mutations/updateProfile.ts", &index);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn stage1_index_byte_match() {
|
||||
let ir = load_ir();
|
||||
let files = Stage1.emit(&ir, &synthetic_config());
|
||||
let index = emit_index(&files);
|
||||
assert_byte_equal("index.ts", &index);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn stage1_types_exports_expected_names() {
|
||||
let ir = load_ir();
|
||||
let files = Stage1.emit(&ir, &synthetic_config());
|
||||
let index = emit_index(&files);
|
||||
let types = index
|
||||
.get(&PathBuf::from("types.ts"))
|
||||
.expect("types.ts must be emitted");
|
||||
|
||||
// Every Pydantic schema named in the IR must surface as a top-level
|
||||
// exported type or interface so Stage 2 adapters can import by name.
|
||||
for expected in [
|
||||
"echoInput", "echoOutput",
|
||||
"whoamiOutput",
|
||||
"userProfileInput", "userProfileOutput",
|
||||
"userOrdersInput",
|
||||
"updateProfileInput", "updateProfileOutput",
|
||||
"findUserInput", "findUserOutput",
|
||||
"renameUserInput", "renameUserOutput",
|
||||
] {
|
||||
let needle_interface = format!("export interface {expected} ");
|
||||
let needle_type = format!("export type {expected} =");
|
||||
assert!(
|
||||
types.contains(&needle_interface) || types.contains(&needle_type),
|
||||
"types.ts must export {expected:?} (interface or type)",
|
||||
);
|
||||
}
|
||||
}
|
||||
66
protocol/mizan-codegen/tests/vue_svelte_parity.rs
Normal file
66
protocol/mizan-codegen/tests/vue_svelte_parity.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
//! Byte-equivalence tests for Vue + Svelte targets against JS baselines.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use mizan_codegen::config::{Config, SourceConfig};
|
||||
use mizan_codegen::emit::CodegenTarget;
|
||||
use mizan_codegen::emit::svelte::SvelteAdapter;
|
||||
use mizan_codegen::emit::vue::VueAdapter;
|
||||
use mizan_codegen::fetch::parse_ir_from_str;
|
||||
|
||||
|
||||
fn load_ir() -> mizan_codegen::ir::MizanIR {
|
||||
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/afi_schema.json");
|
||||
parse_ir_from_str(&std::fs::read_to_string(&path).unwrap()).unwrap()
|
||||
}
|
||||
|
||||
|
||||
fn fixture_config(target: &str) -> Config {
|
||||
Config {
|
||||
project_id: None,
|
||||
output: PathBuf::from("/tmp"),
|
||||
targets: vec![target.to_string()],
|
||||
source: SourceConfig { fastapi: None, django: None },
|
||||
rust_kernel: None,
|
||||
rust_crate_name: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn assert_byte_equal(actual: &str, baseline_path: &str, label: &str) {
|
||||
let baseline = std::fs::read_to_string(
|
||||
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(baseline_path),
|
||||
).unwrap();
|
||||
if actual != baseline {
|
||||
for (lineno, (a, b)) in actual.lines().zip(baseline.lines()).enumerate() {
|
||||
if a != b {
|
||||
panic!(
|
||||
"{label} diverges at line {}:\n expected: {b:?}\n actual: {a:?}",
|
||||
lineno + 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
panic!(
|
||||
"{label} diverges in length: actual={} expected={}",
|
||||
actual.len(), baseline.len(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn vue_target_byte_match() {
|
||||
let ir = load_ir();
|
||||
let files = VueAdapter.emit(&ir, &fixture_config("vue"));
|
||||
assert_eq!(files.len(), 1);
|
||||
assert_byte_equal(&files[0].content, "tests/fixtures/js_vue/vue.ts", "vue.ts");
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn svelte_target_byte_match() {
|
||||
let ir = load_ir();
|
||||
let files = SvelteAdapter.emit(&ir, &fixture_config("svelte"));
|
||||
assert_eq!(files.len(), 1);
|
||||
assert_byte_equal(&files[0].content, "tests/fixtures/js_svelte/svelte.ts", "svelte.ts");
|
||||
}
|
||||
Reference in New Issue
Block a user