fortformat 0.1.2

Parse Fortran format strings and formatted data
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
//! Serialize data according to a Fortran format string.
//!
//! # Basic usage
//!
//! This module expects that you have a Fortran format string, such as
//! `(a10,i3,f8.2)` and want to serialize data according to that format.
//! The functions provided by this module belong to three groups: `to_*`,
//! `to_*_with_fields`, `to_*_custom`. The `to_*_custom` functions are the
//! most flexible but the least convenient. You only need to use them if
//! the default [`SerSettings`] is not sufficient for your use case.
//!
//! The distinction between the `to_*` and `to_*_with_fields` functions
//! mainly matters when serializing structures or maps. The `to_*` functions
//! will output values in the order they are defined. That is, this example
//! will work, because the fields in `Person` are defined in the same order
//! as they appear in the data:
//!
//! ```
//! use fortformat::format_specs::FortFormat;
//! use fortformat::ser::to_string;
//!
//! #[derive(Debug, PartialEq, serde::Serialize)]
//! struct Person {
//!     name: &'static str,
//!     age: i32,
//!     weight: f32,
//! }
//!
//! let ff = FortFormat::parse("(a10,i3,f8.1)").unwrap();
//! let p = Person { name: "John Doe", age: 30, weight: 180.5 };
//! let s = to_string(p, &ff).unwrap();
//! assert_eq!(s, "  John Doe 30   180.5");
//! ```
//!
//! However, the next example will *not* work, because the field order does not match
//! the data:
//!
//! ```
//! # use fortformat::format_specs::FortFormat;
//! # use fortformat::ser::to_string;
//! #
//! # #[derive(Debug, PartialEq, serde::Serialize)]
//! # struct Person {
//! #     name: String,
//! #     age: i32,
//! #     weight: f32,
//! # }
//! let ff = FortFormat::parse("(f8.1,i3,1x,a10)").unwrap();
//! let p = Person { name: "John Doe".to_string(), age: 30, weight: 180.5 };
//! let res = to_string(p, &ff);
//! assert!(res.is_err())
//! ```
//!
//! If the order in which the fields need to be output is different from the order
//! the fields are defined in the structure, then you need to use one of the `*_with_fields`
//! functions:
//!
//! ```
//! # use fortformat::format_specs::FortFormat;
//! # use fortformat::ser::to_string_with_fields;
//! #
//! # #[derive(Debug, PartialEq, serde::Serialize)]
//! # struct Person {
//! #     name: String,
//! #     age: i32,
//! #     weight: f32,
//! # }
//! let ff = FortFormat::parse("(f8.1,i3,a10)").unwrap();
//! let p = Person { name: "John Doe".to_string(), age: 30, weight: 180.5 };
//! let fields = ["weight", "age", "name"];
//! let s = to_string_with_fields(p, &ff, &fields).unwrap();
//! assert_eq!(s, "   180.5 30  John Doe");
//! ```
//!
//! **Note: take special care with maps.** Most maps do not guarantee
//! the order their values are visited, so using one of the serialization
//! functions that doesn't take field names will usually result in undetermined
//! order in which the data are written out.
//!
//! # Serializing nested structures
//!
//! Since Fortran formatted data does not have a way to represent nesting, this
//! can be a little tricky. If relying on the order of structure fields, then you
//! do not need to do anything special - the serializer will correctly recognize
//! that only the bool, int, float, char, and str/String fields map to format
//! fields:
//!
//! ```
//! # use fortformat::format_specs::FortFormat;
//! # use fortformat::ser::to_string;
//!
//! #[derive(Debug, PartialEq, serde::Serialize)]
//! struct SiteInfo {
//!     name: String,
//!     coords: Coordinates
//! }
//!
//! #[derive(Debug, PartialEq, serde::Serialize)]
//! struct Coordinates {
//!     longitude: f32,
//!     latitude: f32,
//! }
//!
//! let site = SiteInfo {
//!     name: "Ferris Island".to_string(),
//!     coords: Coordinates { longitude: 1.0, latitude: -5.5 }
//! };
//!
//! let ff = FortFormat::parse("(a15,2f5.1)").unwrap();
//! let s = to_string(&site, &ff).unwrap();
//! assert_eq!(s, "  Ferris Island  1.0 -5.5");
//! ```
//!
//! However, the following example will return an error from the `to_string` function:
//!
//! ```
//! # use fortformat::format_specs::FortFormat;
//! # use fortformat::ser::to_string_with_fields;
//! #
//! # #[derive(Debug, PartialEq, serde::Serialize)]
//! # struct SiteInfo {
//! #     name: String,
//! #     coords: Coordinates
//! # }
//! #
//! # #[derive(Debug, PartialEq, serde::Serialize)]
//! # struct Coordinates {
//! #     longitude: f32,
//! #     latitude: f32,
//! # }
//!
//! let site = SiteInfo {
//!     name: "Ferris Island".to_string(),
//!     coords: Coordinates { longitude: 1.0, latitude: -5.5 }
//! };
//!
//! let ff = FortFormat::parse("(a15,2f5.1)").unwrap();
//! let fields = ["latitude", "longitude", "name"];
//! let res = to_string_with_fields(&site, &ff, &fields);
//! assert!(res.is_err());
//! ```
//!
//! Specifically, you would see an error that the "coords" field is missing.
//! That is because, without the clue from the `#[serde(flatten)]` attribute
//! that `coords` will not be one of the Fortran fields, the serializer can't
//! tell that ahead of time. Adding `#[serde(flatten)]` to the `coords` field
//! will fix this:
//!
//! ```
//! # use fortformat::format_specs::FortFormat;
//! # use fortformat::ser::to_string_with_fields;
//!
//! #[derive(Debug, PartialEq, serde::Serialize)]
//! struct SiteInfo {
//!     name: String,
//!     #[serde(flatten)]
//!     coords: Coordinates
//! }
//!
//! #[derive(Debug, PartialEq, serde::Serialize)]
//! struct Coordinates {
//!     longitude: f32,
//!     latitude: f32,
//! }
//!
//! let site = SiteInfo {
//!     name: "Ferris Island".to_string(),
//!     coords: Coordinates { longitude: 1.0, latitude: -5.5 }
//! };
//!
//! let ff = FortFormat::parse("(2f5.1,a15)").unwrap();
//! let fields = ["latitude", "longitude", "name"];
//! let s = to_string_with_fields(&site, &ff, &fields).unwrap();
//! assert_eq!(s, " -5.5  1.0  Ferris Island");
//! ```
//!
//! You can also use `#[serde(flatten)]` on maps:
//!
//! ```
//! # use std::collections::HashMap;
//! # use fortformat::format_specs::FortFormat;
//! # use fortformat::ser::to_string_with_fields;
//!
//! #[derive(Debug, PartialEq, serde::Serialize)]
//! struct SiteInfo {
//!     name: String,
//!     #[serde(flatten)]
//!     concentrations: HashMap<&'static str, f32>
//! }
//!
//! let concentrations = HashMap::from([
//!     ("co2", 413.5),
//!     ("ch4", 1899.3),
//! ]);
//!
//! let site = SiteInfo{ name: "Mauna Loa".to_string(), concentrations };
//! let ff = FortFormat::parse("(a12,10f8.1)").unwrap();
//! let fields = ["name", "co2", "ch4"];
//! let s = to_string_with_fields(&site, &ff, &fields).unwrap();
//! assert_eq!(s, "   Mauna Loa   413.5  1899.3");
//! ```
//!
//! Note in this last example that the number of fields in the format string, `(a12,10f8.1)`,
//! is larger than the number of fields actually written. This is fine, and is a useful way
//! to provide a format string that can handle a sequence of an unknown length as the last
//! thing to be serialized: just make the format string long enough to cover the largest
//! possible number of elements.
//!
//! # Field names, tuples, vectors, or other sequences
//!
//! If using field names, there must be one for each concrete value serialized. Consider
//! this example:
//!
//! ```
//! # use fortformat::format_specs::FortFormat;
//! # use fortformat::ser::to_string_with_fields;
//!
//! #[derive(Debug, PartialEq, serde::Serialize)]
//! struct Coordinates {
//!     longitude: f32,
//!     latitude: f32,
//! }
//!
//! let value = ("Ferris Island", Coordinates{ longitude: 1.0, latitude: -5.5 });
//! let ff = FortFormat::parse("(a13,1x,f5.1,f5.1)").unwrap();
//! let fields = ["latitude", "longitude"];
//! let res = to_string_with_fields(value, &ff, &fields);
//! assert!(res.is_err());
//! ```
//!
//! This fails because although "latitude" and "longitude" are our only two named fields
//! in `Coordinates`, the serializer needs one field name for each non-positional format
//! spec in the format string. (Positional specs are ones like `x` that add space but
//! do not represent a value.) Here, the string that is the first tuple element must have
//! a field, even if it is a placeholder. The following will work:
//!
//! ```
//! # use fortformat::format_specs::FortFormat;
//! # use fortformat::ser::to_string_with_fields;
//! #
//! # #[derive(Debug, PartialEq, serde::Serialize)]
//! # struct Coordinates {
//! #     longitude: f32,
//! #     latitude: f32,
//! # }
//! #
//! let value = ("Ferris Island", Coordinates{ longitude: 1.0, latitude: -5.5 });
//! let ff = FortFormat::parse("(a13,1x,f5.1,f5.1)").unwrap();
//! let fields = ["", "latitude", "longitude"];
//! let s = to_string_with_fields(value, &ff, &fields).unwrap();
//! assert_eq!(s, "Ferris Island  -5.5  1.0");
//! ```
//!
//! Another potentially confusing case is when you have a vector of the same structure
//! repeated and you pass field names. In the example below, you might expect that the
//! serializer could get mixed up by the repeated field names. However, this works
//! correctly:
//!
//! ```
//! # use fortformat::format_specs::FortFormat;
//! # use fortformat::ser::to_string_with_fields;
//! #
//! # #[derive(Debug, PartialEq, serde::Serialize)]
//! # struct Coordinates {
//! #     longitude: f32,
//! #     latitude: f32,
//! # }
//! let value = vec![
//!     Coordinates{ longitude: 1.0, latitude: -5.5 },
//!     Coordinates{ longitude: -45.0, latitude: 60.0 }
//! ];
//! let ff = FortFormat::parse("(4f6.1)").unwrap();
//! let fields = ["latitude", "longitude", "latitude", "longitude"];
//! let s = to_string_with_fields(value, &ff, &fields).unwrap();
//!
//! assert_eq!(s, "  -5.5   1.0  60.0 -45.0");
//! ```
//!
//! The reason is that the serializer internally tracks which fields it has written
//! and always uses the next field with the correct name. So in this example the process
//! is:
//!
//! 1. Take the first element of `value`, it is a struct, so take the first field `longitude`.
//! 2. We have not yet serialized any values, so our format and field indices both are 0.
//! 3. Starting from `fields[0]` find the first index where our current struct field, "longitude",
//!    is present. This is index 1, so serialize the value using the format spec at index 1 and hold
//!    this in memory.
//!    (If positional format specs like `1x` are present, they are ignored while counting the index.)
//! 4. Repeat for the other field on our field element.
//! 5. The first struct is fully serialized, write the bytes in the proper order (latitude first, them
//!    longitude.)
//! 6. Since we serialized two fields, increment the field and format index by 2.
//! 7. Take the second element of `value`, which is also a struct, and take its first field `longitude`.
//! 8. Look for where "longitude" shows up in the list of `fields`, *starting from index 2*. This is at
//!    index 3, so the longitude value will be serialized using the format at index 3 and written to the
//!    corresponding position in the output.
//!
//! At present, there is no way to tell it to "loop" the field names (i.e. you cannot pass just
//! `["latitude", "longitude"]` for `fields` in the above example - it must have 4 elements matching
//! 4 total fields).
//!
//! # Strings and the format spec
//!
//! The Fortran `CHARACTER` type is essentially an alias for `byte`, as Fortran was not
//! designed with multi-byte characters in mind. Thus, the character format `aN` means
//! that field can only hold *N* bytes, which may be fewer than *N* Unicode characters.
//! If your output strings contain non-ASCII characters and are being unexpectedly truncated,
//! this could be why.
//!
//! # Enum variants and the format spec
//!
//! Enum variants can be identified either by their name or index. This crate allows either,
//! and will automatically select which one to use based on the format string:
//!
//! ```
//! # use fortformat::format_specs::FortFormat;
//! # use fortformat::ser::to_string;
//! #[derive(serde::Serialize)]
//! enum Test {
//!     Alpha,
//!     Beta
//! }
//!
//! // If the format spec for the variant is an integer, then the
//! // variant index will be written.
//! let value = [Test::Alpha, Test::Beta];
//! let ff1 = FortFormat::parse("(i1,1x,i1)").unwrap();
//! let s1 = to_string(&value, &ff1).unwrap();
//! assert_eq!(s1, "0 1");
//!
//! // If the format spec for the variant is a character array,
//! // then the format name is written instead.
//! let ff2 = FortFormat::parse("(a5,1x,a5)").unwrap();
//! let s2 = to_string(&value, &ff2).unwrap();
//! assert_eq!(s2, "Alpha  Beta");
//! ```
//!
//! Variants containing values are also supported, but will run into the limits of Fortran
//! formatted output. Given an enum whose variants both contain a similar type:
//!
//! ```
//! # use fortformat::format_specs::FortFormat;
//! # use fortformat::ser::to_string;
//! #[derive(serde::Serialize)]
//! enum Test {
//!     Alpha(i32),
//!     Beta(u16)
//! }
//!
//! let value = [Test::Alpha(-99), Test::Beta(42)];
//! let ff = FortFormat::parse("2(a5,1x,i4,1x)").unwrap();
//! let s = to_string(&value, &ff).unwrap();
//! assert_eq!(s, "Alpha  -99  Beta   42");
//! ```
//!
//! this will work for any value of `Test`, because both variants can be serialized as the
//! variant name (or index) and an integer value. However, if you had an enum like:
//!
//! ```no_run
//! enum InstrumentValue {
//!     Nothing,
//!     Value(f32),
//!     ValueWithError(f32, f32)
//! }
//! ```
//!
//! it's clear that the different variants correspond to different formats, e.g.:
//!
//! - `Nothing` = `(a14)`
//! - `Value` = `(a14,f13.5)`
//! - `ValueWithError` = `(a14,f13.5,f13.5)`
//!
//! One way way around this would be to use the `into` attribute to tell serde
//! to convert the enum into some type with a common representation
//!
//! ```
//! # use fortformat::format_specs::FortFormat;
//! # use fortformat::ser::to_string;
//! #[derive(Debug, Clone, serde::Serialize)]
//! #[serde(into = "(&'static str, f32, f32)")]
//! enum InstrumentValue {
//!     Nothing,
//!     Value(f32),
//!     ValueWithError(f32, f32)
//! }
//!
//! // Convert the enum into its name and two floats - even if the variant
//! // doesn't have two floats. Use a fill value for the float(s) not present.
//! impl From<InstrumentValue> for (&'static str, f32, f32) {
//!     fn from(value: InstrumentValue) -> Self {
//!         match value {
//!             InstrumentValue::Nothing => ("Nothing", -999.0, -999.0),
//!             InstrumentValue::Value(v) => ("Value", v, -999.0),
//!             InstrumentValue::ValueWithError(v, e) => ("Val+Err", v, e),
//!         }
//!     }
//! }
//!
//! let values = [
//!     InstrumentValue::Nothing,
//!     InstrumentValue::Value(1.0),
//!     InstrumentValue::ValueWithError(2.0, 0.2)
//! ];
//!
//! // Because we included the #[serde(into = "...")] attribute on the enum,
//! // when serde goes to serialize it, it first gets converted to a string
//! // and two floats, and those are serialized. This allows us to use
//! // (a7,f8.3,f8.3) for any variant.
//! let ff = FortFormat::parse("(3(a7,1x,f8.3,1x,f8.3,1x))").unwrap();
//! let s = to_string(values, &ff).unwrap();
//!
//! assert_eq!(s, "Nothing -999.000 -999.000   Value    1.000 -999.000 Val+Err    2.000    0.200");
//! ```
//!
//! If you need to be able to deserialize such a value, you would probably also want to include the
//! `#[serde(try_from = "...")]` attribute.
//!
//! ## Enum representations
//!
//! For now, the different representations of enums are partially supported, but will all return the
//! same result. Using internal or adjacent tagging on types passed to one of the `*_with_fields`
//! functions will not correctly place the tag value in the correct place given the field names.
//! That is, given the following example:
//!
//! ```
//! # use fortformat::format_specs::FortFormat;
//! # use fortformat::ser::{to_string,to_string_with_fields};
//! #[derive(Debug, serde::Serialize)]
//! #[serde(tag = "type")]
//! enum Internal {
//!     Alpha{value: i32},
//!     Beta{value: i32},
//! }
//!
//! let in_vec = vec![Internal::Alpha{value: 12}, Internal::Beta{value: 24}];
//! let in_ff = FortFormat::parse("(2(a6,i3))").unwrap();
//! let in_s = to_string(&in_vec, &in_ff).unwrap();
//! assert_eq!(in_s, " Alpha 12  Beta 24");
//!
//! let fields = ["type", "value"];
//! let ff = FortFormat::parse("(i2,1x,a5)").unwrap();
//! let res = to_string_with_fields(
//!     Internal::Alpha { value: 42 },
//!     &ff,
//!     &fields
//! );
//! assert!(res.is_err());
//! ```
//!
//! you will get an error because it still tries to serialize the variant first. This is not
//! the desired behavior, and may be fixed in a future version.
//!
//! # None and unit types
//!
//! Since Fortran does not have a good way to represent types containing no data, there was not an
//! single, clear way to handle `None` values, the unit `()`, and unit structures (e.g. structures
//! with no values inside them). Our current approach is to write all of these types as fill values.
//! The logic was:
//!
//! 1. Fill values are the closest thing to an "optional" type in Fortran, so are the least bad way
//!    to communicate a `None` value.
//! 2. Unit types still need a valid placeholder to fill their space in the text input/output, so
//!    again a fill value is the least bad way to handle this.
//!
//! The default "fill value" mimics Fortran's behavior when a value overflows its field width -
//! in such a case, the field is filled with `*`s. Since such a value is interpreted by Fortran
//! as a non-number (for numeric fields), this is again the least-bad default in our opinion.
//! You can change how fill values are generated using one of the `*_custom` methods with a
//! [`SerSettings`] instance. Fill value format is defined by a [`NoneFill`] enum contained
//! in [`SerSettings`]; see the [`NoneFill`] documentation for the available choices.
use std::{
    fmt::{Octal, UpperHex},
    io::Write,
    string::FromUtf8Error,
    sync::Arc,
};

use crate::{
    de::FortFormat,
    format_specs::{FortField, IntBase, RealFmt},
    serde_common::{NoneFill, SError, SResult},
};
use ryu_floating_decimal::d2d;
use serde::ser;

// TODO: debug why settings for left align aren't carrying through structures properly
// (collate results example)
static DEFAULT_SER_SETTINGS: SerSettings = SerSettings {
    fill_method: NoneFill::default_inner(),
    newline: b"\n",
    align_left_str: false,
};

/// Serialize a value into a string using the given Fortran format.
///
/// When serializing structures or maps using this function, the order of the fields
/// or values is the order in which they will be serialized. This must match the order
/// of the type specifications in the format. Note that because most map types do not
/// provide a consistent order, it is therefore not recommended to use this function
/// when serializing a map or anything containing a map. Use [`to_string_with_fields`]
/// instead.
///
/// In addition to erroring if the serialization fails (see [`to_bytes`] for possible
/// causes), this function will error in the unlikely case where the serialized bytes
/// cannot be interpreted as valid UTF-8.
pub fn to_string<T>(value: T, fmt: &FortFormat) -> SResult<String>
where
    T: ser::Serialize,
{
    let mut serializer = Serializer::<_, &str>::new(fmt);
    value.serialize(&mut serializer)?;
    Ok(serializer.try_into_string()?)
}

/// Serialize a value into a string using the given Fortran format and a list of field names.
///
/// When serializing a structure or map, the fields/values will be written out in the order
/// that their names appear in `fields`. (For maps with non-string keys, the serialized version
/// of each key is matched against `fields`.) See the module-level documentation for how this
/// works when a struct/map is part of a tuple.
///
/// Like [`to_string`], this will return an error if serialization fails or if the resulting
/// bytes cannot be converted to UTF-8.
pub fn to_string_with_fields<T, F>(value: T, fmt: &FortFormat, fields: &[F]) -> SResult<String>
where
    T: ser::Serialize,
    F: AsRef<str>,
{
    let mut serializer = Serializer::new_with_fields(fmt, fields);
    value.serialize(&mut serializer)?;
    Ok(serializer.try_into_string()?)
}

/// Serialize a value into a string with full control over how the serialization is done.
///
/// Use this method if you need to pass custom settings. See [`SerSettings`] for available
/// options. Pass `None` for `fields` if there are no field names to match up. Note that
/// calling without fields will require annotating the type F for the fields, so you would
/// call this as `to_string_custom::<_, &str>(...)` in that case.
pub fn to_string_custom<T, F>(
    value: T,
    fmt: &FortFormat,
    fields: Option<&[F]>,
    settings: &SerSettings,
) -> SResult<String>
where
    T: ser::Serialize,
    F: AsRef<str>,
{
    let mut serializer = Serializer::new_custom(fmt, fields, settings);
    value.serialize(&mut serializer)?;
    Ok(serializer.try_into_string()?)
}

/// Serialize a value into a sequence of bytes using the given Fortran format.
///
/// This is the same as [`to_string`], except the resulting bytes are directly
/// returned, rather than being converted to UTF-8 first. If you don't need
/// a string, this avoids the possible error from non-UTF8 bytes.
///
/// This function will return an error if serialization fails. Some common reasons
/// for this are:
///
/// - The next Rust type does not match the next Fortran type in the format spec.
/// - The type `T` has nested structures (a struct with another struct or map as a field)
///   and does not use the `#[serde(flatten)]` attribute on the nested struct or map.
/// - The format spec is too short, and ends before the values to be serialized do.
///
/// These cases are represented by the variants of [`SError`].
pub fn to_bytes<T>(value: T, fmt: &FortFormat) -> SResult<Vec<u8>>
where
    T: ser::Serialize,
{
    let mut serializer = Serializer::<_, &str>::new(fmt);
    value.serialize(&mut serializer)?;
    Ok(serializer.into_bytes())
}

/// Serialize a value into a sequence of bytes using the given Fortran format and a list of field names.
///
/// This works identically to [`to_string_with_fields`] except the serialized bytes are returned directly,
/// rather than being converted to a UTF-8 string.
///
/// An additional reason compared to [`to_bytes`] that this function might error is if the list of field
/// names is shorter than the number of concrete values to serialize.
pub fn to_bytes_with_fields<T, F: AsRef<str>>(
    value: T,
    fmt: &FortFormat,
    fields: &[F],
) -> SResult<Vec<u8>>
where
    T: ser::Serialize,
{
    let mut serializer = Serializer::new_with_fields(fmt, fields);
    value.serialize(&mut serializer)?;
    Ok(serializer.into_bytes())
}

/// Serialize a value into a sequence of bytes with full control over how the serialization is done.
///
/// Use this method if you need to pass custom settings. See [`SerSettings`] for available
/// options. Pass `None` for `fields` if there are no field names to match up.  Note that
/// calling without fields will require annotating the type F for the fields, so you would
/// call this as `to_string_custom::<_, &str>(...)` in that case.
pub fn to_bytes_custom<T, F: AsRef<str>>(
    value: T,
    fmt: &FortFormat,
    fields: Option<&[F]>,
    settings: &SerSettings,
) -> SResult<Vec<u8>>
where
    T: ser::Serialize,
{
    let mut serializer = Serializer::new_custom(fmt, fields, settings);
    value.serialize(&mut serializer)?;
    Ok(serializer.into_bytes())
}

/// Serialize a value directly to a writer using the given Fortran format.
///
/// This is a more convenient way to write to e.g. a file than using [`to_bytes`]
/// to get the byte array first, and writing that. It will also automatically
/// append a newline at the end of the record so that the writer is ready
/// for the next record.
///
/// In addition to possible serialization errors, this will return an error if
/// writing fails.
///
/// # Notes:
///
/// 1. It is *strongly* encouraged to use some form of buffered writer
///    for `writer`, as the serializer often writes single bytes due to the
///    need to match Fortran-style numbers. Without buffering, this could result
///    in very slow output.
/// 2. Often in Fortran a format spec will represent one data record and there
///    will be one data record per line. This method does *not* automatically
///    add a newline at the end of a record; you will need to do so by calling
///    `writer.write(b"\n")` or something equivalent.
pub fn to_writer<T, W>(value: T, fmt: &FortFormat, writer: W) -> SResult<()>
where
    T: ser::Serialize,
    W: Write,
{
    let mut serializer = Serializer::<_, &str>::new_writer(fmt, writer);
    value.serialize(&mut serializer)?;
    serializer.end_record()?;
    Ok(())
}

/// Serialize a value directly to a writer using the given Fortran format and a list of field names.
///
/// This is the equivalent of [`to_bytes_with_fields`] but writes directly to something
/// implementing [`std::io::Write`]. The same notes given for [`to_writer`] apply.
pub fn to_writer_with_fields<T, W, F: AsRef<str>>(
    value: T,
    fmt: &FortFormat,
    fields: &[F],
    writer: W,
) -> SResult<()>
where
    T: ser::Serialize,
    W: Write,
{
    let mut serializer = Serializer::new_writer_with_fields(fmt, fields, writer);
    value.serialize(&mut serializer)?;
    serializer.end_record()?;
    Ok(())
}

/// Serialize a value directly to a writer with full control over how the serialization is done.
///
/// Use this method if you need to pass custom settings. See [`SerSettings`] for available
/// options. Pass `None` for `fields` if there are no field names to match up.  Note that
/// calling without fields will require annotating the type F for the fields, so you would
/// call this as `to_string_custom::<_, &str>(...)` in that case. You can change the newline
/// written to the end of the recrod with the [`SerSettings`] instance.
pub fn to_writer_custom<T, W, F: AsRef<str>>(
    value: T,
    fmt: &FortFormat,
    fields: Option<&[F]>,
    settings: &SerSettings,
    writer: W,
) -> SResult<()>
where
    T: ser::Serialize,
    W: Write,
{
    let mut serializer = Serializer::new_writer_custom(fmt, fields, settings, writer);
    value.serialize(&mut serializer)?;
    serializer.end_record()?;
    Ok(())
}

/// Write many sets of values with the same format, one per line.
///
/// This will use the format `fmt` to write each of the elements in `values`.
/// Each one will be separated by the newline character defined in `settings`,
/// which by default is `\n`.
///
/// If you have many values to write already collected in a sequence, this function
/// should be more efficient than repeated calls to one of the `to_writer*` functions,
/// as it won't have to rebuild the serializer for each line. However, if you only
/// have a lazy iterator over your values, you will need to call one of the `to_writer*`
/// functions for each element.
pub fn many_to_writer_custom<T, W, F>(
    values: &[T],
    fmt: &FortFormat,
    fields: Option<&[F]>,
    settings: &SerSettings,
    writer: W,
) -> SResult<()>
where
    T: ser::Serialize,
    W: Write,
    F: AsRef<str>,
{
    let mut serializer = Serializer::new_writer_custom(fmt, fields, settings, writer);
    for val in values.iter() {
        val.serialize(&mut serializer)?;
        serializer.end_record()?;
    }
    Ok(())
}

/// Settings for serialization.
///
/// This can be used with the `*_custom` methods to change how the serialization
/// behaves. To construct, call `SerSettings::default()` to build the default,
/// then use the various methods to update the settings, e.g.:
///
/// ```
/// # use fortformat::ser::SerSettings;
/// # use fortformat::serde_common::NoneFill;
/// let settings = SerSettings::default()
///     .fill_method(NoneFill::new_string("N/A"));
/// ```
///
/// Each of the methods' documentation describes the purpose of its setting
/// and the default.
#[derive(Debug, Clone)]
pub struct SerSettings {
    fill_method: NoneFill,
    newline: &'static [u8],
    align_left_str: bool,
}

impl Default for SerSettings {
    fn default() -> Self {
        DEFAULT_SER_SETTINGS.clone()
    }
}

impl SerSettings {
    /// Sets how `None` values, the unit type, and unit structs are written.
    ///
    /// The default is the default [`NoneFill`], which is to write an "*" for
    /// the full width of the field.
    pub fn fill_method(mut self, fill_method: NoneFill) -> Self {
        self.fill_method = fill_method;
        self
    }

    /// Sets the bytes used to end a record and start a new line in a writer.
    ///
    /// The default is `b"\n"`, i.e. a literal newline byte. This matches the Rust
    /// convention of always using only a newline character, and never including a
    /// carriage return.
    ///
    /// As a trick, you can pass an empty bytestring, `b""`, if you do not want
    /// any new line bytes(s) automatically appended by one of the writer functions.
    pub fn newline(mut self, newline: &'static [u8]) -> Self {
        self.newline = newline;
        self
    }

    /// Strings will be serialized aligned to the left of the field.
    ///
    /// This is not standard Fortran behavior, which always aligns right.
    /// However, since Fortran character arrays are always fixed width, you
    /// often get the effect that they are left justified, and setting this
    /// to `true` mimics that.
    ///
    /// TODO: provide a "serialize_with" function that pads strings with
    /// spaces to a given width.
    pub fn align_left_str(mut self, align_left: bool) -> Self {
        self.align_left_str = align_left;
        self
    }
}

#[derive(Debug, Default)]
struct MapSerHelper {
    next_field_index: Option<usize>,
    next_field_fmt: Option<FortField>,
    data: Vec<Option<Vec<u8>>>,
    in_use: bool,
}

impl MapSerHelper {
    fn take_validate_data(&mut self) -> Vec<Option<Vec<u8>>> {
        let data = std::mem::take(&mut self.data);
        self.next_field_fmt = None;
        self.next_field_index = None;
        self.in_use = false;
        data
    }
}

/// Serializer for Fortran-format writers
struct Serializer<'f, W: Write, F: AsRef<str>> {
    buf: W,
    fmt: &'f FortFormat,
    fmt_idx: usize,
    fields: Option<&'f [F]>,
    field_idx: usize,
    map_helper: MapSerHelper,
    settings: &'f SerSettings,
}

impl<'f, F: AsRef<str>> Serializer<'f, Vec<u8>, F> {
    pub fn new(fmt: &'f FortFormat) -> Self {
        Self {
            buf: vec![],
            fmt,
            fmt_idx: 0,
            fields: None,
            field_idx: 0,
            map_helper: MapSerHelper::default(),
            settings: &DEFAULT_SER_SETTINGS,
        }
    }

    pub fn new_with_fields(fmt: &'f FortFormat, fields: &'f [F]) -> Self {
        Self {
            buf: vec![],
            fmt,
            fmt_idx: 0,
            fields: Some(fields),
            field_idx: 0,
            map_helper: MapSerHelper::default(),
            settings: &DEFAULT_SER_SETTINGS,
        }
    }

    pub fn new_custom(
        fmt: &'f FortFormat,
        fields: Option<&'f [F]>,
        settings: &'f SerSettings,
    ) -> Self {
        Self {
            buf: vec![],
            fmt,
            fmt_idx: 0,
            fields,
            field_idx: 0,
            map_helper: MapSerHelper::default(),
            settings,
        }
    }

    pub fn into_bytes(self) -> Vec<u8> {
        self.buf.to_vec()
    }

    pub fn try_into_string(self) -> Result<String, FromUtf8Error> {
        String::from_utf8(self.buf)
    }
}

impl<'f, W: Write, F: AsRef<str>> Serializer<'f, W, F> {
    pub fn new_writer(fmt: &'f FortFormat, writer: W) -> Self {
        Self {
            buf: writer,
            fmt,
            fmt_idx: 0,
            fields: None,
            field_idx: 0,
            map_helper: MapSerHelper::default(),
            settings: &DEFAULT_SER_SETTINGS,
        }
    }

    pub fn new_writer_with_fields(fmt: &'f FortFormat, fields: &'f [F], writer: W) -> Self {
        Self {
            buf: writer,
            fmt,
            fmt_idx: 0,
            fields: Some(fields),
            field_idx: 0,
            map_helper: MapSerHelper::default(),
            settings: &DEFAULT_SER_SETTINGS,
        }
    }

    pub fn new_writer_custom(
        fmt: &'f FortFormat,
        fields: Option<&'f [F]>,
        settings: &'f SerSettings,
        writer: W,
    ) -> Self {
        Self {
            buf: writer,
            fmt,
            fmt_idx: 0,
            fields: fields,
            field_idx: 0,
            map_helper: MapSerHelper::default(),
            settings,
        }
    }
}

impl<'f, W: Write + 'f, F: AsRef<str>> Serializer<'f, W, F> {
    // This shares a lot of code with the Deserializer. I tried refactoring that out
    // into an inner struct, but that ended up being more awkward because the inner
    // struct didn't know about the Deserializer's input string. Another option
    // might be a trait that requires defining advance_over_skips and methods
    // to increment indices and get formats/field names - will explore later.

    fn advance_over_skips(&mut self) -> std::io::Result<()> {
        loop {
            // Consume any skips (i.e. 1x, 2x) in the format, also adding space
            // to the output. This can be modified to handle other types
            // of Fortran positioning formats in the future.
            let peeked_fmt = self.fmt.get_field(self.fmt_idx);
            match peeked_fmt {
                Some(&FortField::Skip) => {
                    self.buf.write(b" ")?;
                    self.fmt_idx += 1;
                }
                _ => return Ok(()),
            }
        }
    }

    fn next_fmt(&mut self) -> SResult<&FortField> {
        self.advance_over_skips()?;
        loop {
            let next_fmt = self.fmt.get_field(self.fmt_idx);
            match next_fmt {
                Some(field) => {
                    self.fmt_idx += 1;
                    self.field_idx += 1;
                    return Ok(field);
                }
                None => return Err(SError::FormatSpecTooShort),
            }
        }
    }

    fn curr_field(&mut self) -> Option<&str> {
        if let Some(fields) = self.fields {
            fields.get(self.field_idx).map(|f| f.as_ref())
        } else {
            panic!("Called next_field on a deserializer without fields")
        }
    }

    fn try_prev_field(&self) -> Option<&str> {
        if self.field_idx == 0 {
            return None;
        }

        self.fields
            .map(|f| f.get(self.field_idx - 1))
            .flatten()
            .map(|f| f.as_ref())
    }

    fn peek_fmt(&mut self) -> Option<&FortField> {
        // We don't use advance_over_skips() here because that writes
        // things to the buffer, which a peek shouldn't do (and which
        // might fail).
        let mut i = self.fmt_idx;
        loop {
            let fmt = self.fmt.get_field(i)?;
            if !fmt.is_positional() {
                return Some(fmt);
            }
            i += 1;
        }
    }

    fn get_nth_nonskip_fmt(&self, n: usize) -> Option<&FortField> {
        let mut i = 0;
        let mut j = 0;
        loop {
            let fmt = self.fmt.get_field(j)?;
            if !fmt.is_positional() && i == n {
                return Some(fmt);
            } else if !fmt.is_positional() {
                i += 1;
            }
            j += 1;
        }
    }

    fn get_fmt_and_index_offset_for_field(&self, field_name: &str) -> Option<(usize, FortField)> {
        if let Some(fields) = self.fields {
            let mut i = 0;
            for fname in &fields[self.field_idx..] {
                if field_name == fname.as_ref() {
                    let fmt = self.get_nth_nonskip_fmt(self.field_idx + i)?;
                    return Some((i, *fmt));
                }
                i += 1;
            }
            None
        } else {
            panic!("Called get_fmt_and_index_offset_for_field on a serializer without field names");
        }
    }

    /// Write an existing slice of bytes to the buffer and advance the internal pointer to the
    /// next format and field name.
    ///
    /// # Panics
    /// Panics if the input slice does not have the number of bytes expected for the next format.
    /// It is the caller's responsibility to ensure that the correct number of bytes are provided.
    fn write_next_entry_raw(&mut self, bytes: &[u8], fmt: Option<FortField>) -> SResult<()> {
        let fmt = if let Some(fmt) = fmt {
            fmt
        } else {
            self.advance_over_skips()?;
            *self.next_fmt()?
        };

        let nbytes = match fmt {
            FortField::Char { width } => width,
            FortField::Logical { width } => Some(width),
            FortField::Integer {
                width,
                zeros: _,
                base: _,
            } => Some(width),
            FortField::Real {
                width,
                precision: _,
                fmt: _,
                scale: _,
            } => Some(width),
            FortField::Any => Some(bytes.len() as u32),
            FortField::Skip => {
                panic!("Should not get a skip format, should have advanced over all skips")
            }
        };

        if let Some(n) = nbytes {
            if n != bytes.len() as u32 {
                panic!("Called write_next_entry_raw with a slice of {} bytes, expected a slice of {} bytes", bytes.len(), n);
            }
        }

        self.buf.write(bytes)?;
        if let FortField::Any = fmt {
            // TODO: check how much space Fortran includes, and whether it is written before or after the entry.
            self.buf.write(b" ")?;
        }
        Ok(())
    }

    fn serialize_integer<I: itoa::Integer + Octal + UpperHex>(
        &mut self,
        abs_value: I,
        is_neg: bool,
    ) -> SResult<()> {
        let next_fmt = *self.next_fmt()?;
        if let FortField::Integer { width, zeros, base } = next_fmt {
            serialize_integer(width, zeros, base, &mut self.buf, abs_value, is_neg)
        } else {
            Err(SError::FormatTypeMismatch {
                spec_type: next_fmt,
                serde_type: "integer",
                field_name: self.try_prev_field().map(|f| f.to_string()),
            })
        }
    }

    fn serialize_real(&mut self, v: f64) -> SResult<()> {
        let next_fmt = *self.next_fmt()?;
        if let FortField::Real {
            width,
            precision,
            fmt,
            scale,
        } = next_fmt
        {
            let precision = precision.ok_or_else(|| {
                SError::InvalidOutputFmt(
                    next_fmt,
                    "real number formats must include a precision for output".to_string(),
                )
            })?;

            // TODO: apparently E and D formats can specify the number of digits in the exponent, that will
            // need added to the format spec.
            match fmt {
                RealFmt::D => {
                    serialize_real_exp(&mut self.buf, v, width, precision, scale, "D", None)
                }
                RealFmt::E => {
                    serialize_real_exp(&mut self.buf, v, width, precision, scale, "E", None)
                }
                RealFmt::F => serialize_real_f(&mut self.buf, v, width, precision, scale),
                RealFmt::G => todo!(),
            }
        } else {
            Err(SError::FormatTypeMismatch {
                spec_type: next_fmt,
                serde_type: "float",
                field_name: self.try_prev_field().map(|f| f.to_string()),
            })
        }
    }

    fn serialize_key_helper(&mut self, field: &str) -> SResult<()> {
        if self.fields.is_some() {
            let (offset, fmt) = self
                .get_fmt_and_index_offset_for_field(field)
                .ok_or_else(|| SError::FieldMissingError(field.to_string()))?;
            self.map_helper.next_field_index = Some(offset);
            self.map_helper.next_field_fmt = Some(fmt);
            Ok(())
        } else {
            Ok(())
        }
    }

    fn serialize_value_helper<T: ?Sized>(&mut self, value: &T) -> SResult<()>
    where
        T: serde::Serialize,
    {
        if self.fields.is_none() {
            return value.serialize(&mut *self);
        }

        if let (Some(offset), Some(fmt)) = (
            self.map_helper.next_field_index,
            self.map_helper.next_field_fmt,
        ) {
            while self.map_helper.data.len() <= offset {
                self.map_helper.data.push(None);
            }

            // TODO: I don't think this will work for fields that are themselves structures or maps. Will need
            // to iterate.
            let fortfmt = FortFormat::Fixed(vec![fmt]);
            // let bytes = to_bytes(value, &fortfmt)?;
            let bytes = to_bytes_custom::<_, &str>(value, &fortfmt, None, self.settings)?;
            self.map_helper.data[offset] = Some(bytes);
        } else {
            panic!(
                "serialize_key must be called before serialize_value when field names are given."
            );
        }

        Ok(())
    }

    fn end_helper(&mut self) -> SResult<()> {
        if self.map_helper.next_field_index.is_some() {
            for maybe_bytes in self.map_helper.take_validate_data() {
                if let Some(bytes) = maybe_bytes {
                    self.write_next_entry_raw(&bytes, None)?;
                } else {
                    let field_name = self.curr_field().unwrap_or("?");
                    unimplemented!("The field {field_name} did not have a value, but a later field did. This is not handled yet.");
                }
            }
        }
        Ok(())
    }

    fn end_record(&mut self) -> SResult<()> {
        self.buf.write(self.settings.newline)?;
        self.fmt_idx = 0;
        self.field_idx = 0;
        Ok(())
    }
}

impl<'a, 'f, W: Write + 'f, F: AsRef<str>> ser::Serializer for &'a mut Serializer<'f, W, F> {
    type Ok = ();
    type Error = SError;

    // we already store the format information on this struct so we should
    // be able to just use it for all the sequence serialization.
    type SerializeSeq = Self;
    type SerializeTuple = Self;
    type SerializeTupleStruct = Self;
    type SerializeTupleVariant = Self;
    type SerializeMap = Self;
    type SerializeStruct = Self;
    type SerializeStructVariant = Self;

    fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
        let next_fmt = *self.next_fmt()?;
        if let FortField::Logical { width } = next_fmt {
            serialize_logical(v, width, &mut self.buf)
        } else {
            Err(SError::FormatTypeMismatch {
                spec_type: next_fmt,
                serde_type: "bool",
                field_name: self.try_prev_field().map(|f| f.to_string()),
            })
        }
    }

    fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
        self.serialize_integer(v.abs(), v < 0)
    }

    fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
        self.serialize_integer(v.abs(), v < 0)
    }

    fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
        self.serialize_integer(v.abs(), v < 0)
    }

    fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
        self.serialize_integer(v.abs(), v < 0)
    }

    fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
        self.serialize_integer(v.abs(), v < 0)
    }

    fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
        self.serialize_integer(v, false)
    }

    fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
        self.serialize_integer(v, false)
    }

    fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
        self.serialize_integer(v, false)
    }

    fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
        self.serialize_integer(v, false)
    }

    fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
        self.serialize_integer(v, false)
    }

    fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
        self.serialize_real(v as f64)
    }

    fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
        self.serialize_real(v)
    }

    fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
        self.serialize_str(&v.to_string())
    }

    fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
        self.serialize_bytes(v.as_bytes())
    }

    fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
        let next_fmt = *self.next_fmt()?;
        if let FortField::Char { width } = next_fmt {
            serialize_characters(v, width, &mut self.buf, self.settings.align_left_str)
        } else {
            Err(SError::FormatTypeMismatch {
                spec_type: next_fmt,
                serde_type: "char/str/bytes",
                field_name: self.try_prev_field().map(|f| f.to_string()),
            })
        }
    }

    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
        let next_fmt = *self.next_fmt()?;
        let fill_bytes = self
            .settings
            .fill_method
            .make_fill_bytes(&next_fmt, self.settings.align_left_str)?;
        self.write_next_entry_raw(&fill_bytes, Some(next_fmt))
    }

    fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
    where
        T: serde::Serialize,
    {
        value.serialize(self)
    }

    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
        self.serialize_none()
    }

    fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
        self.serialize_none()
    }

    fn serialize_unit_variant(
        self,
        _name: &'static str,
        variant_index: u32,
        variant: &'static str,
    ) -> Result<Self::Ok, Self::Error> {
        // Since Fortran specifies which type each field should be, we can determine whether to serialize
        // as an integer or string.
        // TODO: add deserialization to allow round-tripping.
        let peeked_fmt = *self.peek_fmt().ok_or_else(|| SError::FormatSpecTooShort)?;
        if let FortField::Integer {
            width: _,
            zeros: _,
            base: _,
        } = peeked_fmt
        {
            self.serialize_u32(variant_index)
        } else if let FortField::Char { width: _ } = peeked_fmt {
            self.serialize_str(variant)
        } else {
            Err(SError::FormatTypeMismatch {
                spec_type: peeked_fmt,
                serde_type: "str or integer",
                field_name: self.try_prev_field().map(|f| f.to_string()),
            })
        }
    }

    fn serialize_newtype_struct<T: ?Sized>(
        self,
        _name: &'static str,
        value: &T,
    ) -> Result<Self::Ok, Self::Error>
    where
        T: serde::Serialize,
    {
        value.serialize(self)
    }

    fn serialize_newtype_variant<T: ?Sized>(
        self,
        name: &'static str,
        variant_index: u32,
        variant: &'static str,
        value: &T,
    ) -> Result<Self::Ok, Self::Error>
    where
        T: serde::Serialize,
    {
        // Consider this behavior subject to change, but it seems that the most sensible
        // way to serialize a variant is to put the index/variant in the first field and
        // the value in the second.
        self.serialize_unit_variant(name, variant_index, variant)?;
        value.serialize(self)
    }

    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
        Ok(self)
    }

    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
        Ok(self)
    }

    fn serialize_tuple_struct(
        self,
        _name: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
        Ok(self)
    }

    fn serialize_tuple_variant(
        self,
        name: &'static str,
        variant_index: u32,
        variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
        self.serialize_unit_variant(name, variant_index, variant)?;
        Ok(self)
    }

    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
        if self.map_helper.in_use {
            unimplemented!("Can't yet recursively call serialize_map - the map_helper must be reset before the next call")
        }
        Ok(self)
    }

    fn serialize_struct(
        self,
        _name: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeStruct, Self::Error> {
        if self.map_helper.in_use {
            unimplemented!("Can't yet recursively call serialize_map - the map_helper must be reset before the next call")
        }
        Ok(self)
    }

    fn serialize_struct_variant(
        self,
        name: &'static str,
        variant_index: u32,
        variant: &'static str,
        _len: usize,
    ) -> Result<Self::SerializeStructVariant, Self::Error> {
        if self.map_helper.in_use {
            unimplemented!("Can't yet recursively call serialize_map - the map_helper must be reset before the next call")
        }
        self.serialize_unit_variant(name, variant_index, variant)?;
        Ok(self)
    }
}

impl<'a, 'f, W: Write + 'f, F: AsRef<str>> ser::SerializeSeq for &'a mut Serializer<'f, W, F> {
    type Ok = ();

    type Error = SError;

    fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: serde::Serialize,
    {
        // From a serialization standpoint, we just serialize each value in a sequence
        // as normal, we cannot indicate that these elements are grouped together.
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
}

impl<'a, 'f, W: Write + 'f, F: AsRef<str>> ser::SerializeTuple for &'a mut Serializer<'f, W, F> {
    type Ok = ();

    type Error = SError;

    fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: serde::Serialize,
    {
        // From a serialization standpoint, we just serialize each value in a tuple
        // as normal, we cannot indicate that these elements are grouped together.
        // (Same as for a sequence.)
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
}

impl<'a, 'f, W: Write + 'f, F: AsRef<str>> ser::SerializeTupleStruct
    for &'a mut Serializer<'f, W, F>
{
    type Ok = ();

    type Error = SError;

    fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: serde::Serialize,
    {
        // From a serialization standpoint, we just serialize each value in a tuple
        // struct as normal, we cannot indicate that these elements are grouped together.
        // (Same as for a sequence.)
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
}

impl<'a, 'f, W: Write + 'f, F: AsRef<str>> ser::SerializeTupleVariant
    for &'a mut Serializer<'f, W, F>
{
    type Ok = ();

    type Error = SError;

    fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: serde::Serialize,
    {
        // From a serialization standpoint, we just serialize each value in a tuple
        // variant as normal, we cannot indicate that these elements are grouped together.
        // (Same as for a sequence.)
        value.serialize(&mut **self)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        Ok(())
    }
}

impl<'a, 'f, W: Write + 'f, F: AsRef<str>> ser::SerializeMap for &'a mut Serializer<'f, W, F> {
    type Ok = ();

    type Error = SError;

    fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<(), Self::Error>
    where
        T: serde::Serialize,
    {
        if self.fields.is_some() {
            // This is weird, but since all we know about key is that it is serializable
            // the best we can do is serialize it to a string and check against the field
            // names
            let fmt = FortFormat::parse("(a512)").unwrap();
            let key_string =
                to_string(key, &fmt).map_err(|e| SError::KeyToFieldError(Arc::new(e)))?;
            self.serialize_key_helper(key_string.trim())
        } else {
            Ok(())
        }
    }

    fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<(), Self::Error>
    where
        T: serde::Serialize,
    {
        self.serialize_value_helper(value)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.end_helper()
    }
}

// This will have to be a different structure; if it has known fields, then
// this will have to build a Vec<Vec<u8>> to put the elements in the correct
// order and write that to the buffer at the end. Same of SerializeMap.
impl<'a, 'f, W: Write + 'f, F: AsRef<str>> ser::SerializeStruct for &'a mut Serializer<'f, W, F> {
    type Ok = ();

    type Error = SError;

    fn serialize_field<T: ?Sized>(
        &mut self,
        key: &'static str,
        value: &T,
    ) -> Result<(), Self::Error>
    where
        T: serde::Serialize,
    {
        if self.fields.is_some() {
            self.serialize_key_helper(key)?;
        }
        self.serialize_value_helper(value)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.end_helper()
    }
}

impl<'a, 'f, W: Write + 'f, F: AsRef<str>> ser::SerializeStructVariant
    for &'a mut Serializer<'f, W, F>
{
    type Ok = ();

    type Error = SError;

    fn serialize_field<T: ?Sized>(
        &mut self,
        key: &'static str,
        value: &T,
    ) -> Result<(), Self::Error>
    where
        T: serde::Serialize,
    {
        if self.fields.is_some() {
            self.serialize_key_helper(key)?;
        }
        self.serialize_value_helper(value)
    }

    fn end(self) -> Result<Self::Ok, Self::Error> {
        self.end_helper()
    }
}

pub(crate) fn serialize_logical<W: Write>(v: bool, width: u32, mut buf: W) -> SResult<()> {
    let b = if v { b"T" } else { b"F" };
    for _ in 0..width - 1 {
        buf.write(b" ")?;
    }
    buf.write(b)?;
    Ok(())
}

pub(crate) fn serialize_characters<W: Write>(
    v: &[u8],
    width: Option<u32>,
    mut buf: W,
    left_align: bool,
) -> SResult<()> {
    if let Some(width) = width {
        let w = width as usize;
        if v.len() >= w {
            buf.write(&v[..w])?;
        } else if left_align {
            buf.write(v)?;
            for _ in 0..(w - v.len()) {
                buf.write(b" ")?;
            }
        } else {
            for _ in 0..(w - v.len()) {
                buf.write(b" ")?;
            }
            buf.write(v)?;
        }
        Ok(())
    } else {
        // With no width specified, the full string is written out with its exact length.
        buf.write(v)?;
        Ok(())
    }
}

pub(crate) fn serialize_integer<W: Write, I: itoa::Integer + Octal + UpperHex>(
    width: u32,
    zeros: Option<u32>,
    base: IntBase,
    mut buf: W,
    abs_value: I,
    is_neg: bool,
) -> SResult<()> {
    // wish that this didn't require allocating a vector, but I think this is the cleanest way
    // to handle this, since we need to know the length of the serialized number before we
    // can write.
    let (s, is_dec): (Vec<u8>, bool) = match base {
        IntBase::Decimal => {
            let mut b = itoa::Buffer::new();
            (
                b.format(abs_value)
                    .as_bytes()
                    .into_iter()
                    .copied()
                    .collect(),
                true,
            )
        }
        IntBase::Octal => (
            format!("{abs_value:o}")
                .as_bytes()
                .into_iter()
                .copied()
                .collect(),
            false,
        ),
        IntBase::Hexadecimal => (
            format!("{abs_value:X}")
                .as_bytes()
                .into_iter()
                .copied()
                .collect(),
            false,
        ),
    };

    let nsign = if is_neg { 1 } else { 0 };
    let nzeros = zeros.map(|n| n.saturating_sub(s.len() as u32)).unwrap_or(0);
    let nchar = nzeros + nsign + s.len() as u32;

    let bad_output = nchar > width || (is_neg && !is_dec);
    if bad_output {
        for _ in 0..width {
            buf.write(b"*")?;
        }
    } else {
        let nspace = width - nchar;
        for _ in 0..nspace {
            buf.write(b" ")?;
        }
        if is_neg {
            buf.write(b"-")?;
        }
        for _ in 0..nzeros {
            buf.write(b"0")?;
        }
        buf.write(&s)?;
    }

    Ok(())
}

/// Wrapper around ryu_floating_decimal::d2d that correctly handles
/// a value of 0.
fn d2d_helper(v: f64) -> ryu_floating_decimal::FloatingDecimal64 {
    if v == 0.0 {
        ryu_floating_decimal::FloatingDecimal64 {
            mantissa: 0,
            exponent: 0,
        }
    } else {
        d2d(v)
    }
}

pub(crate) fn serialize_real_f<W: Write>(
    mut buf: W,
    v: f64,
    width: u32,
    precision: u32,
    scale: i32,
) -> SResult<()> {
    let v_is_neg = v < 0.0;
    let signed_prec = precision as i32;
    let rv = (v * 10.0_f64.powi(signed_prec)).round_ties_even() * 10.0_f64.powi(-signed_prec);

    let v = d2d_helper(rv);

    let mut b = itoa::Buffer::new();
    let s = b.format(v.mantissa);
    let m_bytes = s.as_bytes();
    let exponent = v.exponent;

    // For all numbers, we need the decimal place and the numbers after it. For negative numbers,
    // we also need the negative sign.
    let width = width as usize;
    let n_reserved = if v_is_neg {
        2 + precision
    } else {
        1 + precision
    } as usize;

    // Now we need to figure out how many digits to include before the decimal. Some examples:
    // Value    | Mantissa | n_bytes | Exponent | # leading digits |
    // 3.14     | 314      | 3       | -2       | 1                |
    // 0.0314   | 314      | 3       | -4       | 0                |
    // 3140.0   | 314      | 3       | +1       | 4                |
    // 3141.59  | 314159   | 6       | -2       | 4                |
    // So the number of leading digits is max(n_bytes + exponent, 0). Note that for the second example,
    // the leading 0 will be handled specially.
    // Then there's the leading zeros *after* the decimal point. These are only needed if the neg. of the
    // exponent is greater than the number of bytes, in which case the difference is the number of zeros needed.
    // We also need to consider the scale: a positive scale moves the decimal right, so it adds leading
    // digits, and a negative one does the opposite.

    // Now we have enough information to tell if the value is too wide.
    let exp_plus_scale = (exponent + scale) as isize;
    let n_leading_digits = m_bytes.len().saturating_add_signed(exp_plus_scale);
    if n_leading_digits + n_reserved > width {
        for _ in 0..width {
            buf.write(b"*")?;
        }
        return Ok(());
    }

    let wants_leading_zero = n_leading_digits == 0;
    let (n_spaces, print_leading_zero) = if !wants_leading_zero {
        (width - n_reserved - n_leading_digits, false)
    } else if n_leading_digits + n_reserved < width {
        (width - n_reserved - n_leading_digits - 1, true)
    } else {
        (0, false)
    };

    let zeros_after_decimal = if exp_plus_scale >= 0 {
        0
    } else {
        let nexp = (-exp_plus_scale) as usize;
        nexp.saturating_sub(m_bytes.len())
    };

    for _ in 0..n_spaces {
        buf.write(b" ")?;
    }

    if v_is_neg {
        buf.write(b"-")?;
    }

    if print_leading_zero {
        buf.write(b"0")?;
    }

    for i in 0..(n_leading_digits + precision as usize - zeros_after_decimal) {
        if i == n_leading_digits {
            buf.write(b".")?;
            for _ in 0..zeros_after_decimal {
                buf.write(b"0")?;
            }
        }
        let i = i as usize;
        let c = m_bytes.get(i..i + 1).unwrap_or(b"0");
        buf.write(c)?;
    }

    // Edge case: all of the digits after the decimal are 0
    if n_leading_digits + precision as usize == zeros_after_decimal {
        buf.write(b".")?;
        for _ in 0..zeros_after_decimal {
            buf.write(b"0")?;
        }
    }

    Ok(())
}

pub(crate) fn serialize_real_exp<W: Write>(
    mut buf: W,
    v: f64,
    width: u32,
    precision: u32,
    scale: i32,
    exp_ch: &str,
    n_exp_digits: Option<u32>,
) -> SResult<()> {
    let v_is_neg = v < 0.0;
    let v_orig = v;
    let v = d2d_helper(v);

    // This is complicated so some examples using e12.3 as the format
    // Value    | Mantissa | Exponent | Fortran   | Representation
    // 3.14     | 314      | -2       | 0.314E+01 | (mantissa // 10^3).(mantissa % 10^3)E(exponent+3)
    // 0.0314   | 314      | -4       | 0.314E-01 | (mantissa // 10^3).(mantissa % 10^3)E(exponent+3)
    // 3140.0   | 314      | +1       | 0.314E+04 | (mantissa // 10^3).(mantissa % 10^3)E(exponent+3)
    // 3141.59  | 314159   | -2       | 0.314E+04 | (mantissa // 10^6).(mantissa % 10^6)E(exponent+6)
    //
    // Then scaling shifts where the decimal point is: +ve moves it right, -ve left. So 3.14 formatted
    // as 1pe12.3 would look like 3.140E+00, and 2pe12.3 like 31.40E-01. With +ve scales, we end up with
    // more sig figs (e.g. 3.1415 formatted as 1pe12.3 becomes 3.141E+00). With -ve scales, we keep the
    // same number of digits after the decimal (so -2pe12.3 => 0.003E+03).
    //
    // Also as far as I can tell for spacing, a leading zero before the decimal is optional, but a negative
    // sign is not. so .314E+01 will fit in an 8-wide format, but not a 7-wide. (At 9-wide it gets the leading 0.)
    // If it's negative (-.314E+01), it needs at least 9 characters. At 10, it becomes -0.314E+01.

    // Rounding is a pain: we have `precision` sig figs, plus `scale` (see above, by default, only the values
    // after the decimal are used). Consider 3141.59 and its mantissa of 314159. The ilog10 of that will be 5,
    // so ilog10 + 1 is how many digits the mantissa has. If we have `n` digits, then we need to round the mantissa
    // to the nearest 10^n.
    let n_digits_avail = ((precision as i32) + scale).max(0) as u32;
    let n_digits_wanted = if v.mantissa == 0 {
        // .ilog10() will panic if called on a 0, but if the value was 0,
        // we want 1 digit before the decimal point by default.
        1
    } else {
        v.mantissa.ilog10() + 1
    };

    let mantissa = if n_digits_wanted <= n_digits_avail || v.mantissa == 0 {
        v.mantissa
    } else {
        let m = 10u64.pow(n_digits_wanted - n_digits_avail);
        let r = v.mantissa % m;
        let round_down = if r == m / 2 {
            // Halfway case; always round towards the number that will be even in the least
            // significant remaining digit. E.g. if the mantissa is 31415 and we can have 4
            // sig figs, then is the mantissa / m and modulo 2 (31415 / 10 = 3141 % 2 = 1) odd or even?
            (v.mantissa / m) % 2 == 0
        } else {
            r < m / 2
        };

        let tmp = v.mantissa.checked_next_multiple_of(m).ok_or_else(|| {
            SError::SerializationFailure(format!("overflow while rounding {v_orig}"))
        })?;
        if round_down {
            tmp - m
        } else {
            tmp
        }
    };
    let mut b = itoa::Buffer::new();
    let s = b.format(mantissa);
    let m_bytes = s.as_bytes();

    let exponent = if v.mantissa == 0 {
        // Values of 0 seem to be a special case in fortran
        // (at least for gfortran) - the exponent will always
        // be 0 no matter what the scaling.
        0
    } else {
        v.exponent + m_bytes.len() as i32 - scale
    };
    let mut b = itoa::Buffer::new();
    let s = b.format(exponent.abs());
    let e_bytes = s.as_bytes();

    // Include precision # digits, plus decimal point, and the exponent. If the number of digits
    // in the exponent isn't given, it will always be 4 characters wide. (If it needs three digits,
    // the 'E' or 'D' is dropped.) Otherwise it seems to be 2 for the E+ or E- and the number of digits.
    // If the exponent won't fit, this is a number we can't format, so print out the *'s and return.
    let exp_nchar = if let Some(n) = n_exp_digits {
        if e_bytes.len() > n as usize {
            for _ in 0..width {
                buf.write(b"*")?;
            }
            return Ok(());
        }
        n + 2
    } else {
        if e_bytes.len() > 3 {
            for _ in 0..width {
                buf.write(b"*")?;
            }
            return Ok(());
        }
        4
    };
    let min_width = if v_is_neg {
        precision + 2 + exp_nchar
    } else {
        precision + 1 + exp_nchar
    };
    if width < min_width {
        for _ in 0..width {
            buf.write(b"*")?;
        }
        return Ok(());
    }

    let nspaces = if width > min_width {
        width - min_width - 1
    } else {
        0
    };
    for _ in 0..nspaces {
        buf.write(b" ")?;
    }

    if v_is_neg {
        buf.write(b"-")?;
    }

    if scale > 0 {
        let mut i = 0;
        for _ in 0..scale {
            let c = m_bytes.get(i..i + 1).unwrap_or(b"0");
            buf.write(c)?;
            i += 1;
        }
        buf.write(b".")?;
        let n_after_decimal = if scale <= 1 {
            precision
        } else {
            precision - scale as u32 + 1
        };
        for _ in 0..n_after_decimal {
            let c = m_bytes.get(i..i + 1).unwrap_or(b"0");
            buf.write(c)?;
            i += 1;
        }
    } else {
        if width > min_width {
            // if we have at least one extra character, write the leading zero
            buf.write(b"0")?;
        }
        buf.write(b".")?;
        let mut i = 0;
        for _ in 0..precision {
            if i < -scale {
                buf.write(b"0")?;
            } else {
                let j = (i + scale) as usize;
                let c = m_bytes.get(j..j + 1).unwrap_or(b"0");
                buf.write(c)?;
            }
            i += 1;
        }
    }

    let n_digits = if e_bytes.len() <= (exp_nchar as usize) - 2 {
        buf.write(exp_ch.as_bytes())?;
        exp_nchar - 2
    } else {
        exp_nchar - 1
    };

    if exponent < 0 {
        buf.write(b"-")?;
    } else {
        buf.write(b"+")?;
    }

    // Pad with 0s if needed
    for _ in 0..(n_digits as usize - e_bytes.len()) {
        buf.write(b"0")?;
    }
    buf.write(e_bytes)?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;

    #[derive(Debug, serde::Serialize)]
    struct Test {
        a: &'static str,
        b: i32,
        c: f64,
    }

    #[derive(Debug, serde::Serialize)]
    struct HasFlat {
        name: &'static str,
        #[serde(flatten)]
        data: HashMap<String, i32>,
    }

    #[derive(Debug, serde::Serialize)]
    struct Nested {
        db_id: i64,
        inner: Test,
    }

    #[test]
    fn test_ser_bool() {
        let fmt = FortFormat::parse("(l1)").unwrap();
        let s = to_string(true, &fmt).unwrap();
        assert_eq!(s, "T");
        let s = to_string(false, &fmt).unwrap();
        assert_eq!(s, "F");

        let fmt = FortFormat::parse("(l3)").unwrap();
        let s = to_string(true, &fmt).unwrap();
        assert_eq!(s, "  T");
        let s = to_string(false, &fmt).unwrap();
        assert_eq!(s, "  F");
    }

    #[test]
    fn test_ser_dec_int() {
        let fmt = FortFormat::parse("(i4)").unwrap();
        let s = to_string(42, &fmt).unwrap();
        assert_eq!(s, "  42");
        let s = to_string(-42, &fmt).unwrap();
        assert_eq!(s, " -42");
        let s = to_string(12345, &fmt).unwrap();
        assert_eq!(s, "****");

        let fmt = FortFormat::parse("(i4.3)").unwrap();
        let s = to_string(42, &fmt).unwrap();
        assert_eq!(s, " 042");
        let s = to_string(-42, &fmt).unwrap();
        assert_eq!(s, "-042");
        let s = to_string(123, &fmt).unwrap();
        assert_eq!(s, " 123");
        let s = to_string(-123, &fmt).unwrap();
        assert_eq!(s, "-123");

        let fmt = FortFormat::parse("(i3.3)").unwrap();
        let s = to_string(42, &fmt).unwrap();
        assert_eq!(s, "042");
        let s = to_string(-42, &fmt).unwrap();
        assert_eq!(s, "***");
    }

    #[test]
    fn test_octal_int() {
        let fmt = FortFormat::parse("(o5)").unwrap();
        let s = to_string(42, &fmt).unwrap();
        assert_eq!(s, "   52");
        let s = to_string(-42, &fmt).unwrap();
        assert_eq!(s, "*****");

        let fmt = FortFormat::parse("(o5.3)").unwrap();
        let s = to_string(42, &fmt).unwrap();
        assert_eq!(s, "  052");
        let s = to_string(-42, &fmt).unwrap();
        assert_eq!(s, "*****");
    }

    #[test]
    fn test_hex_int() {
        let fmt = FortFormat::parse("(z5)").unwrap();
        let s = to_string(42, &fmt).unwrap();
        assert_eq!(s, "   2A");
        let s = to_string(-42, &fmt).unwrap();
        assert_eq!(s, "*****");

        let fmt = FortFormat::parse("(z5.3)").unwrap();
        let s = to_string(42, &fmt).unwrap();
        assert_eq!(s, "  02A");
        let s = to_string(-42, &fmt).unwrap();
        assert_eq!(s, "*****");
    }

    #[test]
    fn test_char() {
        let fmt = FortFormat::parse("(a)").unwrap();
        let s = to_string('x', &fmt).unwrap();
        assert_eq!(s, "x");

        let fmt = FortFormat::parse("(a2)").unwrap();
        let s = to_string('x', &fmt).unwrap();
        assert_eq!(s, " x");
    }

    #[test]
    fn test_str() {
        let fmt = FortFormat::parse("(a)").unwrap();
        let s = to_string("abcde", &fmt).unwrap();
        assert_eq!(s, "abcde");

        let fmt = FortFormat::parse("(a5)").unwrap();
        let s = to_string("abc", &fmt).unwrap();
        assert_eq!(s, "  abc");
        let s = to_string("abcde", &fmt).unwrap();
        assert_eq!(s, "abcde");
        let s = to_string("abcdefg", &fmt).unwrap();
        assert_eq!(s, "abcde");

        let settings = SerSettings::default().align_left_str(true);
        let s = to_string_custom::<_, &str>("abc", &fmt, None, &settings).unwrap();
        assert_eq!(s, "abc  ");
    }

    #[test]
    fn test_real_f() {
        let fmt = FortFormat::parse("(f9.4)").unwrap();
        let s = to_string(8e-5, &fmt).unwrap();
        assert_eq!(s, "   0.0001");

        let fmt = FortFormat::parse("(f9.4)").unwrap();
        let s = to_string(4e-5, &fmt).unwrap();
        assert_eq!(s, "   0.0000");

        let fmt = FortFormat::parse("(f5.3)").unwrap();
        let s = to_string(-0.01, &fmt).unwrap();
        assert_eq!(s, "-.010");

        let fmt = FortFormat::parse("(f4.3)").unwrap();
        let s = to_string(0.314, &fmt).unwrap();
        assert_eq!(s, ".314");

        let fmt = FortFormat::parse("(f8.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "   3.140");

        let fmt = FortFormat::parse("(2pf8.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, " 314.000");

        let fmt = FortFormat::parse("(-2pf8.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "   0.031");

        let fmt = FortFormat::parse("(2pf5.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "*****");
    }

    #[test]
    fn test_real_e() {
        let fmt = FortFormat::parse("(e12.3)").unwrap();
        let s = to_string(0.0314, &fmt).unwrap();
        assert_eq!(s, "   0.314E-01");
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "   0.314E+01");
        let s = to_string(3140.0, &fmt).unwrap();
        assert_eq!(s, "   0.314E+04");
        let s = to_string(3141.59, &fmt).unwrap();
        assert_eq!(s, "   0.314E+04");
        let s = to_string(3.14e120, &fmt).unwrap();
        assert_eq!(s, "   0.314+121");

        let fmt = FortFormat::parse("(1pe12.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "   3.140E+00");
        let s = to_string(3.1415, &fmt).unwrap();
        assert_eq!(s, "   3.142E+00"); // always round to the even least significant remaining digit

        let fmt = FortFormat::parse("(2pe12.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "   31.40E-01");

        let fmt = FortFormat::parse("(-1pe12.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "   0.031E+02");

        let fmt = FortFormat::parse("(-2pe12.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "   0.003E+03");

        let fmt = FortFormat::parse("(e7.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "*******");
        let s = to_string(-3.14, &fmt).unwrap();
        assert_eq!(s, "*******");

        let fmt = FortFormat::parse("(e8.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, ".314E+01");
        let s = to_string(-3.14, &fmt).unwrap();
        assert_eq!(s, "********");

        let fmt = FortFormat::parse("(e9.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "0.314E+01");
        let s = to_string(-3.14, &fmt).unwrap();
        assert_eq!(s, "-.314E+01");

        // Test rounding up with various scaling
        let fmt = FortFormat::parse("(-2pe13.5)").unwrap();
        let s = to_string(0.999999, &fmt).unwrap();
        assert_eq!(s, "  0.00100E+03");

        let fmt = FortFormat::parse("(-1pe13.5)").unwrap();
        let s = to_string(0.999999, &fmt).unwrap();
        assert_eq!(s, "  0.01000E+02");

        let fmt = FortFormat::parse("(e13.5)").unwrap();
        let s = to_string(0.999999, &fmt).unwrap();
        assert_eq!(s, "  0.10000E+01");

        let fmt = FortFormat::parse("(1pe13.5)").unwrap();
        let s = to_string(0.999999, &fmt).unwrap();
        assert_eq!(s, "  9.99999E-01");

        let fmt = FortFormat::parse("(2pe13.5)").unwrap();
        let s = to_string(0.999999, &fmt).unwrap();
        assert_eq!(s, "  99.9999E-02");

        // Don't have these implemented yet - format parsing needs to handle the eN at the end
        // let fmt = FortFormat::parse("(e12.3e1)").unwrap();
        // let s = to_string(3.14e5, &fmt).unwrap();
        // assert_eq!(s, "    0.314E+6");
        // let s = to_string(3.14e15, &fmt).unwrap();
        // assert_eq!(s, "************");

        // let fmt = FortFormat::parse("(e12.3e5)").unwrap();
        // let s = to_string(3.14, &fmt).unwrap();
        // assert_eq!(s, "0.314E+00001");

        let fmt = FortFormat::parse("(1pe12.4)").unwrap();
        let s = to_string(9.8765e35, &fmt).unwrap();
        assert_eq!(s, "  9.8765E+35");

        // Extra checks for a number with only 1 sig fig to make sure there
        // aren't edge cases.
        let fmt = FortFormat::parse("(e12.4)").unwrap();
        let s = to_string(1.0, &fmt).unwrap();
        assert_eq!(s, "  0.1000E+01");

        let fmt = FortFormat::parse("(1pe12.4)").unwrap();
        let s = to_string(1.0, &fmt).unwrap();
        assert_eq!(s, "  1.0000E+00");

        let fmt = FortFormat::parse("(2pe12.4)").unwrap();
        let s = to_string(1.0, &fmt).unwrap();
        assert_eq!(s, "  10.000E-01");

        let fmt = FortFormat::parse("(-1pe12.4)").unwrap();
        let s = to_string(1.0, &fmt).unwrap();
        assert_eq!(s, "  0.0100E+02");

        let fmt = FortFormat::parse("(-2pe12.4)").unwrap();
        let s = to_string(1.0, &fmt).unwrap();
        assert_eq!(s, "  0.0010E+03");

        let fmt = FortFormat::parse("(e12.4)").unwrap();
        let s = to_string(-1.0, &fmt).unwrap();
        assert_eq!(s, " -0.1000E+01");

        let fmt = FortFormat::parse("(1pe12.4)").unwrap();
        let s = to_string(-1.0, &fmt).unwrap();
        assert_eq!(s, " -1.0000E+00");

        let fmt = FortFormat::parse("(2pe12.4)").unwrap();
        let s = to_string(-1.0, &fmt).unwrap();
        assert_eq!(s, " -10.000E-01");

        let fmt = FortFormat::parse("(-1pe12.4)").unwrap();
        let s = to_string(-1.0, &fmt).unwrap();
        assert_eq!(s, " -0.0100E+02");

        let fmt = FortFormat::parse("(-2pe12.4)").unwrap();
        let s = to_string(-1.0, &fmt).unwrap();
        assert_eq!(s, " -0.0010E+03");

        // Edge cases
        // First, a value of exactly 0 caused a crash in v0.1.1 because the mantissa
        // became 0 and thus the ilog10 value could not be calculated. (In debug mode,
        // this manifested as an overflow subtraction panic in ryu.)
        let fmt = FortFormat::parse("(e12.4)").unwrap();
        let s = to_string(0.0, &fmt).unwrap();
        assert_eq!(s, "  0.0000E+00");

        let fmt = FortFormat::parse("(1pe12.4)").unwrap();
        let s = to_string(0.0, &fmt).unwrap();
        assert_eq!(s, "  0.0000E+00");

        let fmt = FortFormat::parse("(2pe12.4)").unwrap();
        let s = to_string(0.0, &fmt).unwrap();
        assert_eq!(s, "  00.000E+00");

        let fmt = FortFormat::parse("(-1pe12.4)").unwrap();
        let s = to_string(0.0, &fmt).unwrap();
        assert_eq!(s, "  0.0000E+00");

        let fmt = FortFormat::parse("(-2pe12.4)").unwrap();
        let s = to_string(0.0, &fmt).unwrap();
        assert_eq!(s, "  0.0000E+00");
    }

    #[test]
    fn test_real_d() {
        let fmt = FortFormat::parse("(d12.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "   0.314D+01");
        let s = to_string(3.14e120, &fmt).unwrap();
        assert_eq!(s, "   0.314+121");

        let fmt = FortFormat::parse("(1pd12.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "   3.140D+00");

        let fmt = FortFormat::parse("(-1pd12.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "   0.031D+02");

        let fmt = FortFormat::parse("(-2pd12.3)").unwrap();
        let s = to_string(3.14, &fmt).unwrap();
        assert_eq!(s, "   0.003D+03");

        // Check edge case for 0
        let fmt = FortFormat::parse("(d12.4)").unwrap();
        let s = to_string(0.0, &fmt).unwrap();
        assert_eq!(s, "  0.0000D+00");

        let fmt = FortFormat::parse("(1pd12.4)").unwrap();
        let s = to_string(0.0, &fmt).unwrap();
        assert_eq!(s, "  0.0000D+00");

        let fmt = FortFormat::parse("(2pd12.4)").unwrap();
        let s = to_string(0.0, &fmt).unwrap();
        assert_eq!(s, "  00.000D+00");

        let fmt = FortFormat::parse("(-1pd12.4)").unwrap();
        let s = to_string(0.0, &fmt).unwrap();
        assert_eq!(s, "  0.0000D+00");

        let fmt = FortFormat::parse("(-2pd12.4)").unwrap();
        let s = to_string(0.0, &fmt).unwrap();
        assert_eq!(s, "  0.0000D+00");
    }

    #[test]
    fn test_vec() {
        let fmt = FortFormat::parse("(i3.3,1x,i4.4,1x,i5.5)").unwrap();
        let s = to_string(vec![10, 200, 3000], &fmt).unwrap();
        assert_eq!(s, "010 0200 03000");
    }

    #[test]
    fn test_tuple() {
        let fmt = FortFormat::parse("(a3,1x,i3.3)").unwrap();
        let s = to_string(("Hi!", 42), &fmt).unwrap();
        assert_eq!(s, "Hi! 042");
    }

    #[test]
    fn test_struct_by_order() {
        let fmt = FortFormat::parse("(a6,1x,i3,1x,e8.3)").unwrap();
        let value = Test {
            a: "Hello",
            b: 42,
            c: 3.14,
        };
        let s = to_string(value, &fmt).unwrap();
        assert_eq!(s, " Hello  42 .314E+01");
    }

    #[test]
    fn test_struct_by_name() {
        let fmt = FortFormat::parse("(i3,1x,e8.3,1x,a6)").unwrap();
        let value = Test {
            a: "Hello",
            b: 42,
            c: 3.14,
        };
        let s = to_string_with_fields(value, &fmt, &["b", "c", "a"]).unwrap();
        assert_eq!(s, " 42 .314E+01  Hello");
    }

    #[test]
    fn test_map_by_name() {
        let value = HashMap::from([("a", 2), ("b", 4), ("c", 8)]);
        let fmt = FortFormat::parse("(3i2)").unwrap();
        let s = to_string_with_fields(value, &fmt, &["b", "a", "c"]).unwrap();
        assert_eq!(s, " 4 2 8");
    }

    #[test]
    fn test_struct_with_flattened_map() {
        let data = HashMap::from([
            ("co2".to_string(), 400_000),
            ("ch4".to_string(), 1900),
            ("n2o".to_string(), 310),
            ("co".to_string(), 100),
        ]);
        let value = HasFlat { name: "pa", data };
        let fmt = FortFormat::parse("(a2,4(1x,i6))").unwrap();
        let s = to_string_with_fields(value, &fmt, &["name", "n2o", "co", "co2", "ch4"]).unwrap();
        assert_eq!(s, "pa    310    100 400000   1900");
    }

    #[test]
    fn test_nested_struct_by_order() {
        let inner = Test {
            a: "Hello",
            b: 42,
            c: 3.14,
        };
        let value = Nested { db_id: 1, inner };
        let fmt = FortFormat::parse("(i2,1x,a5,1x,i2,1x,e8.3)").unwrap();
        let s = to_string(value, &fmt).unwrap();
        assert_eq!(s, " 1 Hello 42 .314E+01");
    }

    #[test]
    fn test_nested_struct_by_name() {
        let inner = Test {
            a: "Hello",
            b: 42,
            c: 3.14,
        };
        let value = Nested { db_id: 1, inner };
        let fmt = FortFormat::parse("(e8.3,1x,i2,1x,a5,1x,i1)").unwrap();
        // this gives a "missing field 'inner' error", which is what we expected for now.
        let e = to_string_with_fields(value, &fmt, &["c", "b", "a", "db_id"]);
        assert!(e.is_err());
    }

    #[test]
    fn test_external_enums() {
        #[derive(Debug, serde::Serialize)]
        enum External {
            Alpha(i32),
            Beta(i32),
        }

        let ex_vec = vec![External::Alpha(12), External::Beta(24)];
        let ex_ff = FortFormat::parse("(2(a6,i3))").unwrap();
        let ex_s = to_string(&ex_vec, &ex_ff).unwrap();
        assert_eq!(ex_s, " Alpha 12  Beta 24");
    }

    #[test]
    fn test_internal_enums() {
        #[derive(Debug, serde::Serialize)]
        #[serde(tag = "type")]
        enum Internal {
            Alpha { value: i32 },
            Beta { value: i32 },
        }

        let in_vec = vec![Internal::Alpha { value: 12 }, Internal::Beta { value: 24 }];
        let in_ff = FortFormat::parse("(2(a6,i3))").unwrap();
        let in_s = to_string(&in_vec, &in_ff).unwrap();
        assert_eq!(in_s, " Alpha 12  Beta 24");

        let fields = ["type", "value"];
        let ff = FortFormat::parse("(a5,1x,i2)").unwrap();
        let s = to_string_with_fields(Internal::Alpha { value: 42 }, &ff, &fields).unwrap();
        // TODO: ideally we want the tag field to be sorted according to the field names
        // - need to better understand how different enum representations are handled.
        assert_ne!(s, "42 Alpha");
    }

    #[test]
    fn test_adjacent_enums() {
        #[derive(Debug, serde::Serialize)]
        #[serde(tag = "key", content = "value")]
        enum Adjacent {
            Alpha(i32),
            Beta(i32),
        }

        let adj_vec = vec![Adjacent::Alpha(12), Adjacent::Beta(24)];
        let adj_ff = FortFormat::parse("(2(a6,i3))").unwrap();
        let adj_s = to_string(&adj_vec, &adj_ff).unwrap();
        assert_eq!(adj_s, " Alpha 12  Beta 24");
    }

    #[test]
    fn test_untagged_enums() {
        #[derive(Debug, serde::Serialize)]
        #[serde(untagged)]
        enum Untagged {
            Alpha(i32),
            Beta(f32),
        }

        let un_vec = vec![Untagged::Alpha(12), Untagged::Beta(24.0)];
        let un_ff = FortFormat::parse("(i3,f6.1)").unwrap();
        let un_s = to_string(&un_vec, &un_ff).unwrap();
        assert_eq!(un_s, " 12  24.0");
    }

    #[test]
    fn test_enum_into() {
        #[derive(Debug, Clone, serde::Serialize)]
        #[serde(into = "String")]
        enum InstrumentValue {
            Nothing,
            Value(f32),
            ValueWithError(f32, f32),
        }

        impl From<InstrumentValue> for String {
            fn from(value: InstrumentValue) -> Self {
                match value {
                    InstrumentValue::Nothing => "Nothing".to_string(),
                    InstrumentValue::Value(v) => format!("{v:.3}"),
                    InstrumentValue::ValueWithError(v, e) => format!("{v:.1}+/-{e:.1}"),
                }
            }
        }

        let values = [
            InstrumentValue::Nothing,
            InstrumentValue::Value(1.0),
            InstrumentValue::ValueWithError(2.0, 0.2),
        ];
        let ff = FortFormat::parse("(3a12)").unwrap();
        let s = to_string(values, &ff).unwrap();
        assert_eq!(s, "     Nothing       1.000   2.0+/-0.2");
    }

    #[test]
    fn test_none_default_fill() {
        let fmt = FortFormat::parse("(a,1x,a3,1x,l1,1x,i5,1x,f8.3)").unwrap();
        let value: (
            Option<String>,
            Option<String>,
            Option<bool>,
            Option<i32>,
            Option<f32>,
        ) = (None, None, None, None, None);
        let s = to_string(value, &fmt).unwrap();
        assert_eq!(s, "* *** * ***** ********");
    }

    #[test]
    fn test_none_string_fill() {
        let settings =
            SerSettings::default().fill_method(NoneFill::String("FILL_VAL".as_bytes().to_vec()));

        let fmt = FortFormat::parse("(a,1x,a3,1x,l1,1x,i5,1x,f8.3)").unwrap();
        let value: (
            Option<String>,
            Option<String>,
            Option<bool>,
            Option<i32>,
            Option<f32>,
        ) = (None, None, None, None, None);
        let s = to_string_custom::<_, &str>(value, &fmt, None, &settings).unwrap();
        assert_eq!(s, "F FIL F FILL_ FILL_VAL");
    }

    #[test]
    fn test_none_partial_typed_fill() {
        let settings =
            SerSettings::default().fill_method(NoneFill::new_partial_typed(-999, -999.999));
        let fmt = FortFormat::parse("(a,1x,a3,1x,l1,1x,i5,1x,f8.3)").unwrap();
        let value: (
            Option<String>,
            Option<String>,
            Option<bool>,
            Option<i32>,
            Option<f32>,
        ) = (None, None, None, None, None);
        let s = to_string_custom::<_, &str>(value, &fmt, None, &settings).unwrap();
        assert_eq!(s, "* *** *  -999 -999.999");
    }

    #[test]
    fn test_none_typed_fill() {
        let settings =
            SerSettings::default().fill_method(NoneFill::new_typed(false, -999, -999.999, "N/A"));
        let fmt = FortFormat::parse("(a,1x,a3,1x,l1,1x,i5,1x,f8.3)").unwrap();
        let value: (
            Option<String>,
            Option<String>,
            Option<bool>,
            Option<i32>,
            Option<f32>,
        ) = (None, None, None, None, None);
        let s = to_string_custom::<_, &str>(value, &fmt, None, &settings).unwrap();
        assert_eq!(s, "N/A N/A F  -999 -999.999");
    }

    #[test]
    fn test_unit_type_fills() {
        // Since unit types are treated the same as `None` (at least as of 19 Feb 2024)
        // we rely on the none tests to ensure that alternate fill settings work.
        #[derive(serde::Serialize)]
        struct Empty;
        let fmt = FortFormat::parse("(i5,1x,f8.3)").unwrap();
        let value = ((), Empty);
        let s = to_string(value, &fmt).unwrap();
        assert_eq!(s, "***** ********");
    }

    #[test]
    fn test_multi_rec_write() {
        let values = [
            ("pa", 45.945, -90.273),
            ("db", -12.45, 130.93),
            ("lh", -45.038, 169.684),
        ];
        let fmt = FortFormat::parse("(a2,1x,f7.3,1x,f8.3)").unwrap();
        let mut buf = vec![];
        many_to_writer_custom::<_, _, &str>(&values, &fmt, None, &SerSettings::default(), &mut buf)
            .unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert_eq!(
            s,
            "pa  45.945  -90.273\ndb -12.450  130.930\nlh -45.038  169.684\n"
        );
    }

    #[test]
    fn test_multi_rec_struct_write() {
        let values = [
            Test {
                a: "alpha",
                b: 1,
                c: 100.0,
            },
            Test {
                a: "beta",
                b: 2,
                c: 200.0,
            },
            Test {
                a: "gamma",
                b: 3,
                c: 300.0,
            },
        ];
        let fmt = FortFormat::parse("(i1,1x,f5.1,1x,a5)").unwrap();
        let mut buf = vec![];
        many_to_writer_custom(
            &values,
            &fmt,
            Some(&["b", "c", "a"]),
            &SerSettings::default(),
            &mut buf,
        )
        .unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert_eq!(s, "1 100.0 alpha\n2 200.0  beta\n3 300.0 gamma\n");
    }

    #[test]
    fn test_string_justification() {
        // There was a very tricky to find bug that meant some fields were not respecting left-alignment
        // settings, this test has a lot of the cases I went through to find it.

        // First, check that a simple sequence respects left-alignment.
        let fmt = FortFormat::parse("(a8,i2)").unwrap();
        let values = ("abcde", 42);
        let settings = SerSettings::default().align_left_str(true);
        let s = to_string_custom::<_, &str>(values, &fmt, None, &settings).unwrap();
        assert_eq!(s, "abcde   42");

        // Then a struct with a str ref - for each struct we'll test serializing to both a string and a writer,
        // with and without field names specified. (The original bug was that settings weren't passed when field
        // names were given.)
        #[derive(Debug, serde::Serialize)]
        struct Test1 {
            s: &'static str,
            n: i32,
        }

        let values = Test1 { s: "abcde", n: 42 };
        let s = to_string_custom::<_, &str>(&values, &fmt, None, &settings).unwrap();
        assert_eq!(s, "abcde   42");

        let s = to_string_custom::<_, &str>(&values, &fmt, Some(&["s", "n"]), &settings).unwrap();
        assert_eq!(s, "abcde   42");

        let mut buf = vec![];
        to_writer_custom::<_, _, &str>(&values, &fmt, None, &settings, &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert_eq!(s, "abcde   42\n");

        let mut buf = vec![];
        to_writer_custom::<_, _, &str>(&values, &fmt, Some(&["s", "n"]), &settings, &mut buf)
            .unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert_eq!(s, "abcde   42\n");

        // Next a struct with the string as an owned string
        #[derive(Debug, serde::Serialize)]
        struct Test2 {
            s: String,
            n: i32,
        }

        let values = Test2 {
            s: "abcde".to_string(),
            n: 42,
        };
        let s = to_string_custom::<_, &str>(&values, &fmt, None, &settings).unwrap();
        assert_eq!(s, "abcde   42");

        let s = to_string_custom::<_, &str>(&values, &fmt, Some(&["s", "n"]), &settings).unwrap();
        assert_eq!(s, "abcde   42");

        let mut buf = vec![];
        to_writer_custom::<_, _, &str>(&values, &fmt, None, &settings, &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert_eq!(s, "abcde   42\n");

        let mut buf = vec![];
        to_writer_custom::<_, _, &str>(&values, &fmt, Some(&["s", "n"]), &settings, &mut buf)
            .unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert_eq!(s, "abcde   42\n");

        // Then a struct with the string on an inner, flattened struct.
        #[derive(Debug, serde::Serialize)]
        struct Test3 {
            #[serde(flatten)]
            inner: Test2,
        }

        let values = Test3 { inner: values };
        let s = to_string_custom::<_, &str>(&values, &fmt, None, &settings).unwrap();
        assert_eq!(s, "abcde   42");

        let s = to_string_custom::<_, &str>(&values, &fmt, Some(&["s", "n"]), &settings).unwrap();
        assert_eq!(s, "abcde   42");

        let mut buf = vec![];
        to_writer_custom::<_, _, &str>(&values, &fmt, None, &settings, &mut buf).unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert_eq!(s, "abcde   42\n");

        let mut buf = vec![];
        to_writer_custom::<_, _, &str>(&values, &fmt, Some(&["s", "n"]), &settings, &mut buf)
            .unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert_eq!(s, "abcde   42\n");

        // Finally, confirm that a hash map (as opposed to a defined structure) behaves correctly.
        // Can't reliably test without field names specified because HashMaps are unordered.
        let hfmt = FortFormat::parse("(a8,a2)").unwrap();
        let values =
            HashMap::<_, _, std::hash::RandomState>::from_iter(vec![("s", "abcde"), ("n", "42")]);

        let s = to_string_custom::<_, &str>(&values, &hfmt, Some(&["s", "n"]), &settings).unwrap();
        assert_eq!(s, "abcde   42");

        let mut buf = vec![];
        to_writer_custom::<_, _, &str>(&values, &hfmt, Some(&["s", "n"]), &settings, &mut buf)
            .unwrap();
        let s = String::from_utf8(buf).unwrap();
        assert_eq!(s, "abcde   42\n");
    }
}