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.
Rust agent-skillsclaude-codeclicode-migrationcodexffigolangjavajavascriptmcpmigrationnapiperformanceportingpyo3pythonrewriterubyrusttypescript
mod common;
use std::path::Path;
use rust_anything::bridge::{self, Manifest};
use rust_anything::{port_map, recording};
fn copy_dir(from: &Path, to: &Path) {
std::fs::create_dir_all(to).unwrap();
for entry in std::fs::read_dir(from).unwrap() {
let entry = entry.unwrap();
let dest = to.join(entry.file_name());
if entry.file_type().unwrap().is_dir() {
copy_dir(&entry.path(), &dest);
} else {
std::fs::copy(entry.path(), &dest).unwrap();
}
}
}
#[test]
fn map_lists_ports_and_marks_upstream_changes() {
let fixture = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/mini");
let repo = std::env::temp_dir().join(format!("rust-anything-map-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&repo);
copy_dir(&fixture, &repo);
let unit = "packages/a/src/util.ts";
let crate_dir = repo.join("rust/util-rs");
bridge::bridge(&bridge::Options {
root: &repo,
unit,
kind: None,
out: Some(crate_dir.clone()),
name: None,
force: false,
keep: Vec::new(),
lang: None,
batch: Vec::new(),
as_json: Vec::new(),
allocator: Default::default(),
})
.unwrap();
let map = port_map::map(&repo, &[]).unwrap();
assert_eq!(map.ports.len(), 1, "{map:#?}");
let port = &map.ports[0];
assert_eq!(port.unit, unit);
assert_eq!(port.crate_dir, "rust/util-rs");
assert!(!port.in_rust.is_empty());
assert_eq!(port.cases, 0);
assert!(port.last_diff.is_none());
assert!(port.changed_upstream.is_none());
assert!(map.analyze_error.is_none(), "{:?}", map.analyze_error);
assert!(map.not_ported.iter().all(|u| u.unit != unit));
assert_eq!(map.changed(), 0);
let m = Manifest::load(&crate_dir).unwrap();
recording::save_fingerprint(&crate_dir, &m, None).unwrap();
let source = repo.join(unit);
let text = std::fs::read_to_string(&source).unwrap();
std::fs::write(&source, format!("{text}\nexport const added = 1;\n")).unwrap();
let map = port_map::map(&repo, &[]).unwrap();
assert_eq!(map.changed(), 1);
let md = port_map::markdown(&map);
assert!(
md.contains("**1 changed upstream** since it was recorded: re-record it"),
"{md}"
);
assert!(md.contains("`rust-anything record rust/util-rs`"), "{md}");
let outside =
std::env::temp_dir().join(format!("rust-anything-map-out-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&outside);
bridge::bridge(&bridge::Options {
root: &repo,
unit,
kind: None,
out: Some(outside.clone()),
name: Some("util-outside".into()),
force: false,
keep: Vec::new(),
lang: None,
batch: Vec::new(),
as_json: Vec::new(),
allocator: Default::default(),
})
.unwrap();
assert_eq!(port_map::map(&repo, &[]).unwrap().ports.len(), 1);
assert_eq!(
port_map::map(&repo, std::slice::from_ref(&outside))
.unwrap()
.ports
.len(),
2
);
std::fs::remove_dir_all(&repo).unwrap();
std::fs::remove_dir_all(&outside).unwrap();
}
#[test]
fn map_json_names_a_c_abi_port_the_way_bridge_and_the_manifest_do() {
let fixture = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/mini");
let repo = std::env::temp_dir().join(format!("rust-anything-map-cabi-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&repo);
copy_dir(&fixture, &repo);
let unit = "packages/a/src/util.ts";
let crate_dir = repo.join("rust/util-rs");
bridge::bridge(&bridge::Options {
root: &repo,
unit,
kind: Some(bridge::Kind::CAbi),
out: Some(crate_dir.clone()),
name: None,
force: false,
keep: Vec::new(),
lang: None,
batch: Vec::new(),
as_json: Vec::new(),
allocator: Default::default(),
})
.unwrap();
let manifest = std::fs::read_to_string(crate_dir.join(bridge::MANIFEST)).unwrap();
assert!(manifest.contains("\"c-abi\""), "{manifest}");
let map = port_map::map(&repo, &[]).unwrap();
assert_eq!(map.ports.len(), 1, "{map:#?}");
// Not "cabi" (Kind::CAbi's Debug, lowercased): the same "c-abi" bridge --kind takes and the
// manifest writes.
assert_eq!(map.ports[0].kind, "c-abi");
std::fs::remove_dir_all(&repo).unwrap();
}
#[test]
fn map_names_a_crate_folder_whose_manifest_does_not_parse() {
let fixture = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/mini");
let repo = std::env::temp_dir().join(format!("rust-anything-map-bad-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&repo);
copy_dir(&fixture, &repo);
let crate_dir = repo.join("rust/util-rs");
bridge::bridge(&bridge::Options {
root: &repo,
unit: "packages/a/src/util.ts",
kind: None,
out: Some(crate_dir.clone()),
name: None,
force: false,
keep: Vec::new(),
lang: None,
batch: Vec::new(),
as_json: Vec::new(),
allocator: Default::default(),
})
.unwrap();
// A manifest that does not parse used to be skipped in silence: the map then said the unit had
// no port, which is wrong: the crate folder is right there. Break it the way a hand edit does:
// valid JSON, one required field missing.
let path = crate_dir.join("rust-anything.json");
let mut manifest: serde_json::Value =
serde_json::from_str(&std::fs::read_to_string(&path).unwrap()).unwrap();
manifest["exports"][0]["params"][0]
.as_object_mut()
.unwrap()
.remove("optional");
std::fs::write(&path, manifest.to_string()).unwrap();
std::fs::create_dir_all(repo.join("rust/app")).unwrap();
std::fs::write(
repo.join("rust/app/rust-anything.json"),
r#"{"schema": "rust-anything/front-door@1", "name": "mini"}"#,
)
.unwrap();
let map = port_map::map(&repo, &[]).unwrap();
let md = port_map::markdown(&map);
assert!(md.contains("could not be read"), "{md}");
assert!(!md.contains("rust/app"), "{md}");
// The unit is still counted, so the summary does not claim the repository has none.
assert!(md.contains("0 of 6 units"), "{md}");
assert!(md.contains("rust/util-rs"), "{md}");
assert!(
map.not_ported
.iter()
.all(|u| u.unit != "packages/a/src/util.ts"),
"the folder still covers its unit: {:?}",
map.not_ported.iter().map(|u| &u.unit).collect::<Vec<_>>()
);
let _ = std::fs::remove_dir_all(&repo);
}
#[test]
fn a_broken_manifest_with_an_unresolved_root_is_reported() {
let fixture = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/mini");
let repo = std::env::temp_dir().join(format!("rust-anything-map-root-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&repo);
copy_dir(&fixture, &repo);
let crate_dir = repo.join("rust/util-rs");
bridge::bridge(&bridge::Options {
root: &repo,
unit: "packages/a/src/util.ts",
kind: None,
out: Some(crate_dir.clone()),
name: None,
force: false,
keep: Vec::new(),
lang: None,
batch: Vec::new(),
as_json: Vec::new(),
allocator: Default::default(),
})
.unwrap();
// `root` names a directory that does not exist: that says nothing about which repository the
// folder belongs to, so the failure is reported instead of being taken for a foreign port.
std::fs::write(
crate_dir.join("rust-anything.json"),
"{\"schema\": \"rust-anything/bridge@1\", \"root\": \"../../missing-dir\"}\n",
)
.unwrap();
let map = port_map::map(&repo, &[]).unwrap();
let md = port_map::markdown(&map);
assert!(md.contains("could not be read"), "{md}");
assert!(md.contains("rust/util-rs"), "{md}");
let _ = std::fs::remove_dir_all(&repo);
}
#[test]
fn map_refuses_a_file_instead_of_printing_an_empty_map() {
let file = std::env::temp_dir().join(format!("ra-map-file-{}.toml", std::process::id()));
std::fs::write(&file, "[package]\nname = \"x\"\n").unwrap();
let err = port_map::map(&file, &[]).unwrap_err().to_string();
// `map` canonicalizes first, and on macOS `temp_dir()` can be a symlinked path (`/var` →
// `/private/var`), so compare against what the message can carry.
let shown = std::fs::canonicalize(&file).unwrap_or_else(|_| file.clone());
assert!(
err.contains(&shown.display().to_string()) && err.contains("is a file"),
"the error names the path and what is wrong: {err}"
);
// `--crates` entries too: a file there is the same mistake one argument over.
let root = std::env::temp_dir().join(format!("ra-map-crates-{}", std::process::id()));
std::fs::create_dir_all(&root).unwrap();
let err = port_map::map(&root, std::slice::from_ref(&file))
.unwrap_err()
.to_string();
assert!(err.contains("`--crates` takes folders"), "{err}");
let _ = std::fs::remove_dir_all(&root);
let _ = std::fs::remove_file(&file);
}
#[test]
fn one_unit_bridged_twice_counts_as_one_unit() {
let repo = std::env::temp_dir().join(format!("ra-map-twice-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&repo);
std::fs::create_dir_all(repo.join("src")).unwrap();
std::fs::write(
repo.join("package.json"),
"{\"name\":\"app\",\"version\":\"1.0.0\"}\n",
)
.unwrap();
std::fs::write(
repo.join("src/math.ts"),
"export function add(a: number, b: number): number { return a + b; }\n",
)
.unwrap();
for out in ["rust/math-rs", "rust/mathn"] {
bridge::bridge(&bridge::Options {
root: &repo,
unit: "src/math.ts",
kind: None,
out: Some(repo.join(out)),
name: None,
force: false,
keep: Vec::new(),
lang: None,
batch: Vec::new(),
as_json: Vec::new(),
allocator: Default::default(),
})
.unwrap();
}
let map = port_map::map(&repo, &[]).unwrap();
assert!(map.analyze_error.is_none(), "{:?}", map.analyze_error);
let text = port_map::markdown(&map);
assert!(text.contains("1 of 1 unit has a Rust port"), "{text}");
// The table still has a row per crate.
assert!(
text.contains("| `src/math.ts` | `rust/math-rs` |"),
"{text}"
);
assert!(text.contains("| `src/math.ts` | `rust/mathn` |"), "{text}");
let _ = std::fs::remove_dir_all(&repo);
}
#[test]
fn unrecorded_port_is_marked_not_recorded() {
let fixture = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/mini");
let repo = std::env::temp_dir().join(format!(
"rust-anything-map-unrecorded-{}",
std::process::id()
));
let _ = std::fs::remove_dir_all(&repo);
copy_dir(&fixture, &repo);
let unit = "packages/a/src/util.ts";
let crate_dir = repo.join("rust/util-rs");
bridge::bridge(&bridge::Options {
root: &repo,
unit,
kind: None,
out: Some(crate_dir.clone()),
name: None,
force: false,
keep: Vec::new(),
lang: None,
batch: Vec::new(),
as_json: Vec::new(),
allocator: Default::default(),
})
.unwrap();
let map = port_map::map(&repo, &[]).unwrap();
assert_eq!(map.ports.len(), 1);
assert!(!map.ports[0].recorded);
let json = serde_json::to_value(&map).unwrap();
assert_eq!(json["ports"][0]["recorded"], serde_json::json!(false));
let md = port_map::markdown(&map);
assert!(md.contains("| not recorded |"), "{md}");
assert!(!md.contains("| unchanged |"), "{md}");
assert!(
!md.contains("no original changed since it was recorded"),
"{md}"
);
assert!(md.contains("none recorded yet."), "{md}");
let m = Manifest::load(&crate_dir).unwrap();
// A recording is its cases plus the fingerprint over them; the fingerprint alone, with the
// cases gone, is a recording that cannot be trusted.
std::fs::write(
crate_dir.join(rust_anything::harness::CASES),
"{\"fn\":\"words\",\"args\":[\"a b\"],\"ok\":[\"a\",\"b\"]}\n",
)
.unwrap();
recording::save_fingerprint(&crate_dir, &m, None).unwrap();
let map = port_map::map(&repo, &[]).unwrap();
assert!(map.ports[0].recorded);
let json = serde_json::to_value(&map).unwrap();
assert_eq!(json["ports"][0]["recorded"], serde_json::json!(true));
let md = port_map::markdown(&map);
assert!(md.contains("| unchanged |"), "{md}");
assert!(!md.contains("| not recorded |"), "{md}");
assert!(
md.contains("no original changed since it was recorded"),
"{md}"
);
let _ = std::fs::remove_dir_all(&repo);
}
#[test]
fn map_footer_includes_passed_root_and_omits_commit_hint_on_stdout() {
let fixture = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/mini");
let repo = std::env::temp_dir().join(format!("rust-anything-map hint-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&repo);
copy_dir(&fixture, &repo);
let stdout_run = common::cli()
.args(["map", "tests/fixtures/mini"])
.current_dir(env!("CARGO_MANIFEST_DIR"))
.output()
.unwrap();
assert!(stdout_run.status.success());
let out = String::from_utf8_lossy(&stdout_run.stdout);
assert!(
out.contains("rust-anything map tests/fixtures/mini --write"),
"{out}"
);
assert!(!out.contains("Commit this file"), "{out}");
let write_run = common::cli()
.args(["map", "--write"])
.arg(&repo)
.output()
.unwrap();
assert!(write_run.status.success());
let written = std::fs::read_to_string(repo.join(port_map::MAP_FILE)).unwrap();
let expected = format!(
"rust-anything map {} --write",
rust_anything::app::cli_path(&repo)
);
assert!(written.contains(&expected), "{written}");
assert!(written.contains("Commit this file:"), "{written}");
let dot_write = common::cli()
.args(["map", ".", "--write"])
.current_dir(&repo)
.output()
.unwrap();
assert!(dot_write.status.success());
let dot_written = std::fs::read_to_string(repo.join(port_map::MAP_FILE)).unwrap();
assert!(
dot_written.contains("Regenerate with `rust-anything map --write`. Commit this file:"),
"{dot_written}"
);
let _ = std::fs::remove_dir_all(&repo);
}
#[test]
fn map_check_fails_for_a_recording_whose_fingerprint_file_is_gone() {
// Deleting recorded-from.json used to read as "recorded before fingerprints": `map --check`
// said nothing changed, while the stale and edited-recording checks were off.
let fixture = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/mini");
let repo = std::env::temp_dir().join(format!("rust-anything-map-gone-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&repo);
copy_dir(&fixture, &repo);
let crate_dir = repo.join("rust/util-rs");
bridge::bridge(&bridge::Options {
root: &repo,
unit: "packages/a/src/util.ts",
kind: None,
out: Some(crate_dir.clone()),
name: None,
force: false,
keep: Vec::new(),
lang: None,
batch: Vec::new(),
as_json: Vec::new(),
allocator: Default::default(),
})
.unwrap();
std::fs::write(
crate_dir.join("cases.jsonl"),
"{\"fn\":\"count\",\"args\":[\"a b\"],\"ok\":1}\n",
)
.unwrap();
let m = Manifest::load(&crate_dir).unwrap();
recording::save_fingerprint(&crate_dir, &m, None).unwrap();
let map = port_map::map(&repo, &[]).unwrap();
assert_eq!((map.changed(), map.untrusted()), (0, 0), "{map:#?}");
std::fs::remove_file(crate_dir.join("recorded-from.json")).unwrap();
let map = port_map::map(&repo, &[]).unwrap();
assert_eq!(map.changed(), 0, "the original did not change");
assert_eq!(map.untrusted(), 1, "{map:#?}");
let md = port_map::markdown(&map);
assert!(md.contains("1 recording cannot be trusted"), "{md}");
assert!(
md.contains("**cannot be trusted**: `rust-anything record rust/util-rs`"),
"{md}"
);
let out = common::cli()
.args(["map", "--check"])
.arg(&repo)
.output()
.unwrap();
assert_eq!(
out.status.code(),
Some(1),
"map --check must exit 1, not 2 for an error: {}",
String::from_utf8_lossy(&out.stderr)
);
std::fs::remove_dir_all(&repo).unwrap();
let help = common::cli().args(["map", "--help"]).output().unwrap();
let help = String::from_utf8_lossy(&help.stdout);
assert!(
help.contains("a recording cannot be trusted"),
"map --help must say an untrusted recording fails --check: {help}"
);
}