rust-anything 1.5.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 and Java
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
//! The binary wire for Go and Java: values cross as compact bytes whose encoders and decoders
//! are generated per type on both sides, without JSON text or reflection.
//!
//! Layout, little endian: `I64` and `F64` as 8 bytes, `Bool` as 1 byte. `Str`, `List`, `Map`
//! and `Struct` start with a presence byte (0 for null/nil, 1 otherwise); strings then carry a
//! u32 length and UTF-8 bytes, lists and maps a u32 count and their items (maps as string key,
//! value). `Opt` is a presence byte and the value. `Tuple` (several results) is its items in order.

use std::fmt::Write as _;

/// A source-language type the wire carries. `native` is the type as the source spells it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Ty {
    I64(String),
    F64(String),
    Bool,
    Str,
    /// `array` marks a Java array (`int[]`) rather than a list.
    List {
        item: Box<Ty>,
        native: String,
        array: bool,
    },
    Map {
        value: Box<Ty>,
        native: String,
    },
    /// Fields: (source field name, JSON key, type).
    Struct {
        name: String,
        fields: Vec<(String, String, Ty)>,
    },
    Opt {
        inner: Box<Ty>,
        native: String,
    },
    Tuple(Vec<Ty>),
    // JS (TS) values: a JS number; a string as UTF-16 code units (lone surrogates survive);
    // `undefined`/`null`/value; an object whose fields travel by index in the writer's key
    // order (unknown keys by name); a string-keyed record; and any value, tagged by kind.
    Num,
    Str16,
    OptJs {
        inner: Box<Ty>,
        nullable: bool,
        undefinable: bool,
    },
    Obj(Vec<(String, Ty)>),
    Map16(Box<Ty>),
    Dyn,
}

/// The `wire::Schema` constant expression for a type, for `read_value`/`write_value`.
pub fn schema_expr(t: &Ty) -> String {
    match t {
        Ty::I64(_) => "wire::Schema::I64".into(),
        Ty::F64(_) => "wire::Schema::F64".into(),
        Ty::Bool => "wire::Schema::Bool".into(),
        Ty::Str => "wire::Schema::Str".into(),
        Ty::List { item, .. } => format!("wire::Schema::List(&{})", schema_expr(item)),
        Ty::Map { value, .. } => format!("wire::Schema::Map(&{})", schema_expr(value)),
        Ty::Opt { inner, .. } => format!("wire::Schema::Opt(&{})", schema_expr(inner)),
        Ty::Struct { fields, .. } => format!(
            "wire::Schema::Struct(&[{}])",
            fields
                .iter()
                .map(|(_, key, t)| format!("({key:?}, {})", schema_expr(t)))
                .collect::<Vec<_>>()
                .join(", ")
        ),
        Ty::Tuple(items) => format!(
            "wire::Schema::Tuple(&[{}])",
            items.iter().map(schema_expr).collect::<Vec<_>>().join(", ")
        ),
        Ty::Num => "wire::Schema::Num".into(),
        Ty::Str16 => "wire::Schema::Str16".into(),
        Ty::OptJs { inner, .. } => format!("wire::Schema::OptJs(&{})", schema_expr(inner)),
        Ty::Obj(fields) => format!(
            "wire::Schema::Obj(&[{}])",
            fields
                .iter()
                .map(|(key, t)| format!("({key:?}, {})", schema_expr(t)))
                .collect::<Vec<_>>()
                .join(", ")
        ),
        Ty::Map16(value) => format!("wire::Schema::Map16(&{})", schema_expr(value)),
        Ty::Dyn => "wire::Schema::Dyn".into(),
    }
}

fn generic_arg<'a>(rust: &'a str, head: &str) -> Option<&'a str> {
    let rust = rust.trim();
    let rest = rust
        .strip_prefix(head)
        .or_else(|| rust.strip_prefix(&format!("std::collections::{head}")))?;
    rest.strip_prefix('<')?.strip_suffix('>').map(str::trim)
}

fn map_value(rust: &str) -> Option<&str> {
    for head in ["HashMap", "BTreeMap", "IndexMap"] {
        if let Some(args) = generic_arg(rust, head) {
            let (key, value) = args.split_once(',')?;
            return (key.trim() == "String").then_some(value.trim());
        }
    }
    None
}

fn is_value(rust: &str) -> bool {
    matches!(rust.trim(), "serde_json::Value" | "Value")
}

/// A closure body: `r.f64()` stays as is, anything else is wrapped in `Ok(...)`.
fn body(read: &str) -> String {
    match read.strip_suffix('?') {
        Some(call) if !call.contains('?') => call.to_string(),
        _ => format!("Ok({read})"),
    }
}

/// A schema as an argument: `&S` for a static, `S.inner()` (already a reference) as is.
fn schema_ref(schema: &str) -> String {
    if schema.ends_with(')') {
        schema.to_string()
    } else {
        format!("&{schema}")
    }
}

/// Rust code reading a value of `t` into the port type `rust` from reader `r`; `None` when the
/// port type does not fit the wire type (the function then keeps the JSON bridge).
pub fn rust_read(t: &Ty, rust: &str, schema: &str) -> Option<String> {
    if is_value(rust) {
        return Some(format!("wire::read_value(r, {})?", schema_ref(schema)));
    }
    let rust = rust.trim();
    if *t == Ty::Dyn {
        // Any JS value into a typed port parameter: through a tagged value and serde.
        return Some(
            "serde_json::from_value(wire::read_dyn(r)?).map_err(|e| format!(\"TypeError: {e}\"))?"
                .into(),
        );
    }
    let structural = rust_read_structural(t, rust, schema);
    if structural.is_none() && rust.contains("Value") {
        return Some(format!(
            "serde_json::from_value(wire::read_value(r, {})?).map_err(|e| format!(\"TypeError: {{e}}\"))?",
            schema_ref(schema)
        ));
    }
    structural
}

fn rust_read_structural(t: &Ty, rust: &str, schema: &str) -> Option<String> {
    Some(match t {
        Ty::I64(_) if rust == "i64" => "r.i64()?".into(),
        Ty::F64(_) if rust == "f64" => "r.f64()?".into(),
        Ty::Bool if rust == "bool" => "r.bool()?".into(),
        Ty::Str if rust == "String" => "r.string()?".into(),
        Ty::Opt { inner, .. } => match generic_arg(rust, "Option") {
            Some(inner_rust) => format!(
                "r.opt(|r| {})?",
                body(&rust_read(inner, inner_rust, &format!("{schema}.inner()"))?)
            ),
            None => format!(
                "r.required(|r| {})?",
                body(&rust_read(inner, rust, &format!("{schema}.inner()"))?)
            ),
        },
        Ty::List { item, .. } => format!(
            "r.list(|r| {})?",
            body(&rust_read(
                item,
                generic_arg(rust, "Vec")?,
                &format!("{schema}.inner()")
            )?)
        ),
        Ty::Map { value, .. } => format!(
            "r.map(|r| {})?.into_iter().collect()",
            body(&rust_read(
                value,
                map_value(rust)?,
                &format!("{schema}.inner()")
            )?)
        ),
        Ty::Num if rust == "f64" => "r.f64()?".into(),
        Ty::Str16 if rust == "String" => "r.string16()?".into(),
        Ty::Str16 if rust == "Vec<u16>" => "r.units()?".into(),
        Ty::OptJs { inner, .. } => match generic_arg(rust, "Option") {
            Some(inner_rust) => format!(
                "r.opt_js(|r| {})?",
                body(&rust_read(inner, inner_rust, &format!("{schema}.inner()"))?)
            ),
            None => format!(
                "r.required_js(|r| {})?",
                body(&rust_read(inner, rust, &format!("{schema}.inner()"))?)
            ),
        },
        Ty::Map16(value) => format!(
            "r.map16(|r| {})?.into_iter().collect()",
            body(&rust_read(
                value,
                map_value(rust)?,
                &format!("{schema}.inner()")
            )?)
        ),
        _ => return None,
    })
}

/// Rust statements writing `expr` (a reference to the port type `rust`) as `t` into writer `w`.
pub fn rust_write(t: &Ty, rust: &str, expr: &str, schema: &str, depth: usize) -> Option<String> {
    if is_value(rust) {
        return Some(format!(
            "wire::write_value(w, {expr}, {})?;",
            schema_ref(schema)
        ));
    }
    if *t == Ty::Dyn {
        return Some(format!(
            "wire::write_dyn(w, &serde_json::to_value({expr}).map_err(|e| e.to_string())?);"
        ));
    }
    if rust.contains("Value") {
        if let Some(code) = rust_write_structural(t, rust, expr, schema, depth) {
            return Some(code);
        }
        return Some(format!(
            "wire::write_value(w, &serde_json::to_value({expr}).map_err(|e| e.to_string())?, {})?;",
            schema_ref(schema)
        ));
    }
    rust_write_structural(t, rust, expr, schema, depth)
}

fn rust_write_structural(
    t: &Ty,
    rust: &str,
    expr: &str,
    schema: &str,
    depth: usize,
) -> Option<String> {
    let rust = rust.trim();
    let x = format!("x{depth}");
    // A method receiver: `out` as is, `&out` in parentheses.
    let r = if expr.starts_with('&') {
        format!("({expr})")
    } else {
        expr.to_string()
    };
    Some(match t {
        Ty::I64(_) if rust == "i64" => format!("w.i64(*{expr});"),
        Ty::F64(_) if rust == "f64" => format!("w.f64(*{expr});"),
        Ty::Bool if rust == "bool" => format!("w.bool(*{expr});"),
        Ty::Str if rust == "String" => format!("w.string({expr});"),
        Ty::Opt { inner, .. } => {
            let inner_schema = format!("{schema}.inner()");
            match generic_arg(rust, "Option") {
                Some(inner_rust) => format!(
                    "match {expr} {{ Some({x}) => {{ w.tag(1); {} }} None => w.tag(0) }}",
                    rust_write(inner, inner_rust, &x, &inner_schema, depth + 1)?
                ),
                None => format!(
                    "w.tag(1); {}",
                    rust_write(inner, rust, expr, &inner_schema, depth + 1)?
                ),
            }
        }
        Ty::List { item, .. } => format!(
            "w.tag(1); w.count({r}.len()); for {x} in {r}.iter() {{ {} }}",
            rust_write(
                item,
                generic_arg(rust, "Vec")?,
                &x,
                &format!("{schema}.inner()"),
                depth + 1
            )?
        ),
        Ty::Map { value, .. } => format!(
            "w.tag(1); w.count({r}.len()); for (k{depth}, {x}) in {r}.iter() {{ w.string(k{depth}); {} }}",
            rust_write(
                value,
                map_value(rust)?,
                &x,
                &format!("{schema}.inner()"),
                depth + 1
            )?
        ),
        Ty::Num if rust == "f64" => format!("w.f64(*{expr});"),
        Ty::Str16 if rust == "String" => format!("w.string16({expr});"),
        Ty::Str16 if rust == "Vec<u16>" => format!("w.units({expr});"),
        Ty::OptJs {
            inner, undefinable, ..
        } => {
            let inner_schema = format!("{schema}.inner()");
            // `None` is `undefined`, or `null` for a `T | null` that cannot be undefined.
            let none = if *undefinable { 0 } else { 2 };
            match generic_arg(rust, "Option") {
                Some(inner_rust) => format!(
                    "match {expr} {{ Some({x}) => {{ w.tag(1); {} }} None => w.tag({none}) }}",
                    rust_write(inner, inner_rust, &x, &inner_schema, depth + 1)?
                ),
                None => format!(
                    "w.tag(1); {}",
                    rust_write(inner, rust, expr, &inner_schema, depth + 1)?
                ),
            }
        }
        Ty::Map16(value) => format!(
            "w.tag(1); w.count({r}.len()); for (k{depth}, {x}) in {r}.iter() {{ w.string16(k{depth}); {} }}",
            rust_write(
                value,
                map_value(rust)?,
                &x,
                &format!("{schema}.inner()"),
                depth + 1
            )?
        ),
        _ => return None,
    })
}

/// `src/wire.rs` of a generated crate: the reader, the writer, and schema-driven conversion of
/// `serde_json::Value` (for ports that keep structured values as JSON values).
pub const WIRE_RS: &str = r#"//! The binary wire between the source language and the port. Generated by rust-anything.

#![forbid(unsafe_code)]
#![allow(dead_code)]

use serde_json::{Map, Value, json};

/// A wire type, for converting `serde_json::Value`s.
pub enum Schema {
    I64,
    F64,
    Bool,
    Str,
    List(&'static Schema),
    Map(&'static Schema),
    Opt(&'static Schema),
    Struct(&'static [(&'static str, Schema)]),
    Tuple(&'static [Schema]),
    Num,
    Str16,
    OptJs(&'static Schema),
    Obj(&'static [(&'static str, Schema)]),
    Map16(&'static Schema),
    Dyn,
}

impl Schema {
    /// The item type of a list, map or option.
    pub const fn inner(&self) -> &Schema {
        match self {
            Self::List(s) | Self::Map(s) | Self::Opt(s) | Self::OptJs(s) | Self::Map16(s) => s,
            _ => self,
        }
    }
}

pub struct Reader<'a> {
    bytes: &'a [u8],
    at: usize,
}

const SHORT: &str = "TypeError: wire data ended early";

impl<'a> Reader<'a> {
    pub fn new(bytes: &'a [u8]) -> Self {
        Self { bytes, at: 0 }
    }

    fn take(&mut self, n: usize) -> Result<&'a [u8], String> {
        let end = self.at.checked_add(n).filter(|&e| e <= self.bytes.len()).ok_or(SHORT)?;
        let out = &self.bytes[self.at..end];
        self.at = end;
        Ok(out)
    }

    pub fn tag(&mut self) -> Result<u8, String> {
        Ok(self.take(1)?[0])
    }

    pub fn count(&mut self) -> Result<usize, String> {
        let b = self.take(4)?;
        Ok(u32::from_le_bytes([b[0], b[1], b[2], b[3]]) as usize)
    }

    pub fn i64(&mut self) -> Result<i64, String> {
        let b = self.take(8)?;
        Ok(i64::from_le_bytes(b.try_into().map_err(|_| SHORT)?))
    }

    pub fn f64(&mut self) -> Result<f64, String> {
        let b = self.take(8)?;
        Ok(f64::from_le_bytes(b.try_into().map_err(|_| SHORT)?))
    }

    pub fn bool(&mut self) -> Result<bool, String> {
        Ok(self.tag()? != 0)
    }

    /// A string; `None` for null.
    pub fn opt_string(&mut self) -> Result<Option<String>, String> {
        if self.tag()? == 0 {
            return Ok(None);
        }
        let n = self.count()?;
        Ok(Some(String::from_utf8_lossy(self.take(n)?).into_owned()))
    }

    pub fn string(&mut self) -> Result<String, String> {
        self.opt_string()?
            .ok_or_else(|| "NullPointerException: null string".to_string())
    }

    pub fn opt<T>(&mut self, item: impl FnOnce(&mut Self) -> Result<T, String>) -> Result<Option<T>, String> {
        if self.tag()? == 0 { Ok(None) } else { item(self).map(Some) }
    }

    pub fn required<T>(&mut self, item: impl FnOnce(&mut Self) -> Result<T, String>) -> Result<T, String> {
        self.opt(item)?
            .ok_or_else(|| "NullPointerException: null value".to_string())
    }

    /// A JS string as UTF-16 code units.
    pub fn units(&mut self) -> Result<Vec<u16>, String> {
        if self.tag()? == 0 {
            return Err("TypeError: expected a string".into());
        }
        let n = self.count()?;
        let bytes = self.take(n.checked_mul(2).ok_or(SHORT)?)?;
        Ok(bytes.as_chunks::<2>().0.iter().map(|b| u16::from_le_bytes(*b)).collect())
    }

    /// A JS string; lone surrogates become U+FFFD, as napi's own conversion does.
    pub fn string16(&mut self) -> Result<String, String> {
        Ok(String::from_utf16_lossy(&self.units()?))
    }

    /// A JS optional value: 0 `undefined`, 1 a value, 2 `null`.
    pub fn opt_js<T>(&mut self, item: impl FnOnce(&mut Self) -> Result<T, String>) -> Result<Option<T>, String> {
        if self.tag()? == 1 { item(self).map(Some) } else { Ok(None) }
    }

    pub fn required_js<T>(&mut self, item: impl FnOnce(&mut Self) -> Result<T, String>) -> Result<T, String> {
        self.opt_js(item)?
            .ok_or_else(|| "TypeError: expected a value, got undefined or null".to_string())
    }

    /// A string-keyed record (keys as JS strings).
    pub fn map16<T>(&mut self, mut item: impl FnMut(&mut Self) -> Result<T, String>) -> Result<Vec<(String, T)>, String> {
        if self.tag()? == 0 {
            return Ok(Vec::new());
        }
        let n = self.count()?;
        let mut out = Vec::with_capacity(n.min(1 << 16));
        for _ in 0..n {
            let key = self.string16()?;
            out.push((key, item(self)?));
        }
        Ok(out)
    }

    /// A list; nil reads as empty.
    pub fn list<T>(&mut self, mut item: impl FnMut(&mut Self) -> Result<T, String>) -> Result<Vec<T>, String> {
        if self.tag()? == 0 {
            return Ok(Vec::new());
        }
        let n = self.count()?;
        let mut out = Vec::with_capacity(n.min(1 << 16));
        for _ in 0..n {
            out.push(item(self)?);
        }
        Ok(out)
    }

    /// A map's entries; nil reads as empty.
    pub fn map<T>(&mut self, mut item: impl FnMut(&mut Self) -> Result<T, String>) -> Result<Vec<(String, T)>, String> {
        if self.tag()? == 0 {
            return Ok(Vec::new());
        }
        let n = self.count()?;
        let mut out = Vec::with_capacity(n.min(1 << 16));
        for _ in 0..n {
            let key = self.string()?;
            out.push((key, item(self)?));
        }
        Ok(out)
    }
}

#[derive(Default)]
pub struct Writer(pub Vec<u8>);

impl Writer {
    pub fn tag(&mut self, t: u8) {
        self.0.push(t);
    }

    pub fn count(&mut self, n: usize) {
        self.0.extend_from_slice(&u32::try_from(n).unwrap_or(u32::MAX).to_le_bytes());
    }

    pub fn i64(&mut self, v: i64) {
        self.0.extend_from_slice(&v.to_le_bytes());
    }

    pub fn f64(&mut self, v: f64) {
        self.0.extend_from_slice(&v.to_le_bytes());
    }

    pub fn bool(&mut self, v: bool) {
        self.0.push(u8::from(v));
    }

    pub fn string(&mut self, s: &str) {
        self.tag(1);
        self.count(s.len());
        self.0.extend_from_slice(s.as_bytes());
    }

    /// A JS string from a Rust one (well-formed: tag 1).
    pub fn string16(&mut self, s: &str) {
        self.tag(1);
        let at = self.0.len();
        self.count(0);
        let mut n = 0usize;
        for u in s.encode_utf16() {
            self.0.extend_from_slice(&u.to_le_bytes());
            n += 1;
        }
        self.0[at..at + 4].copy_from_slice(&u32::try_from(n).unwrap_or(u32::MAX).to_le_bytes());
    }

    /// A JS string from code units, lone surrogates included (tag 4).
    pub fn units(&mut self, units: &[u16]) {
        self.tag(4);
        self.count(units.len());
        for u in units {
            self.0.extend_from_slice(&u.to_le_bytes());
        }
    }
}

fn float_value(x: f64) -> Value {
    if x.is_nan() {
        json!({"$num": "NaN"})
    } else if x.is_infinite() {
        json!({"$num": if x > 0.0 { "Infinity" } else { "-Infinity" }})
    } else if x == 0.0 && x.is_sign_negative() {
        json!({"$num": "-0"})
    } else {
        json!(x)
    }
}

fn value_float(v: &Value) -> Option<f64> {
    match v {
        Value::Number(n) => n.as_f64(),
        Value::Object(o) => match o.get("$num")?.as_str()? {
            "NaN" => Some(f64::NAN),
            "Infinity" => Some(f64::INFINITY),
            "-Infinity" => Some(f64::NEG_INFINITY),
            "-0" => Some(-0.0),
            _ => None,
        },
        _ => None,
    }
}

/// Reads a value of `schema` as JSON: null for nil, `{"$num": ...}` for NaN and infinities.
pub fn read_value(r: &mut Reader, schema: &Schema) -> Result<Value, String> {
    Ok(match schema {
        Schema::I64 => json!(r.i64()?),
        Schema::F64 => float_value(r.f64()?),
        Schema::Bool => Value::Bool(r.bool()?),
        Schema::Str => r.opt_string()?.map_or(Value::Null, Value::String),
        Schema::Opt(inner) => r.opt(|r| read_value(r, inner))?.unwrap_or(Value::Null),
        Schema::List(item) => {
            if r.tag()? == 0 {
                Value::Null
            } else {
                let n = r.count()?;
                let mut out = Vec::with_capacity(n.min(1 << 16));
                for _ in 0..n {
                    out.push(read_value(r, item)?);
                }
                Value::Array(out)
            }
        }
        Schema::Map(item) => {
            if r.tag()? == 0 {
                Value::Null
            } else {
                let n = r.count()?;
                let mut out = Map::new();
                for _ in 0..n {
                    let key = r.string()?;
                    out.insert(key, read_value(r, item)?);
                }
                Value::Object(out)
            }
        }
        Schema::Struct(fields) => {
            if r.tag()? == 0 {
                Value::Null
            } else {
                let mut out = Map::new();
                for (key, field) in *fields {
                    out.insert((*key).to_string(), read_value(r, field)?);
                }
                Value::Object(out)
            }
        }
        Schema::Tuple(items) => Value::Array(items.iter().map(|s| read_value(r, s)).collect::<Result<_, _>>()?),
        Schema::Num => js_number(r.f64()?),
        Schema::Str16 => js_string(r.units()?),
        Schema::OptJs(inner) => match r.tag()? {
            1 => read_value(r, inner)?,
            2 => Value::Null,
            _ => json!({"$undefined": true}),
        },
        Schema::Obj(fields) => {
            let n = r.tag()?;
            let mut out = Map::new();
            for _ in 0..n {
                let index = usize::from(r.tag()?);
                match fields.get(index) {
                    Some((key, field)) => {
                        out.insert((*key).to_string(), read_value(r, field)?);
                    }
                    None => {
                        let key = r.string16()?;
                        out.insert(key, read_dyn(r)?);
                    }
                }
            }
            Value::Object(out)
        }
        Schema::Map16(item) => {
            if r.tag()? == 0 {
                Value::Null
            } else {
                let n = r.count()?;
                let mut out = Map::new();
                for _ in 0..n {
                    let key = r.string16()?;
                    out.insert(key, read_value(r, item)?);
                }
                Value::Object(out)
            }
        }
        Schema::Dyn => read_dyn(r)?,
    })
}

/// A JS number as JSON, as the JSON bridge reads it: integers as integers, markers for the rest.
fn js_number(x: f64) -> Value {
    if x.is_finite() && !(x == 0.0 && x.is_sign_negative()) && x.fract() == 0.0 && x.abs() < 9_007_199_254_740_992.0 {
        json!(x as i64)
    } else {
        float_value(x)
    }
}

/// A JS string as JSON: lone surrogates as `{"$utf16": [...]}`.
fn js_string(units: Vec<u16>) -> Value {
    match String::from_utf16(&units) {
        Ok(s) => Value::String(s),
        Err(_) => json!({"$utf16": units}),
    }
}

/// Any JS value, tagged: 0 undefined, 1 null, 2 false, 3 true, 4 number, 5 string, 6 array,
/// 7 object.
pub fn read_dyn(r: &mut Reader) -> Result<Value, String> {
    let units = |r: &mut Reader| -> Result<Vec<u16>, String> {
        let n = r.count()?;
        let bytes = r.take(n.checked_mul(2).ok_or(SHORT)?)?;
        Ok(bytes.as_chunks::<2>().0.iter().map(|b| u16::from_le_bytes(*b)).collect())
    };
    Ok(match r.tag()? {
        0 => json!({"$undefined": true}),
        1 => Value::Null,
        2 => Value::Bool(false),
        3 => Value::Bool(true),
        4 => js_number(r.f64()?),
        5 => js_string(units(r)?),
        6 => {
            let n = r.count()?;
            let mut out = Vec::with_capacity(n.min(1 << 16));
            for _ in 0..n {
                out.push(read_dyn(r)?);
            }
            Value::Array(out)
        }
        7 => {
            let n = r.count()?;
            let mut out = Map::new();
            for _ in 0..n {
                let key = String::from_utf16_lossy(&units(r)?);
                out.insert(key, read_dyn(r)?);
            }
            Value::Object(out)
        }
        t => return Err(format!("TypeError: unknown value tag {t}")),
    })
}

fn marker<'v>(v: &'v Value, key: &str) -> Option<&'v Value> {
    v.as_object().filter(|o| o.len() == 1).and_then(|o| o.get(key))
}

fn write_units16(w: &mut Writer, v: &Value) -> Option<()> {
    if let Value::String(s) = v {
        w.string16(s);
        return Some(());
    }
    let units: Vec<u16> = marker(v, "$utf16")?
        .as_array()?
        .iter()
        .map(|u| u.as_u64().and_then(|u| u16::try_from(u).ok()))
        .collect::<Option<_>>()?;
    w.units(&units);
    Some(())
}

/// Writes any JSON value as a tagged JS value; markers become what they stand for.
pub fn write_dyn(w: &mut Writer, v: &Value) {
    match v {
        Value::Null => w.tag(1),
        Value::Bool(b) => w.tag(if *b { 3 } else { 2 }),
        Value::Number(n) => {
            w.tag(4);
            w.f64(n.as_f64().unwrap_or(f64::NAN));
        }
        Value::String(s) => {
            w.tag(5);
            let units: Vec<u16> = s.encode_utf16().collect();
            w.count(units.len());
            for u in units {
                w.0.extend_from_slice(&u.to_le_bytes());
            }
        }
        Value::Array(items) => {
            w.tag(6);
            w.count(items.len());
            for x in items {
                write_dyn(w, x);
            }
        }
        Value::Object(o) => {
            if marker(v, "$undefined").is_some() {
                w.tag(0);
            } else if let Some(x) = value_float(v).filter(|_| marker(v, "$num").is_some()) {
                w.tag(4);
                w.f64(x);
            } else if let Some(units) = marker(v, "$utf16").and_then(Value::as_array) {
                w.tag(5);
                w.count(units.len());
                for u in units {
                    w.0.extend_from_slice(&u16::try_from(u.as_u64().unwrap_or(0xFFFD)).unwrap_or(0xFFFD).to_le_bytes());
                }
            } else {
                w.tag(7);
                w.count(o.len());
                for (k, x) in o {
                    let units: Vec<u16> = k.encode_utf16().collect();
                    w.count(units.len());
                    for u in units {
                        w.0.extend_from_slice(&u.to_le_bytes());
                    }
                    write_dyn(w, x);
                }
            }
        }
    }
}

/// The start of the error a result of the wrong shape raises; JS bridges answer it by running
/// the call through JSON instead.
pub const MISMATCH: &str = "TypeError: the port returned ";

fn mismatch(schema: &str, v: &Value) -> String {
    format!("{MISMATCH}{v} where the original returns {schema}")
}

/// Writes a JSON value as `schema`; the port's value must fit the original's type.
pub fn write_value(w: &mut Writer, v: &Value, schema: &Schema) -> Result<(), String> {
    match schema {
        Schema::I64 => w.i64(v.as_i64().or_else(|| v.as_f64().filter(|f| f.fract() == 0.0).map(|f| f as i64)).ok_or_else(|| mismatch("an integer", v))?),
        Schema::F64 => w.f64(value_float(v).ok_or_else(|| mismatch("a float", v))?),
        Schema::Bool => w.bool(v.as_bool().ok_or_else(|| mismatch("a bool", v))?),
        Schema::Str => match v {
            Value::Null => w.tag(0),
            Value::String(s) => w.string(s),
            _ => return Err(mismatch("a string", v)),
        },
        Schema::Opt(inner) => {
            if v.is_null() {
                w.tag(0);
            } else {
                w.tag(1);
                write_value(w, v, inner)?;
            }
        }
        Schema::List(item) => match v {
            Value::Null => w.tag(0),
            Value::Array(items) => {
                w.tag(1);
                w.count(items.len());
                for x in items {
                    write_value(w, x, item)?;
                }
            }
            _ => return Err(mismatch("a list", v)),
        },
        Schema::Map(item) => match v {
            Value::Null => w.tag(0),
            Value::Object(o) => {
                w.tag(1);
                w.count(o.len());
                for (k, x) in o {
                    w.string(k);
                    write_value(w, x, item)?;
                }
            }
            _ => return Err(mismatch("a map", v)),
        },
        Schema::Struct(fields) => match v {
            Value::Null => w.tag(0),
            Value::Object(o) => {
                w.tag(1);
                for (key, field) in *fields {
                    write_value(w, o.get(*key).unwrap_or(&Value::Null), field)?;
                }
            }
            _ => return Err(mismatch("a struct", v)),
        },
        Schema::Tuple(items) => match v {
            Value::Array(values) if values.len() == items.len() => {
                for (x, s) in values.iter().zip(*items) {
                    write_value(w, x, s)?;
                }
            }
            _ => return Err(mismatch("several results", v)),
        },
        Schema::Num => w.f64(value_float(v).ok_or_else(|| mismatch("a number", v))?),
        Schema::Str16 => write_units16(w, v).ok_or_else(|| mismatch("a string", v))?,
        Schema::OptJs(inner) => {
            if marker(v, "$undefined").is_some() {
                w.tag(0);
            } else if v.is_null() {
                w.tag(2);
            } else {
                w.tag(1);
                write_value(w, v, inner)?;
            }
        }
        Schema::Obj(fields) => {
            let o = v.as_object().ok_or_else(|| mismatch("an object", v))?;
            w.tag(u8::try_from(o.len()).map_err(|_| mismatch("an object of at most 255 keys", v))?);
            for (k, x) in o {
                match fields.iter().position(|(key, _)| key == k) {
                    Some(i) if i < 255 => {
                        w.tag(i as u8);
                        write_value(w, x, &fields[i].1)?;
                    }
                    _ => {
                        w.tag(255);
                        w.string16(k);
                        write_dyn(w, x);
                    }
                }
            }
        }
        Schema::Map16(item) => match v {
            Value::Null => w.tag(0),
            Value::Object(o) => {
                w.tag(1);
                w.count(o.len());
                for (k, x) in o {
                    w.string16(k);
                    write_value(w, x, item)?;
                }
            }
            _ => return Err(mismatch("a record", v)),
        },
        Schema::Dyn => write_dyn(w, v),
    }
    Ok(())
}
"#;

/// Rust statements that read every argument of a port function from `r`; `None` when one does
/// not fit.
pub fn rust_read_args(
    params: &[(String, Ty)],
    port_params: &[(String, String)],
    schema_prefix: &str,
    statics: &mut String,
) -> Option<String> {
    let mut out = String::new();
    for (i, ((_, ty), (name, rust))) in params.iter().zip(port_params).enumerate() {
        let schema = format!("{schema_prefix}_{i}");
        let read = rust_read(ty, rust, &schema)?;
        if read.contains(&schema) {
            let _ = writeln!(
                statics,
                "static {schema}: wire::Schema = {};",
                schema_expr(ty)
            );
        }
        let _ = writeln!(out, "        let {name}: {rust} = {read};");
    }
    Some(out)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn list(item: Ty) -> Ty {
        Ty::List {
            item: Box::new(item),
            native: String::new(),
            array: false,
        }
    }

    #[test]
    fn reads_fit_the_port_types() {
        let strings = list(Ty::Str);
        assert_eq!(
            rust_read(&strings, "Vec<String>", "S").unwrap(),
            "r.list(|r| r.string())?"
        );
        let boxed = Ty::Opt {
            inner: Box::new(Ty::I64("int".into())),
            native: "Integer".into(),
        };
        assert_eq!(
            rust_read(&boxed, "i64", "S").unwrap(),
            "r.required(|r| r.i64())?"
        );
        assert_eq!(
            rust_read(&boxed, "Option<i64>", "S").unwrap(),
            "r.opt(|r| r.i64())?"
        );
        assert_eq!(
            rust_read(&strings, "serde_json::Value", "S").unwrap(),
            "wire::read_value(r, &S)?"
        );
        // A port type that does not fit the wire type keeps the JSON bridge.
        assert!(rust_read(&strings, "Vec<i64>", "S").is_none());
    }

    #[test]
    fn writes_fit_the_port_types() {
        let floats = list(Ty::F64("double".into()));
        assert_eq!(
            rust_write(&floats, "Vec<f64>", "out", "S", 0).unwrap(),
            "w.tag(1); w.count(out.len()); for x0 in out.iter() { w.f64(*x0); }"
        );
        assert_eq!(
            schema_expr(&floats),
            "wire::Schema::List(&wire::Schema::F64)"
        );
    }
}