Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

fix(bridge): explain the language scope of as-json (#1226)

RRFZ6d9d2e7c2d3a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 104160dc..7756ea2c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,8 @@
## Unreleased
+- `bridge --as-json` explains its TS/JS scope when used on another language. The CLI and MCP help
+ name that scope as well (#1201, #1226 by @Plastic-time).
- `app drift` lists a case whose recording was edited, marked skipped or removed under "Recording
changed" (verdict `recording edited`, `recording skipped` or `recording removed`, and `recording`
in `--json`), not as "original changed": it says to record again or restore the files, and
diff --git a/docs/tools.md b/docs/tools.md
index f69dadfe..1668c292 100644
--- a/docs/tools.md
+++ b/docs/tools.md
@@ -32,7 +32,7 @@ Scaffold a Rust crate with stubs to port in src/port.rs. Generates napi-rs or wa
| `force` | boolean | | Bridge a unit the plan marks blocked or keep. |
| `keep` | string[] | | Exports to keep in the source language: `name` or `name: reason`. |
| `batch` | string[] | | Batch classes, `function:param`: the function runs over many `param` values kept in Rust, on all cores, in one call. A `<Function>Index` struct in port.rs replaces the default per-item loop. |
-| `as_json` | string[] | | Type parameters the app only fills with data (`T` in `paginate<T>(items: T[])`): bridged as JSON values instead of keeping the export in TS. |
+| `as_json` | string[] | | TS/JS units: bridge type parameters the app only fills with data (`T` in `paginate<T>(items: T[])`) as JSON values instead of keeping the export in TS. |
| `allocator` | string | | Global allocator for crates with batches. mimalloc (default) is C and leaves wasm alone; rusty_alloc is pure Rust (no C compiler) and also serves wasm. |
| `end_state` | string | | What the bindings are for once the port is done; ask the user before bridging. replace: the original is deleted after cut-over. bindings: callers keep importing it for good, so the bindings are public API. scaffold: the bindings are deleted at cut-over. |
diff --git a/src/bridge.rs b/src/bridge.rs
index 06b1fe18..2343cdeb 100644
--- a/src/bridge.rs
+++ b/src/bridge.rs
@@ -2124,6 +2124,18 @@ pub fn bridge(opts: &Options) -> Result<Outcome> {
&defined.iter().map(|(name, _)| *name).collect::<Vec<_>>(),
&unit.id,
)?;
+ if lang != crate::lang::Lang::Ts && !opts.as_json.is_empty() {
+ let tip = if lang == crate::lang::Lang::Python {
+ " The Python bridge already maps generic data to JSON values."
+ } else {
+ ""
+ };
+ bail!(
+ "--as-json is for TS/JS units. {wanted} is {} and uses {}.{tip}",
+ lang.name(),
+ Bridge::for_lang(lang).label()
+ );
+ }
check_as_json(&opts.as_json, &type_params, &unit.id)?;
if let Some(pair) = defined.windows(2).find(|w| w[0].0 == w[1].0) {
bail!(
@@ -2915,6 +2927,48 @@ mod tests {
std::fs::remove_dir_all(dir).unwrap();
}
+ #[test]
+ fn bridge_refuses_as_json_for_python_units() {
+ let dir = crate::app::temp_dir("bridge-python-as-json").unwrap();
+ std::fs::write(
+ dir.join("gen.py"),
+ "from typing import TypeVar\nT = TypeVar('T')\n\
+ def first(xs: list[T]) -> T:\n return xs[0]\n",
+ )
+ .unwrap();
+ let run = |as_json, keep| {
+ bridge(&Options {
+ root: &dir,
+ unit: "gen.py",
+ kind: None,
+ out: None,
+ name: None,
+ force: false,
+ lang: None,
+ keep,
+ batch: vec![],
+ allocator: Allocator::default(),
+ as_json,
+ })
+ };
+ let refused = run(vec!["T".into()], vec![]);
+ let bad_keep = run(vec!["T".into()], vec!["missing".into()]);
+ let wrote_rust = dir.join("rust").exists();
+ let default = run(vec![], vec![]);
+ let port = std::fs::read_to_string(dir.join("rust/gen-rs/src/port.rs"));
+ std::fs::remove_dir_all(dir).unwrap();
+
+ let err = format!("{:#}", refused.unwrap_err());
+ assert!(err.contains("--as-json is for TS/JS units"), "{err}");
+ assert!(err.contains("gen.py is Python"), "{err}");
+ assert!(err.contains("JSON values"), "{err}");
+ assert!(!wrote_rust);
+ let err = format!("{:#}", bad_keep.unwrap_err());
+ assert!(err.contains("--keep `missing`"), "{err}");
+ assert!(default.is_ok(), "{default:?}");
+ assert!(port.unwrap().contains("Vec<serde_json::Value>"));
+ }
+
#[test]
fn bridge_refuses_an_as_json_name_that_is_not_a_type_parameter() {
let dir = crate::app::temp_dir("bridge-as-json").unwrap();
diff --git a/src/main.rs b/src/main.rs
index 4f83354d..4cf96dda 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -96,8 +96,8 @@ enum Cmd {
/// Rust, on all cores (what `profile` recommends for leaves called in loops).
#[arg(long)]
batch: Vec<String>,
- /// Bridge a type parameter as JSON values (`--as-json T`) when the app only fills it
- /// with data, instead of keeping the generic export in TS. Repeatable.
+ /// TS/JS units: bridge a type parameter as JSON values (`--as-json T`) when the app only
+ /// fills it with data, instead of keeping the generic export in TS. Repeatable.
#[arg(long, value_name = "TYPE_PARAM")]
as_json: Vec<String>,
/// Global allocator for crates with batches: `mimalloc` (C; wasm keeps Rust's default)
diff --git a/src/mcp.rs b/src/mcp.rs
index a96ac7b2..bd8ef715 100644
--- a/src/mcp.rs
+++ b/src/mcp.rs
@@ -741,7 +741,7 @@ fn tool_schemas() -> Value {
"force": { "type": "boolean", "description": "Bridge a unit the plan marks blocked or keep." },
"keep": { "type": "array", "items": { "type": "string" }, "description": "Exports to keep in the source language: `name` or `name: reason`." },
"batch": { "type": "array", "items": { "type": "string" }, "description": "Batch classes, `function:param`: the function runs over many `param` values kept in Rust, on all cores, in one call. A `<Function>Index` struct in port.rs replaces the default per-item loop." },
- "as_json": { "type": "array", "items": { "type": "string" }, "description": "Type parameters the app only fills with data (`T` in `paginate<T>(items: T[])`): bridged as JSON values instead of keeping the export in TS." },
+ "as_json": { "type": "array", "items": { "type": "string" }, "description": "TS/JS units: bridge type parameters the app only fills with data (`T` in `paginate<T>(items: T[])`) as JSON values instead of keeping the export in TS." },
"allocator": { "type": "string", "enum": ["mimalloc", "rusty_alloc"], "description": "Global allocator for crates with batches. mimalloc (default) is C and leaves wasm alone; rusty_alloc is pure Rust (no C compiler) and also serves wasm." },
"end_state": { "type": "string", "enum": ["replace", "bindings", "scaffold"], "description": "What the bindings are for once the port is done; ask the user before bridging. replace: the original is deleted after cut-over. bindings: callers keep importing it for good, so the bindings are public API. scaffold: the bindings are deleted at cut-over." },
}, "required": ["root", "unit"] },
diff --git a/tests/tools.rs b/tests/tools.rs
index 3b1a4f53..6204c318 100644
--- a/tests/tools.rs
+++ b/tests/tools.rs
@@ -263,6 +263,46 @@ fn app_record_and_front_door_help_name_servers_and_libraries_like_the_mcp_tools(
}
}
+#[test]
+fn bridge_as_json_scope_agrees_in_help_and_for_a_c_source_file() {
+ let help = common::cli().args(["bridge", "--help"]).output().unwrap();
+ assert!(help.status.success(), "{help:?}");
+ let help = String::from_utf8(help.stdout).unwrap();
+ assert!(
+ help.contains("TS/JS units: bridge a type parameter"),
+ "{help}"
+ );
+
+ let bridge = tools()
+ .into_iter()
+ .find(|tool| tool["name"] == "bridge")
+ .unwrap();
+ let description = bridge["inputSchema"]["properties"]["as_json"]["description"]
+ .as_str()
+ .unwrap();
+ assert!(description.starts_with("TS/JS units:"), "{description}");
+
+ let dir = common::Scratch::new(std::env::temp_dir().join(format!(
+ "rust-anything-bridge-as-json-c-{}",
+ std::process::id()
+ )));
+ std::fs::write(
+ dir.0.join("sum.c"),
+ "int add(int a, int b) { return a + b; }\n",
+ )
+ .unwrap();
+ let result = common::cli()
+ .args(["bridge", ".", "sum.c", "--as-json", "T"])
+ .current_dir(&dir.0)
+ .output()
+ .unwrap();
+ assert!(!result.status.success(), "{result:?}");
+ let error = String::from_utf8(result.stderr).unwrap();
+ assert!(error.contains("--as-json is for TS/JS units"), "{error}");
+ assert!(error.contains("C ABI"), "{error}");
+ assert!(!dir.0.join("rust").exists());
+}
+
#[test]
fn replay_timeout_says_its_default_is_the_one_record_saved() {
// Replay falls back to the timeout `app record` saved, not to a fixed 30 seconds.
diff --git a/website/content/cli-help.json b/website/content/cli-help.json
index 214455ab..0924e289 100644
--- a/website/content/cli-help.json
+++ b/website/content/cli-help.json
@@ -11,7 +11,7 @@
},
{
"command": "bridge",
- "help": "2. Scaffold a Rust crate with bindings for one unit\n\nUsage: rust-anything bridge [OPTIONS] <ROOT> <UNIT>\n\nArguments:\n <ROOT>\n Repository root\n\n <UNIT>\n Unit id from `analyze`, or a source file inside a unit to bridge on its own\n\nOptions:\n --kind <KIND>\n TS/JS units: napi or wasm; defaults to the plan's choice. Other languages have their own bridge\n \n [possible values: napi, wasm]\n\n -o, --out <OUT>\n Crate folder; defaults to `<root>/rust/<name>`\n\n --name <NAME>\n Crate name; defaults to `<unit>-rs`\n\n --force\n Bridge a unit the plan marks blocked or keep\n\n --keep <KEEP>\n Keep an export in the original language: `name` or `\"name: reason\"`. Repeatable\n\n --batch <BATCH>\n Add a batch class: `function:param` runs `function` over many `param` values kept in Rust, on all cores (what `profile` recommends for leaves called in loops)\n\n --as-json <TYPE_PARAM>\n Bridge a type parameter as JSON values (`--as-json T`) when the app only fills it with data, instead of keeping the generic export in TS. Repeatable\n\n --allocator <ALLOCATOR>\n Global allocator for crates with batches: `mimalloc` (C; wasm keeps Rust's default) or `rusty_alloc` (pure Rust, no C compiler; wasm too). Kept in `rust-anything.json`\n\n Possible values:\n - mimalloc: mimalloc, built from C with `cc`. Wasm crates keep Rust's default allocator\n - rusty_alloc: rusty_alloc, a pure-Rust remake of mimalloc: no C compiler, and wasm crates get it too\n \n [default: mimalloc]\n\n --end-state <END_STATE>\n What the bindings are for once the port is done: `replace` (the original is deleted after cut-over), `bindings` (callers keep importing it: public API) or `scaffold` (the bindings are deleted at cut-over). Kept in `rust-anything.json`\n\n Possible values:\n - replace: The Rust replaces the original, which is deleted after cut-over\n - bindings: Callers keep importing the unit through the bindings for good: they are public API\n - scaffold: The bindings are temporary and are deleted at cut-over\n\n -h, --help\n Print help (see a summary with '-h')\n"
+ "help": "2. Scaffold a Rust crate with bindings for one unit\n\nUsage: rust-anything bridge [OPTIONS] <ROOT> <UNIT>\n\nArguments:\n <ROOT>\n Repository root\n\n <UNIT>\n Unit id from `analyze`, or a source file inside a unit to bridge on its own\n\nOptions:\n --kind <KIND>\n TS/JS units: napi or wasm; defaults to the plan's choice. Other languages have their own bridge\n \n [possible values: napi, wasm]\n\n -o, --out <OUT>\n Crate folder; defaults to `<root>/rust/<name>`\n\n --name <NAME>\n Crate name; defaults to `<unit>-rs`\n\n --force\n Bridge a unit the plan marks blocked or keep\n\n --keep <KEEP>\n Keep an export in the original language: `name` or `\"name: reason\"`. Repeatable\n\n --batch <BATCH>\n Add a batch class: `function:param` runs `function` over many `param` values kept in Rust, on all cores (what `profile` recommends for leaves called in loops)\n\n --as-json <TYPE_PARAM>\n TS/JS units: bridge a type parameter as JSON values (`--as-json T`) when the app only fills it with data, instead of keeping the generic export in TS. Repeatable\n\n --allocator <ALLOCATOR>\n Global allocator for crates with batches: `mimalloc` (C; wasm keeps Rust's default) or `rusty_alloc` (pure Rust, no C compiler; wasm too). Kept in `rust-anything.json`\n\n Possible values:\n - mimalloc: mimalloc, built from C with `cc`. Wasm crates keep Rust's default allocator\n - rusty_alloc: rusty_alloc, a pure-Rust remake of mimalloc: no C compiler, and wasm crates get it too\n \n [default: mimalloc]\n\n --end-state <END_STATE>\n What the bindings are for once the port is done: `replace` (the original is deleted after cut-over), `bindings` (callers keep importing it: public API) or `scaffold` (the bindings are deleted at cut-over). Kept in `rust-anything.json`\n\n Possible values:\n - replace: The Rust replaces the original, which is deleted after cut-over\n - bindings: Callers keep importing the unit through the bindings for good: they are public API\n - scaffold: The bindings are temporary and are deleted at cut-over\n\n -h, --help\n Print help (see a summary with '-h')\n"
},
{
"command": "refresh",