Port
You or your agent write the port in src/port.rs. Everything else in the crate is generated. After changing a signature in port.rs, run rust-anything refresh <crate>.
Rules that save a round of diff
- Read each risk the plan lists for the unit, at its
file:line, before writing code. - String offsets: JS and Java index UTF-16 code units, Rust UTF-8 bytes. Use the generated
src/utf16.rs. - Errors: return
Err("TypeName: message"). It becomes that exception in Python, Ruby and Java, and theerrortext in Go. A Rust panic never matches a recorded error. - Numbers in Python and Ruby: ints that the original passes through must come back as ints. In Python ports use
crate::pynum::Numfor int-or-float, notserde_json::Value. - C: inputs recorded as
crash:are undefined behavior; any result passes. - Ordered maps and object keys:
indexmapwhere order reaches the output. - Regex lookaround or backreferences:
fancy-regex, notregex. Either way,\w,\b,\dand case-insensitive matching treat non-ASCII text differently from the source language's engine. Setunicode(false)or spell out ASCII classes ([A-Za-z0-9_]) where the original's\wis ASCII-only (JS withoutu, Java). - No
unsafe. If truly needed: minimal, isolated, with a// SAFETY:comment. - Keep helpers private. Only the unit's exports are
pub; apubhelper becomes part of the bridged API. - Add Rust unit tests in
port.rsfor the cases you reason about.
The generated js.rs carries helpers that reproduce JavaScript where Rust differs, such as js::number_to_string (JS String(n)) and js::math_round (Math.round).
An example: the emoji that moved a position
packages/core/src/fuzzy.ts from x18n, ported both ways in
demo/:
rust-anything analyze ../x18n # fuzzy.ts: ready, wasm-bindgen (core runs in browsers)
rust-anything bridge ../x18n packages/core/src/fuzzy.ts --out demo/fuzzy-rs
rust-anything record demo/fuzzy-rs --count 2000
# write src/port.rs
rust-anything gate demo/fuzzy-rs
A first port that indexed by Rust char passed simple tests but failed diff:
198/200 cases match
- fuzzyScore("b😀", "a😀b😀c"): expected {"ok":1997}, got {"ok":1998}
JS indexOf counts UTF-16 code units, so an emoji is two positions. The port in
demo/*/src/port.rs uses the generated utf16 helpers and passes 2000/2000 cases and the gate,
as napi-rs (demo/fuzzy-napi) and as wasm-bindgen (demo/fuzzy-rs).
The plan rust-anything writes for all of x18n: docs/examples/x18n.md.
How big should it be?
gate compares the port's lines with the original's and fails above 3x (plus 50 lines for imports and signatures). Over that usually means hand-writing what a crate already does, or handling cases one by one. analyze lists the crates to use.