rust-anything 4.2.0

Rewrite your app in Rust, one piece at a time, checked against the original's recorded behavior at every step. TypeScript/JavaScript, Python, C, Go, Ruby, Java, Kotlin, PHP, C# and PowerShell
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
mod common;

use std::{path::Path, process::Command};

use rust_anything::{
    bridge,
    codegen_java::{DRIVER_JAVA, ERROR_JAVA, JSON_JAVA, WIRE_JAVA},
    harness::DiffOutcome,
};

#[test]
fn java_replay_distinguishes_null_from_a_rejected_argument() {
    if !common::have("java") || !common::have("javac") {
        return;
    }

    let root = std::env::temp_dir().join(format!("rust-anything-java-null-{}", std::process::id()));
    std::fs::create_dir_all(&root).unwrap();
    let result = replay(&root);
    std::fs::remove_dir_all(root).unwrap();

    let diff = result.unwrap();
    assert_eq!((diff.passed, diff.failed), (1, 1));
    assert_eq!(diff.mismatches[0]["actual"]["invalid"], true);
}

#[test]
fn java_replay_and_wrapper_handle_extends_wildcard_parameters() {
    if !common::have("java") || !common::have("javac") {
        return;
    }

    let root = std::env::temp_dir().join(format!(
        "rust-anything-java-wildcard-{}",
        std::process::id()
    ));
    let _ = std::fs::remove_dir_all(&root);
    std::fs::create_dir_all(&root).unwrap();
    let result = replay_wildcards(&root);
    let _ = std::fs::remove_dir_all(&root);
    result.unwrap();
}

fn replay_wildcards(root: &Path) -> anyhow::Result<()> {
    let src = r#"package acme;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class Wildcards {
    public static int count(List<? extends CharSequence> xs) {
        int total = 0;
        for (CharSequence s : xs) total += s.length();
        return total;
    }
    public static int sumValues(Map<String, ? extends Integer> m) {
        int total = 0;
        for (Integer v : m.values()) total += v;
        return total;
    }
    public static int firstLen(Optional<? extends CharSequence> opt) {
        return opt.map(CharSequence::length).orElse(-1);
    }
    public static int nested(List<? extends List<? extends CharSequence>> rows) {
        int total = 0;
        for (List<? extends CharSequence> row : rows) total += row.size();
        return total;
    }
    public static int nestedGenericArray(List<? extends List<String>[][]> grids) {
        List<String>[][] first = grids.get(0);
        return grids.size() + first.length + first[0].length;
    }
    public static Map<CharSequence, Integer> echoCharSeqMap(Map<CharSequence, Integer> m) {
        return m;
    }
}
"#;
    std::fs::create_dir_all(root.join("acme"))?;
    std::fs::write(root.join("acme/Wildcards.java"), src)?;

    // Bridge the class and compile the generated Java wrapper + wire classes with javac.
    let out = root.join("port");
    bridge::bridge(&bridge::Options {
        root,
        unit: "acme/Wildcards.java",
        kind: None,
        out: Some(out.clone()),
        name: Some("wildcards-rs".into()),
        force: false,
        keep: Vec::new(),
        lang: Some(rust_anything::lang::Lang::Java),
        batch: Vec::new(),
        as_json: Vec::new(),
        allocator: Default::default(),
    })?;

    let java_dir = out.join("java");
    let compile_dir = root.join("wrapper-classes");
    std::fs::create_dir_all(&compile_dir)?;
    let mut javac = Command::new("javac");
    javac.args(["-d", compile_dir.to_str().unwrap()]);
    for rel in [
        "rustanything/RustAnythingJson.java",
        "rustanything/RustAnythingWire.java",
        "rustanything/RustAnythingError.java",
        "rustanything/WildcardsRsNative.java",
        "acme/Wildcards.java",
    ] {
        javac.arg(java_dir.join(rel));
    }
    run(&mut javac)?;

    // Compile the original class with the driver and run diff across valid and invalid inputs.
    let run_dir = root.join("driver-run");
    std::fs::create_dir_all(run_dir.join("acme"))?;
    std::fs::write(run_dir.join("RustAnythingDriver.java"), DRIVER_JAVA)?;
    std::fs::write(run_dir.join("RustAnythingJson.java"), JSON_JAVA)?;
    std::fs::write(run_dir.join("RustAnythingWire.java"), WIRE_JAVA)?;
    std::fs::write(run_dir.join("RustAnythingError.java"), ERROR_JAVA)?;
    std::fs::write(run_dir.join("acme/Wildcards.java"), src)?;
    std::fs::write(
        run_dir.join("targets.json"),
        r#"{"count":["acme.Wildcards","count"],"sumValues":["acme.Wildcards","sumValues"],"firstLen":["acme.Wildcards","firstLen"],"nested":["acme.Wildcards","nested"],"nestedGenericArray":["acme.Wildcards","nestedGenericArray"],"echoCharSeqMap":["acme.Wildcards","echoCharSeqMap"]}"#,
    )?;
    std::fs::write(
        run_dir.join("cases.jsonl"),
        concat!(
            r#"{"fn":"count","args":[["hi","there"]],"ok":7}"#,
            "\n",
            r#"{"fn":"sumValues","args":[{"a":2,"b":5}],"ok":7}"#,
            "\n",
            r#"{"fn":"firstLen","args":["rust"],"ok":4}"#,
            "\n",
            r#"{"fn":"firstLen","args":[null],"ok":-1}"#,
            "\n",
            r#"{"fn":"nested","args":[[["a","b"],["c"]]],"ok":3}"#,
            "\n",
            r#"{"fn":"nestedGenericArray","args":[[[[["a"]]]]],"ok":3}"#,
            "\n",
            r#"{"fn":"echoCharSeqMap","args":[{"k":9}],"ok":{"k":9}}"#,
            "\n",
            r#"{"fn":"count","args":[[123]],"ok":0}"#,
            "\n",
        ),
    )?;

    run(Command::new("javac").current_dir(&run_dir).args([
        "-d",
        ".",
        "RustAnythingDriver.java",
        "RustAnythingJson.java",
        "RustAnythingWire.java",
        "RustAnythingError.java",
        "acme/Wildcards.java",
    ]))?;
    run(Command::new("java").current_dir(&run_dir).args([
        "-cp",
        ".",
        "rustanything.RustAnythingDriver",
        "diff",
        "targets.json",
        "cases.jsonl",
        "diff.json",
    ]))?;
    let diff: DiffOutcome = serde_json::from_slice(&std::fs::read(run_dir.join("diff.json"))?)?;
    assert_eq!((diff.passed, diff.failed), (7, 1), "{diff:?}");
    assert_eq!(diff.mismatches[0]["actual"]["invalid"], true, "{diff:?}");
    Ok(())
}

#[test]
fn java_replay_and_wrapper_handle_ordered_sets() {
    if !common::have("java") || !common::have("javac") {
        return;
    }

    let root = std::env::temp_dir().join(format!("rust-anything-java-sets-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&root);
    std::fs::create_dir_all(&root).unwrap();
    let result = replay_ordered_sets(&root);
    let _ = std::fs::remove_dir_all(&root);
    result.unwrap();
}

fn replay_ordered_sets(root: &Path) -> anyhow::Result<()> {
    let src = r#"package acme;
import java.util.LinkedHashSet;
import java.util.NavigableSet;
import java.util.SortedSet;
import java.util.TreeSet;
public class OrderedSets {
    public static String joinSorted(SortedSet<String> words) {
        return String.join(",", words);
    }
    public static TreeSet<String> toTree(LinkedHashSet<String> words) {
        return new TreeSet<>(words);
    }
    public static LinkedHashSet<String> keepOrder(LinkedHashSet<String> words) {
        return words;
    }
    public static int maxNav(NavigableSet<Integer> nums) {
        return nums.isEmpty() ? -1 : nums.last();
    }
    @SuppressWarnings("unchecked")
    public static TreeSet<String>[] splitSets(int n) {
        return new TreeSet[n];
    }
    public static String[] toArray(LinkedHashSet<String> words) {
        return words.toArray(new String[0]);
    }
}
"#;
    std::fs::create_dir_all(root.join("acme"))?;
    std::fs::write(root.join("acme/OrderedSets.java"), src)?;

    let out = root.join("port");
    bridge::bridge(&bridge::Options {
        root,
        unit: "acme/OrderedSets.java",
        kind: None,
        out: Some(out.clone()),
        name: Some("ordered-sets-rs".into()),
        force: false,
        keep: Vec::new(),
        lang: Some(rust_anything::lang::Lang::Java),
        batch: Vec::new(),
        as_json: Vec::new(),
        allocator: Default::default(),
    })?;

    let java_dir = out.join("java");
    let compile_dir = root.join("wrapper-classes");
    std::fs::create_dir_all(&compile_dir)?;
    let mut javac = Command::new("javac");
    javac.args(["-d", compile_dir.to_str().unwrap()]);
    for rel in [
        "rustanything/RustAnythingJson.java",
        "rustanything/RustAnythingWire.java",
        "rustanything/RustAnythingError.java",
        "rustanything/OrderedSetsRsNative.java",
        "acme/OrderedSets.java",
    ] {
        javac.arg(java_dir.join(rel));
    }
    run(&mut javac)?;

    let run_dir = root.join("driver-run");
    std::fs::create_dir_all(run_dir.join("acme"))?;
    std::fs::write(run_dir.join("RustAnythingDriver.java"), DRIVER_JAVA)?;
    std::fs::write(run_dir.join("RustAnythingJson.java"), JSON_JAVA)?;
    std::fs::write(run_dir.join("RustAnythingWire.java"), WIRE_JAVA)?;
    std::fs::write(run_dir.join("RustAnythingError.java"), ERROR_JAVA)?;
    std::fs::write(run_dir.join("acme/OrderedSets.java"), src)?;
    std::fs::write(
        run_dir.join("targets.json"),
        r#"{"joinSorted":["acme.OrderedSets","joinSorted"],"toTree":["acme.OrderedSets","toTree"],"keepOrder":["acme.OrderedSets","keepOrder"],"maxNav":["acme.OrderedSets","maxNav"]}"#,
    )?;
    std::fs::write(
        run_dir.join("cases.jsonl"),
        concat!(
            r#"{"fn":"joinSorted","args":[["a","b"]],"ok":"a,b"}"#,
            "\n",
            r#"{"fn":"toTree","args":[["c","a","b"]],"ok":["a","b","c"]}"#,
            "\n",
            r#"{"fn":"keepOrder","args":[["c","a","b"]],"ok":["c","a","b"]}"#,
            "\n",
            r#"{"fn":"keepOrder","args":[["c","a","a"]],"ok":["c","a"]}"#,
            "\n",
            r#"{"fn":"joinSorted","args":[["a","a"]],"ok":"a"}"#,
            "\n",
            r#"{"fn":"maxNav","args":[[1,2,3,9]],"ok":9}"#,
            "\n",
            r#"{"fn":"joinSorted","args":[[null]],"ok":""}"#,
            "\n",
            r#"{"fn":"joinSorted","args":[["b","a"]],"ok":"b,a"}"#,
            "\n",
        ),
    )?;

    std::fs::write(
        run_dir.join("CheckTreeSetWire.java"),
        r#"import rustanything.RustAnythingWire;
import java.util.TreeSet;
public class CheckTreeSetWire {
    public static void main(String[] args) {
        RustAnythingWire.Writer ok = new RustAnythingWire.Writer();
        ok.tag(1); ok.len(2); ok.str("a"); ok.str("b");
        RustAnythingWire.Reader okReader = new RustAnythingWire.Reader(ok.bytes());
        TreeSet<String> parsed = okReader.treeSet(okReader::str);
        if (!parsed.toString().equals("[a, b]")) {
            throw new AssertionError("unexpected sorted set: " + parsed);
        }
        RustAnythingWire.Writer dup = new RustAnythingWire.Writer();
        dup.tag(1); dup.len(2); dup.str("a"); dup.str("a");
        try {
            RustAnythingWire.Reader dupReader = new RustAnythingWire.Reader(dup.bytes());
            dupReader.linkedHashSet(dupReader::str);
            throw new AssertionError("expected a repeated element to be rejected");
        } catch (IllegalArgumentException expected) {
            if (!expected.getMessage().contains("duplicate")) {
                throw new AssertionError("unexpected error: " + expected);
            }
        }
        RustAnythingWire.Writer rev = new RustAnythingWire.Writer();
        rev.tag(1); rev.len(2); rev.str("b"); rev.str("a");
        try {
            RustAnythingWire.Reader revReader = new RustAnythingWire.Reader(rev.bytes());
            revReader.treeSet(revReader::str);
            throw new AssertionError("expected non-natural order to be rejected");
        } catch (IllegalArgumentException expected) {
            if (!expected.getMessage().contains("natural order")) {
                throw new AssertionError("unexpected error: " + expected);
            }
        }
    }
}
"#,
    )?;

    run(Command::new("javac").current_dir(&run_dir).args([
        "-d",
        ".",
        "RustAnythingDriver.java",
        "RustAnythingJson.java",
        "RustAnythingWire.java",
        "RustAnythingError.java",
        "CheckTreeSetWire.java",
        "acme/OrderedSets.java",
    ]))?;
    run(Command::new("java")
        .current_dir(&run_dir)
        .args(["-cp", ".", "CheckTreeSetWire"]))?;
    run(Command::new("java").current_dir(&run_dir).args([
        "-cp",
        ".",
        "rustanything.RustAnythingDriver",
        "diff",
        "targets.json",
        "cases.jsonl",
        "diff.json",
    ]))?;
    let diff: DiffOutcome = serde_json::from_slice(&std::fs::read(run_dir.join("diff.json"))?)?;
    assert_eq!((diff.passed, diff.failed), (4, 4), "{diff:?}");
    assert!(
        diff.mismatches
            .iter()
            .all(|m| m["actual"]["invalid"] == true),
        "{diff:?}"
    );
    Ok(())
}

#[test]
fn java_error_lookup_matches_across_json_typed_and_wire_paths() {
    if !common::have("java") || !common::have("javac") {
        return;
    }

    let root = std::env::temp_dir().join(format!("rust-anything-java-err-{}", std::process::id()));
    std::fs::create_dir_all(&root).unwrap();
    let result = check_error_lookup(&root);
    std::fs::remove_dir_all(root).unwrap();
    result.unwrap();
}

fn check_error_lookup(root: &Path) -> anyhow::Result<()> {
    std::fs::write(root.join("RustAnythingError.java"), ERROR_JAVA)?;
    std::fs::create_dir_all(root.join("com/example"))?;
    std::fs::write(
        root.join("com/example/TestException.java"),
        "package com.example;\n\
         public final class TestException extends RuntimeException {\n\
             public TestException(String message) { super(message); }\n\
         }\n",
    )?;
    std::fs::write(
        root.join("com/example/Outer.java"),
        "package com.example;\n\
         public final class Outer {\n\
             public static final class InnerException extends RuntimeException {\n\
                 public InnerException(String message) { super(message); }\n\
             }\n\
         }\n",
    )?;
    std::fs::write(
        root.join("com/example/NoMsgException.java"),
        "package com.example;\n\
         public final class NoMsgException extends RuntimeException {\n\
             public NoMsgException() { super(\"fixed\"); }\n\
         }\n",
    )?;
    std::fs::write(
        root.join("CheckErrors.java"),
        r#"import rustanything.RustAnythingError;

public class CheckErrors {
    private static void checkBoth(String type, String msg, Class<?> expected) {
        RuntimeException fromJson = RustAnythingError.from(type + ": " + msg);
        RuntimeException fromReal = new RustAnythingError(type, msg).real();
        RuntimeException fromSlot;
        try {
            String[] slot = RustAnythingError.slot();
            slot[0] = type;
            slot[1] = msg;
            RustAnythingError.check(slot);
            throw new AssertionError("expected exception for " + type);
        } catch (RuntimeException e) {
            fromSlot = e;
        }
        if (fromJson.getClass() != expected || fromReal.getClass() != expected || fromSlot.getClass() != expected) {
            throw new AssertionError("class mismatch for " + type + ": "
                + fromJson.getClass() + " / " + fromReal.getClass() + " / " + fromSlot.getClass()
                + " (expected " + expected + ")");
        }
        String expectedMessage = expected == RuntimeException.class ? type + ": " + msg : msg;
        if (!expectedMessage.equals(fromJson.getMessage())
            || !expectedMessage.equals(fromReal.getMessage())
            || !expectedMessage.equals(fromSlot.getMessage())) {
            throw new AssertionError("message mismatch for " + type + ": "
                + fromJson.getMessage() + " / " + fromReal.getMessage() + " / " + fromSlot.getMessage());
        }
    }

    public static void main(String[] args) {
        // Resolving: packaged, nested, and JDK exception packages with a (String) constructor.
        checkBoth("com.example.TestException", "bad", com.example.TestException.class);
        checkBoth("com.example.Outer$InnerException", "nested", com.example.Outer.InnerException.class);
        checkBoth("IllegalArgumentException", "bad", IllegalArgumentException.class);
        checkBoth("NoSuchElementException", "missing", java.util.NoSuchElementException.class);
        checkBoth("DateTimeException", "empty year", java.time.DateTimeException.class);
        checkBoth("java.time.DateTimeException", "empty year", java.time.DateTimeException.class);
        checkBoth("RejectedExecutionException", "busy", java.util.concurrent.RejectedExecutionException.class);

        // Non-resolving: checked, non-exception, missing (String) ctor, unknown, malformed.
        checkBoth("java.io.IOException", "checked", RuntimeException.class);
        checkBoth("java.lang.String", "not an exception", RuntimeException.class);
        checkBoth("com.example.NoMsgException", "no string ctor", RuntimeException.class);
        checkBoth("UncheckedIOException", "disk", RuntimeException.class);
        checkBoth("NoSuchException", "oops", RuntimeException.class);
        checkBoth("bad name", "msg", RuntimeException.class);
        checkBoth("com.example..Bad", "msg", RuntimeException.class);

        // No ": " separator.
        for (String raw : new String[] { "plain error", "IllegalArgumentException:bad", "bad input: 42" }) {
            RuntimeException plain = RustAnythingError.from(raw);
            if (plain.getClass() != RuntimeException.class || !raw.equals(plain.getMessage())) {
                throw new AssertionError("unexpected plain error for " + raw + ": " + plain);
            }
        }
    }
}
"#,
    )?;

    run(Command::new("javac").current_dir(root).args([
        "-d",
        ".",
        "RustAnythingError.java",
        "com/example/TestException.java",
        "com/example/Outer.java",
        "com/example/NoMsgException.java",
        "CheckErrors.java",
    ]))?;
    run(Command::new("java")
        .current_dir(root)
        .args(["-cp", ".", "CheckErrors"]))?;
    Ok(())
}

fn replay(root: &Path) -> anyhow::Result<DiffOutcome> {
    std::fs::write(root.join("RustAnythingDriver.java"), DRIVER_JAVA)?;
    std::fs::write(root.join("RustAnythingJson.java"), JSON_JAVA)?;
    std::fs::write(
        root.join("Port.java"),
        "public class Port { public static String value(int input) { return null; } }",
    )?;
    std::fs::write(root.join("targets.json"), r#"{"value":["Port","value"]}"#)?;
    std::fs::write(
        root.join("cases.jsonl"),
        r#"{"fn":"value","args":[1],"ok":null}
{"fn":"value","args":["previously accepted"],"ok":null}
"#,
    )?;

    run(Command::new("javac").current_dir(root).args([
        "-d",
        ".",
        "RustAnythingDriver.java",
        "RustAnythingJson.java",
        "Port.java",
    ]))?;
    run(Command::new("java").current_dir(root).args([
        "-cp",
        ".",
        "rustanything.RustAnythingDriver",
        "diff",
        "targets.json",
        "cases.jsonl",
        "diff.json",
    ]))?;
    Ok(serde_json::from_slice(&std::fs::read(
        root.join("diff.json"),
    )?)?)
}

fn run(cmd: &mut Command) -> anyhow::Result<()> {
    let output = cmd.output()?;
    anyhow::ensure!(
        output.status.success(),
        "{cmd:?} failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    Ok(())
}