powerio-tx 0.10.0

Compiler infrastructure for power systems: parse, convert, validate, and lower grid models
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
//! Readers and writers for supported case formats, all meeting at [`BalancedNetwork`].
//!
//! Each format module owns its reader and/or writer: MATPOWER `.m`,
//! PowerModels JSON, PSS/E `.raw`, PowerWorld `.aux`, egret `ModelData` JSON,
//! pandapower JSON, PyPSA CSV folders, PSLF `.epc`, GO Challenge 3 JSON, and
//! Surge JSON, and DeepMind OPFData JSON. PowerWorld `.pwb` cases, GO Challenge
//! 3 and OPFData JSON canonical output, and PowerWorld `.pwd` displays are read
//! only. Case input and
//! output formats meet here, so adding a writable format is one module plus
//! one hub registration.
//! [`parse`] reads a retained source into a typed module, detecting the
//! format from the source name and content; [`parse_display_file`] reads
//! display artifacts such as PowerWorld `.pwd`. [`write_as`] writes a parsed
//! module, echoing the retained source on a same format target, and
//! [`write_network`] is the semantic write for bare typed networks.
//! Non-finite numeric values, such as MATPOWER `Inf`/`NaN` angle limits, are
//! written as JSON `null`.
//!
//! # Fidelity behavior
//!
//! Conversion is two-tier:
//!
//! - **Same format writes of an unchanged parsed module return the original
//!   bytes.** The module retains its source, so [`write_as`] back to the same
//!   format returns every field, comment, and numeric token.
//! - **Cross-format keeps maximal fidelity with itemized loss.** Whatever the
//!   target format cannot represent is reported in the [`Conversion`]
//!   findings, never dropped silently. On the read side, readers itemize what
//!   they ignore on the module's diagnostics.

use std::collections::{BTreeSet, HashMap};
use std::fmt;
use std::str::FromStr;

use serde_json::{Map, Value};

use powerio_core::{PioModule, SourceDescriptor};

use crate::diagnostics::{Diagnostic, DiagnosticInfo, Diagnostics, EmitFamily, codes};
use crate::gen_cost::{GenCostPatch, MissingGenCostPolicy};
use crate::network::{BalancedNetwork, Branch, BranchRatingSet, Bus, BusId, BusType, SourceFormat};
use crate::{Error, Result};
use routing::{Detection, JsonClass, SourceFormat as DetectedFormat, TransmissionFormat};

mod decode;
mod egret;
#[doc(hidden)]
pub mod goc3;
mod matpower;
mod opfdata;
mod pandapower;
mod powermodels;
pub mod powerworld;
mod pslf;
mod psse;
mod pypsa;
pub mod routing;
mod surge;

pub use egret::{egret_declares_time_series, parse_egret_time_series, write_egret_json};
pub use goc3::parse_goc3_json;
pub use matpower::write_matpower;
pub use opfdata::{OpfDataSolution, parse_opfdata_json};
pub use pandapower::write_pandapower_json;
pub use powermodels::write_powermodels_json;
pub use powerworld::{PwdDisplay, PwdSubstation, write_powerworld};
pub use pslf::write_pslf;
pub use psse::{write_psse, write_psse_rev};
pub use pypsa::{
    PypsaAxis, PypsaCsvOutputs, PypsaCsvSequence, parse_pypsa_csv_time_series, pypsa_axis,
    write_pypsa_csv_folder,
};
pub use surge::write_surge_json;

/// A target case format. See [`write_as`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum TargetFormat {
    /// PowerModels.jl network data JSON.
    PowerModelsJson,
    /// egret `ModelData` JSON.
    EgretJson,
    /// PSS/E `.raw` at the given revision. `rev` selects the record layout the
    /// writer emits (33, 34, or 35); 33 is the historical default. The reader
    /// takes the revision from the file header, so this only affects writes.
    Psse { rev: u32 },
    /// PowerWorld auxiliary `.aux`.
    PowerWorld,
    /// pandapower `pandapowerNet` JSON.
    PandapowerJson,
    /// MATPOWER `.m` (round-trip; byte-exact when the case kept its source).
    Matpower,
    /// GE PSLF `.epc` (round-trip; byte-exact when the case kept its source).
    Pslf,
    /// DOE GO Challenge 3 JSON input data. This is read only except for
    /// same format source echo when the parsed network still carries its source.
    Goc3Json,
    /// Surge native JSON network document.
    SurgeJson,
    /// One JSON document from a DeepMind OPFData release. Read only except for
    /// an exact write back to the retained source format.
    DeepMindOpfDataJson,
}

impl TargetFormat {
    /// Conventional file extension for this format (no leading dot).
    #[must_use]
    pub fn extension(self) -> &'static str {
        match self {
            TargetFormat::PowerModelsJson
            | TargetFormat::EgretJson
            | TargetFormat::PandapowerJson
            | TargetFormat::Goc3Json
            | TargetFormat::SurgeJson
            | TargetFormat::DeepMindOpfDataJson => "json",
            TargetFormat::Psse { .. } => "raw",
            TargetFormat::PowerWorld => "aux",
            TargetFormat::Matpower => "m",
            TargetFormat::Pslf => "epc",
        }
    }

    /// Human-readable format name for diagnostics.
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            TargetFormat::PowerModelsJson => "PowerModels JSON",
            TargetFormat::EgretJson => "egret JSON",
            TargetFormat::Psse { .. } => "PSS/E .raw",
            TargetFormat::PowerWorld => "PowerWorld .aux",
            TargetFormat::PandapowerJson => "pandapower JSON",
            TargetFormat::Matpower => "MATPOWER .m",
            TargetFormat::Pslf => "PSLF .epc",
            TargetFormat::Goc3Json => "GO Challenge 3 JSON",
            TargetFormat::SurgeJson => "Surge JSON",
            TargetFormat::DeepMindOpfDataJson => "DeepMind OPFData JSON",
        }
    }

    /// Canonical API token for this format.
    #[must_use]
    pub fn token(self) -> &'static str {
        match self {
            TargetFormat::PowerModelsJson => "powermodels-json",
            TargetFormat::EgretJson => "egret-json",
            TargetFormat::Psse { rev: 34 } => "psse34",
            TargetFormat::Psse { rev: 35 } => "psse35",
            TargetFormat::Psse { .. } => "psse",
            TargetFormat::PowerWorld => "powerworld",
            TargetFormat::PandapowerJson => "pandapower-json",
            TargetFormat::Matpower => "matpower",
            TargetFormat::Pslf => "pslf",
            TargetFormat::Goc3Json => "goc3-json",
            TargetFormat::SurgeJson => "surge-json",
            TargetFormat::DeepMindOpfDataJson => "opfdata-json",
        }
    }
}

impl fmt::Display for TargetFormat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.token())
    }
}

impl FromStr for TargetFormat {
    type Err = Error;

    fn from_str(name: &str) -> Result<Self> {
        target_format_from_name(name).ok_or_else(|| Error::UnknownFormat(name.to_string()))
    }
}

/// A display artifact format. These files are not power network cases and do
/// not parse to [`BalancedNetwork`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum DisplayFormat {
    /// PowerWorld oneline display `.pwd`.
    PowerWorld,
    /// The standalone geographic document ([`crate::geo::GeoLayer`]):
    /// canonical `.geo.json`, read tolerantly from GeoJSON, aliased CSV/JSON
    /// records, and headerless buscoords CSV.
    GeoJson,
}

impl DisplayFormat {
    /// Conventional file extension for this display format (no leading dot).
    #[must_use]
    pub fn extension(self) -> &'static str {
        match self {
            DisplayFormat::PowerWorld => "pwd",
            DisplayFormat::GeoJson => crate::geo::GEO_LAYER_EXTENSION,
        }
    }

    /// Human-readable format name for diagnostics.
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            DisplayFormat::PowerWorld => "PowerWorld .pwd",
            DisplayFormat::GeoJson => "geo layer",
        }
    }

    /// Canonical API token for this format.
    #[must_use]
    pub fn token(self) -> &'static str {
        match self {
            DisplayFormat::PowerWorld => "powerworld-display",
            DisplayFormat::GeoJson => "geojson",
        }
    }
}

impl fmt::Display for DisplayFormat {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.token())
    }
}

impl FromStr for DisplayFormat {
    type Err = Error;

    fn from_str(name: &str) -> Result<Self> {
        display_format_from_name(name).ok_or_else(|| Error::UnknownFormat(name.to_string()))
    }
}

/// Map a display format name to a [`DisplayFormat`], or `None` if unrecognized.
/// Accepts `pwd`, `powerworld-pwd`, and `powerworld-display`; `geojson`,
/// `geo-json`, and `geo` name the geographic layer.
#[must_use]
pub fn display_format_from_name(name: &str) -> Option<DisplayFormat> {
    Some(match name.to_ascii_lowercase().as_str() {
        "pwd" | "powerworld-pwd" | "powerworld-display" => DisplayFormat::PowerWorld,
        "geojson" | "geo-json" | "geo" => DisplayFormat::GeoJson,
        _ => return None,
    })
}

/// Map a format name (with the common aliases) to a [`TargetFormat`], or `None`
/// if unrecognized. Accepts `matpower`/`m`, `powermodels-json`/`powermodels`/`pm`,
/// `egret-json`/`egret`, `pandapower-json`/`pandapower`/`pp`, `psse`/`raw`,
/// `powerworld`/`aux`, `pslf`/`epc`, `goc3-json`/`goc3`, and
/// `surge-json`/`surge`, and `opfdata-json`/`opfdata`/`gridopt`.
/// Case-insensitive. The one place the bindings (Python, C ABI) share, so a new
/// text format means one new arm here, not three. PyPSA CSV folders, GridFM
/// datasets, and PowerWorld `.pwb` are directory or read only inputs with no
/// text target; they are routed by [`crate::format::routing`].
///
/// [`SourceFormat`]'s reported token is [`SourceFormat::name`], which resolves
/// here directly, so `net.to_format(other.source_format)` works for every
/// format. The `powermodelsjson`/`egretjson`/`pandapowerjson` aliases keep the
/// pre-0.9 camel-case spellings (`"PowerModelsJson"` lowercased) resolving for
/// callers that stored them.
#[must_use]
pub fn target_format_from_name(name: &str) -> Option<TargetFormat> {
    Some(match routing::transmission_format_from_name(name)? {
        TransmissionFormat::Matpower => TargetFormat::Matpower,
        TransmissionFormat::PowerModelsJson => TargetFormat::PowerModelsJson,
        TransmissionFormat::EgretJson => TargetFormat::EgretJson,
        TransmissionFormat::Psse => TargetFormat::Psse { rev: 33 },
        TransmissionFormat::Psse34 => TargetFormat::Psse { rev: 34 },
        TransmissionFormat::Psse35 => TargetFormat::Psse { rev: 35 },
        TransmissionFormat::PowerWorld => TargetFormat::PowerWorld,
        TransmissionFormat::PandapowerJson => TargetFormat::PandapowerJson,
        TransmissionFormat::Pslf => TargetFormat::Pslf,
        TransmissionFormat::Goc3Json => TargetFormat::Goc3Json,
        TransmissionFormat::SurgeJson => TargetFormat::SurgeJson,
        TransmissionFormat::DeepMindOpfDataJson => TargetFormat::DeepMindOpfDataJson,
        TransmissionFormat::PypsaCsv | TransmissionFormat::Pwb | TransmissionFormat::Gridfm => {
            return None;
        }
    })
}

/// Output of a display parse. PowerWorld `.pwd` produces
/// [`DisplayData::PowerWorld`]; a geographic sidecar produces
/// [`DisplayData::Geo`].
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum DisplayData {
    /// PowerWorld oneline display data.
    PowerWorld(PwdDisplay),
    /// A standalone geographic layer.
    Geo(crate::geo::GeoLayer),
}

impl DisplayData {
    /// The display format represented by this value.
    #[must_use]
    pub fn format(&self) -> DisplayFormat {
        match self {
            DisplayData::PowerWorld(_) => DisplayFormat::PowerWorld,
            DisplayData::Geo(_) => DisplayFormat::GeoJson,
        }
    }
}

fn display_file_guidance() -> Error {
    Error::UnknownFormat(
        "a PowerWorld .pwd is display data, not a BalancedNetwork case; \
         use parse_display_file(path, None)"
            .into(),
    )
}

/// Parse display bytes in the named display format `from`.
///
/// # Errors
/// [`Error::UnknownFormat`] if `from` is not a display format; otherwise the
/// reader's own [`Error`] on malformed input.
pub fn parse_display_bytes(bytes: &[u8], from: &str) -> Result<DisplayData> {
    let fmt =
        display_format_from_name(from).ok_or_else(|| Error::UnknownFormat(from.to_string()))?;
    match fmt {
        DisplayFormat::PowerWorld => Ok(DisplayData::PowerWorld(powerworld::parse_pwd_display(
            bytes,
        )?)),
        // The tolerant reader's own notes are available through
        // `GeoLayer::parse_bytes` for callers that want them.
        DisplayFormat::GeoJson => Ok(DisplayData::Geo(
            crate::geo::GeoLayer::parse_bytes(bytes, None)?.layer,
        )),
    }
}

/// Render a file extension for a user-facing message: `` extension `xyz` ``
/// when present, `no extension` otherwise.
fn describe_extension(extension: Option<&str>) -> String {
    match extension {
        Some(ext) => format!("extension `{ext}`"),
        None => "no extension".to_owned(),
    }
}

/// Parse the display file at `path`, choosing the reader from `from` or, when
/// `None`, from the extension. A `.pwd` extension selects PowerWorld display
/// data.
///
/// # Errors
/// [`Error::UnknownFormat`] if `from` is unrecognized or the extension cannot
/// be mapped; [`Error::Io`] if the file cannot be read; the reader's own
/// [`Error`] on malformed input.
pub fn parse_display_file(
    path: impl AsRef<std::path::Path>,
    from: Option<&str>,
) -> Result<DisplayData> {
    let path = path.as_ref();
    let fmt = match from {
        Some(f) => {
            display_format_from_name(f).ok_or_else(|| Error::UnknownFormat(f.to_string()))?
        }
        None => match path
            .extension()
            .and_then(|e| e.to_str())
            .map(str::to_ascii_lowercase)
            .as_deref()
        {
            Some("pwd") => DisplayFormat::PowerWorld,
            Some("geojson") => DisplayFormat::GeoJson,
            // `.geo.json` is the canonical layer name; a bare `.json` stays
            // ambiguous (it is usually a case file).
            Some("json")
                if path
                    .file_name()
                    .and_then(|name| name.to_str())
                    .is_some_and(|name| {
                        name.to_ascii_lowercase()
                            .ends_with(crate::geo::GEO_LAYER_EXTENSION)
                    }) =>
            {
                DisplayFormat::GeoJson
            }
            other => {
                return Err(Error::UnknownFormat(format!(
                    "cannot infer display format from file with {}; \
                     pass an explicit display format",
                    describe_extension(other)
                )));
            }
        },
    };
    let bytes = read_file_bytes(path)?;
    match fmt {
        DisplayFormat::PowerWorld => Ok(DisplayData::PowerWorld(powerworld::parse_pwd_display(
            &bytes,
        )?)),
        DisplayFormat::GeoJson => Ok(DisplayData::Geo(
            crate::geo::GeoLayer::parse_bytes(&bytes, path.file_name().and_then(|n| n.to_str()))?
                .layer,
        )),
    }
}

/// An I/O failure naming the path it happened on. The bare OS message ("No
/// such file or directory") reaches callers who cannot see which path the
/// library resolved, so every read here names the file.
pub(crate) fn named_io_error(path: &std::path::Path, e: &std::io::Error) -> Error {
    Error::Io(std::io::Error::new(
        e.kind(),
        format!("cannot read {}: {e}", path.display()),
    ))
}

fn read_file_bytes(path: &std::path::Path) -> Result<Vec<u8>> {
    std::fs::read(path).map_err(|e| named_io_error(path, &e))
}

/// Whether a format name means a PyPSA CSV folder. PyPSA folders are directory
/// inputs, not text targets, so they have no [`TargetFormat`] arm; this is the
/// companion alias matcher to [`target_format_from_name`] and the one place the
/// PyPSA aliases live.
pub fn is_pypsa_csv_name(name: &str) -> bool {
    matches!(
        name.to_ascii_lowercase().replace(['-', '_'], "").as_str(),
        "pypsacsv" | "pypsa"
    )
}

/// Whether a source format name means PSLF EPC.
fn is_pslf_name(name: &str) -> bool {
    matches!(
        name.to_ascii_lowercase().replace(['-', '_'], "").as_str(),
        "pslf" | "epc" | "pslfepc"
    )
}

/// Parse the case file at `path`, choosing the reader from `from` (the
/// [`target_format_from_name`] names plus `pypsa-csv`/`pypsa`, `pwb`, `pslf`,
/// and `epc`) or, when `None`, from the path: a directory containing
/// `network.csv` parses as a PyPSA CSV folder (any other directory is refused
/// as a directory with [`Error::UnknownFormat`], before extension inference),
/// and a file maps by extension (`m`/`json`/`raw`/`aux`/`pwb`/`epc`),
/// case insensitively (issue #97: `.RAW` is as common as `.raw` in the wild). A
/// `.json` file is classified by top level shape markers: pandapower
/// (`"_class": "pandapowerNet"`), egret (`elements` and `system`), GO Challenge
/// 3 (`network` plus `time_series_input`/`reliability`), Surge JSON
/// (`format: "surge-json"`), OPFData (`grid`, `solution`, and `metadata`), and
/// PowerModels JSON (`baseMVA`, `branch`, `gen`, or `gencost`). JSON matching
/// model JSON markers (`buses` plus a network key), distribution markers,
/// ambiguous markers, or no known markers returns [`Error::UnknownFormat`].
/// Declare a format on the source to force a parser. PowerWorld `.pwb` is a
/// binary read only format; PSLF `.epc` is text and has a writer. Returns the
/// typed module: the network value, the reader's findings, and the retained
/// source.
///
/// The one parser the CLI and the Python/C/Julia bindings share, so adding a
/// source format is one edit here, not one per binding.
///
/// # Errors
/// A `Request` failure when the format cannot be determined or is refused, an
/// `Io` failure when acquisition fails, and the reader's own failure on
/// malformed input. Findings collected before a failure ride the returned
/// error.
///
pub fn parse(
    source: powerio_core::Source,
) -> std::result::Result<PioModule<BalancedNetwork>, powerio_core::Error> {
    parse_with_json_class(source, None)
}

/// [`parse`], given a JSON classification the caller already computed on the
/// same bytes. The `powerio` facade routes a source by its own call to
/// [`routing::classify_json_text`] before it ever reaches this crate; when
/// that routing lands on the balanced hub, passing the result here skips the
/// second classification [`parse`] would otherwise run over the identical
/// text. `None` reproduces [`parse`] exactly, classifying inline only if and
/// when [`parse_to_network`] needs to.
///
/// Not part of this crate's public reading surface — the facade is the one
/// caller with a classification already in hand — so this stays out of the
/// rendered docs.
///
/// # Errors
/// Same as [`parse`].
#[doc(hidden)]
pub fn parse_with_json_class(
    source: powerio_core::Source,
    json_class: Option<routing::JsonClass>,
) -> std::result::Result<PioModule<BalancedNetwork>, powerio_core::Error> {
    let mut warnings = Diagnostics::new();
    match parse_to_network(&source, &mut warnings, json_class) {
        Ok(network) => {
            let format = network.source_format();
            let mut module = PioModule::new(network);
            for buffer in source.acquired_buffers() {
                // A stored descriptor is a display name, not a filesystem
                // path; keep only the final component of a buffer name that
                // came from Source::open.
                let name = std::path::Path::new(buffer.name())
                    .file_name()
                    .and_then(|name| name.to_str())
                    .unwrap_or_else(|| buffer.name());
                let descriptor = match SourceDescriptor::new(
                    buffer.id().clone(),
                    name,
                    buffer.bytes().len() as u64,
                ) {
                    Ok(descriptor) => descriptor,
                    Err(error) => return Err(error.with_source(source)),
                };
                // SourceFormat::name is always a valid format id; skipping
                // on the impossible error keeps this path panic free.
                let descriptor = match powerio_core::FormatId::new(format.name()) {
                    Ok(id) => descriptor.with_format(id),
                    Err(_) => descriptor,
                };
                if let Err(error) = module.add_source_descriptor(descriptor) {
                    return Err(error.with_source(source));
                }
            }
            let mut module = module.with_source(source);
            for record in warnings.into_records() {
                module.add_diagnostic(record)?;
            }
            Ok(module)
        }
        Err(error) => {
            let core = powerio_core::Error::new(error.code(), error.to_string());
            Err(core
                .with_diagnostics(warnings.into_records())
                .with_cause(error)
                .with_source(source))
        }
    }
}

/// The format dispatch behind [`parse`]: name and content detection, then the
/// one reader map. `json_class` is a classification the caller already
/// computed on this source's own text ([`parse_with_json_class`]); when it is
/// `None`, this classifies inline at the point a `.json` source needs it,
/// exactly as [`parse`] always has.
fn parse_to_network(
    source: &powerio_core::Source,
    warnings: &mut Diagnostics,
    json_class: Option<routing::JsonClass>,
) -> Result<BalancedNetwork> {
    let from = source.format().map(powerio_core::FormatId::as_str);
    let path = std::path::Path::new(source.name());
    // The file stem is the name hint for formats that don't carry their own
    // name. An angle bracketed source name is the conventional non-file
    // spelling an anonymous in-memory caller uses and carries no hint; a name
    // with an extension contributes its stem, and any other name is the hint
    // itself.
    let stem = if source.name().starts_with('<') {
        None
    } else if path.extension().is_some() {
        path.file_stem().and_then(|stem| stem.to_str())
    } else {
        Some(source.name())
    };
    // PyPSA CSV folders are directories, not files; dispatch them before any
    // extension logic. `from` accepts the pypsa aliases, and a bare directory
    // source with a `network.csv` auto-detects.
    if source.is_directory() {
        let marker = powerio_core::ArtifactPath::new("network.csv")
            .expect("static name is a valid artifact path");
        if from.is_some_and(is_pypsa_csv_name) || (from.is_none() && source.buffer(&marker).is_ok())
        {
            return pypsa::read_pypsa_csv_source(source, warnings);
        }
        // Any other directory has no reader; refuse it as a directory before
        // the extension logic reads ".07" off a name like `pglib-opf-23.07`.
        return Err(Error::UnknownFormat(format!(
            "{} is a directory, and the only directory case format is a PyPSA CSV \
             folder (one holding a network.csv); pass a case file",
            path.display()
        )));
    }
    if from.is_some_and(is_pypsa_csv_name) {
        return Err(Error::UnknownFormat(
            "a PyPSA CSV case is a directory holding a network.csv; open the folder as the source"
                .into(),
        ));
    }
    // PowerWorld `.pwb` is binary and read only; dispatch it before the text
    // read. `from` accepts "pwb" for files with a different extension.
    let ext = path
        .extension()
        .and_then(|e| e.to_str())
        .map(str::to_ascii_lowercase);
    if from.is_some_and(|f| f.eq_ignore_ascii_case("pwb"))
        || (from.is_none() && ext.as_deref() == Some("pwb"))
    {
        // Binary input: the exact bytes go to the reader, byte order mark
        // handling included, since the mark is a text concept.
        let buffer = primary(source)?;
        return powerworld::parse_pwb_collecting(buffer.bytes(), stem, warnings);
    }
    if from.is_some_and(is_pslf_name) || (from.is_none() && ext.as_deref() == Some("epc")) {
        let buffer = primary(source)?;
        let network = pslf::parse_pslf_source(source_text(&buffer)?, stem, warnings)?;
        reject_empty_case(&network, "PSLF .epc")?;
        return Ok(network);
    }
    if from
        .and_then(target_format_from_name)
        .is_some_and(|format| format == TargetFormat::DeepMindOpfDataJson)
        && matches!(ext.as_deref(), Some("pt" | "gz"))
    {
        return Err(Error::UnknownFormat(
            "OPFData .pt tensor caches and .tar.gz archives are not case files; extract and parse an example_N.json source file"
                .into(),
        ));
    }
    // Settle the format before touching the file: an unmapped or binary
    // extension must surface as UnknownFormat, not as the UTF-8 read error
    // the text formats' loader would hit first. `.pwd` gets its own arm
    // because the display sibling ships next to every case file in the wild
    // and carries no case data.
    if from.is_none() && ext.as_deref() == Some("pwd") {
        return Err(display_file_guidance());
    }
    let fmt_hint = match from {
        Some(f) => {
            if display_format_from_name(f).is_some() {
                return Err(display_file_guidance());
            }
            Some(target_format_from_name(f).ok_or_else(|| unknown_source_format(f))?)
        }
        None => {
            // Everything but `.json` (sniffed below) resolves without the text.
            match ext.as_deref() {
                Some("m") => Some(TargetFormat::Matpower),
                Some("raw") => Some(TargetFormat::Psse { rev: 33 }),
                Some("aux") => Some(TargetFormat::PowerWorld),
                Some("json") => None,
                Some("dss") => return Err(unknown_source_format("dss")),
                other => {
                    // A nameless or oddly named source can still carry a JSON
                    // document (in-memory text has no extension to state);
                    // sniff it like a `.json` before refusing. The primary
                    // buffer is already retained, so peeking is free.
                    let jsonish = source.primary_buffer().is_ok_and(|buffer| {
                        source_text(&buffer)
                            .is_ok_and(|text| text.trim_start().starts_with(['{', '[']))
                    });
                    if jsonish {
                        None
                    } else {
                        return Err(Error::UnknownFormat(format!(
                            "cannot infer from source name with {}; \
                             declare a source format",
                            describe_extension(other)
                        )));
                    }
                }
            }
        }
    };
    // The parser decodes a byte order mark free slice of the one retained
    // buffer; the module keeps the exact original bytes for same format
    // writing. Sniffing a `.json` borrows the same slice.
    let buffer = primary(source)?;
    let text = source_text(&buffer)?;
    let fmt = match fmt_hint {
        Some(fmt) => fmt,
        // A caller ahead of this (the `powerio` facade's own routing) may
        // already have classified this exact text; trust that answer instead
        // of running the same classification a second time. `unwrap_or_else`
        // only classifies here when nothing did yet, so a caller with no
        // hint (every direct `parse` caller) behaves exactly as before.
        None => match json_class.unwrap_or_else(|| routing::classify_json_text(text)) {
            // The network serialization is not a case format, but it parses:
            // a bare model JSON document decodes through `from_json` and
            // routes to `BalancedNetwork` like any other balanced source.
            JsonClass::ModelJson => return BalancedNetwork::from_json(text),
            class => json_target_from_class(class)?,
        },
    };
    read_source(text, fmt, stem, warnings)
}

/// The primary buffer of a file or memory source.
fn primary(source: &powerio_core::Source) -> Result<powerio_core::SourceBuffer> {
    source.primary_buffer().map_err(|error| Error::FormatRead {
        format: "source",
        message: error.to_string(),
    })
}

/// The text a reader decodes: the buffer's byte order mark free slice,
/// validated as UTF-8.
fn source_text(buffer: &powerio_core::SourceBuffer) -> Result<&str> {
    std::str::from_utf8(buffer.content_bytes()).map_err(|e| Error::FormatRead {
        format: "case text",
        message: format!("not valid UTF-8: {e}"),
    })
}

/// Read decoded `text` as `fmt`, using `name_hint` (e.g. the file stem) when
/// the format carries no name of its own. The single format to reader map:
/// every parse route funnels through it, so every format is dispatched the
/// same way. Readers borrow the text; the module retains the source bytes.
fn read_source(
    text: &str,
    fmt: TargetFormat,
    name_hint: Option<&str>,
    warnings: &mut Diagnostics,
) -> Result<BalancedNetwork> {
    let net = match fmt {
        TargetFormat::Matpower => matpower::parse_matpower_source(text, name_hint),
        TargetFormat::PowerModelsJson => {
            powermodels::parse_powermodels_json_source(text, name_hint, warnings)
        }
        TargetFormat::Psse { .. } => psse::parse_psse_source(text, name_hint, warnings),
        TargetFormat::PowerWorld => {
            powerworld::map::parse_powerworld_source(text, name_hint, warnings)
        }
        TargetFormat::EgretJson => egret::parse_egret_source(text, name_hint),
        TargetFormat::PandapowerJson => {
            pandapower::parse_pandapower_source(text, name_hint, warnings)
        }
        // PSLF read normally enters through the `is_pslf_name`/`.epc` fast
        // path in the dispatch; this arm keeps the funnel total.
        TargetFormat::Pslf => pslf::parse_pslf_source(text, name_hint, warnings),
        // The general parse takes the network and drops the typed problem
        // document; the calculation instance this source declares arrives
        // with the instance types.
        TargetFormat::Goc3Json => {
            goc3::parse_goc3_source(text, name_hint, warnings).map(|(net, _goc3)| net)
        }
        TargetFormat::SurgeJson => surge::parse_surge_source(text, name_hint, warnings),
        TargetFormat::DeepMindOpfDataJson => {
            opfdata::parse_opfdata_source(text, name_hint, warnings)
        }
    }?;
    reject_empty_case(&net, fmt.label())?;
    Ok(net)
}

/// Geographic metadata for a reader that harvested longitude/latitude
/// coordinates: `Some` once any bus carries a location, so a case without
/// coordinates serializes exactly as before. The space is stamped geographic
/// only when every point fits longitude/latitude bounds; a source that
/// violates its format's own convention (projected meters in a pandapower
/// `geo` column) reads as unknown instead of claiming WGS84.
pub(crate) fn geographic_meta(buses: &[Bus]) -> Option<crate::geo::GeoMeta> {
    let mut located = buses.iter().filter_map(|bus| bus.location).peekable();
    located.peek()?;
    let in_bounds = located.all(|location| location.x.abs() <= 180.0 && location.y.abs() <= 90.0);
    Some(crate::geo::GeoMeta {
        space: if in_bounds {
            crate::geo::CoordinateSpace::Geographic { crs: None }
        } else {
            crate::geo::CoordinateSpace::Unknown
        },
        kind: None,
    })
}

/// A source id from an f64: an in range value truncates the way the readers
/// always have; a negative, non-finite, or over-ceiling value is refused with
/// a message naming `column`, instead of letting the `as usize` cast saturate.
/// The ceiling is [`crate::network::BusId::MAX`] (`i64::MAX`, the C ABI id
/// bound), applied to every id column so a non-bus id gets the same policy.
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
pub(crate) fn id_from_f64(
    value: f64,
    column: impl std::fmt::Display,
) -> std::result::Result<usize, String> {
    // Strict `<`: `i64::MAX as f64` rounds up to 2^63, so `<=` would admit
    // values the cast saturates past `BusId::MAX`.
    if value >= 0.0 && value < i64::MAX as f64 {
        Ok(value as usize)
    } else {
        // Debug keeps the shortest float form ("1e300", never 301 digits).
        Err(format!(
            "`{column}` value {value:?} is outside the id range 0..2^63"
        ))
    }
}

/// A case with no buses is content-free for every consumer. Most readers
/// already reject it on a missing required table, but a JSON carrying only
/// `baseMVA` would otherwise parse to a hollow network; reject it in the
/// [`read_source`] funnel so every parse path (file and in-memory) is guarded,
/// and in the PyPSA folder reader, which bypasses the funnel.
pub(crate) fn reject_empty_case(net: &BalancedNetwork, format: &'static str) -> Result<()> {
    if net.buses().is_empty() {
        return Err(Error::FormatRead {
            format,
            message: "case has no buses".into(),
        });
    }
    Ok(())
}

/// The source format names [`parse`] accepts as a declared format, each with
/// its aliases. The unknown format error prints this list, and a test walks
/// every alias through [`routing::transmission_format_from_name`] so it
/// cannot drift from the matcher. `pypsa-csv` names a directory source and
/// `pwb` a binary one; every other name reads file and memory sources alike.
pub const SOURCE_FORMAT_NAMES: &str = "matpower/m, powermodels-json/powermodels/pm, \
     egret-json/egret, psse/raw, psse34, psse35, powerworld/aux, \
     pandapower-json/pandapower/pp, pslf/epc, pypsa-csv/pypsa, pwb, goc3-json/goc3, \
     surge-json/surge, opfdata-json/opfdata/gridopt";

/// An unrecognized source format token. When the token names a distribution
/// format (`dss`, `pmd`, `bmopf`), the error points at the distribution
/// surface instead of echoing the token: this parser reads only balanced
/// transmission formats. Otherwise the refusal enumerates the accepted names.
fn unknown_source_format(name: &str) -> Error {
    if name.eq_ignore_ascii_case("powerio-json") {
        return Error::UnknownFormat(
            "the `powerio-json` token was retired in 0.9.0: model JSON is not a case \
             format or a conversion target; write it with `to_json` \
             (`pio_balanced_network_to_json` in C, `json_format model-json` on the MCP \
             server), store the case as `.pio.json`, and classify a JSON document with \
             `classify_json_text` (family `model-json`)"
                .into(),
        );
    }
    if let Some(dist) = routing::distribution_format_from_name(name) {
        return Error::UnknownFormat(format!(
            "`{}` is a distribution format, and this parser reads only balanced \
             transmission formats; parse it through the one module family \
             (powerio::parse in Rust, pio_parse_file in C, powerio.parse in Python, \
             parse_file in Julia), which routes distribution formats",
            dist.name()
        ));
    }
    Error::UnknownFormat(format!("{name}; accepted names: {SOURCE_FORMAT_NAMES}"))
}

/// The JSON formats share the `.json` extension, so an explicit source format
/// isn't always given. Classification lives here so the CLI and bindings use
/// the same top level markers as the Rust parsers.
#[cfg(test)]
fn sniff_json(text: &str) -> Result<TargetFormat> {
    json_target_from_class(routing::classify_json_text(text))
}

/// The case format a JSON classification selects; the shapes that are not
/// case formats are refused with the surface that reads them named. Model
/// JSON never reaches this from `parse`, which decodes it directly.
fn json_target_from_class(class: JsonClass) -> Result<TargetFormat> {
    match class {
        JsonClass::Module => Err(Error::UnknownFormat(
            "JSON is a .pio.json stored module; read it with the module surface \
             (powerio::parse in Rust, pio_parse_str in C, powerio.parse in \
             Python, parse_bytes in Julia)"
                .into(),
        )),
        JsonClass::ModelJson => Err(Error::UnknownFormat(
            "JSON is bare powerio model JSON, which is not a case format; read it with \
             BalancedNetwork::from_json (pio_balanced_network_from_json in C, \
             powerio.from_json in Python, from_json in Julia)"
                .into(),
        )),
        JsonClass::Case(Detection::Known(DetectedFormat::Transmission(format))) => {
            transmission_json_target(format)
        }
        JsonClass::Case(Detection::Known(DetectedFormat::Distribution(format))) => {
            Err(Error::UnknownFormat(format!(
                "JSON looks like distribution `{}`; use the distribution parser or pass an explicit transmission format",
                format.name()
            )))
        }
        JsonClass::Case(Detection::Ambiguous) => Err(Error::UnknownFormat(
            "ambiguous JSON markers; pass an explicit source format".into(),
        )),
        JsonClass::Case(Detection::Unknown) => Err(Error::UnknownFormat(
            "cannot infer JSON format; pass an explicit source format".into(),
        )),
    }
}

fn transmission_json_target(format: TransmissionFormat) -> Result<TargetFormat> {
    match format {
        TransmissionFormat::PowerModelsJson => Ok(TargetFormat::PowerModelsJson),
        TransmissionFormat::EgretJson => Ok(TargetFormat::EgretJson),
        TransmissionFormat::PandapowerJson => Ok(TargetFormat::PandapowerJson),
        TransmissionFormat::Goc3Json => Ok(TargetFormat::Goc3Json),
        TransmissionFormat::SurgeJson => Ok(TargetFormat::SurgeJson),
        TransmissionFormat::DeepMindOpfDataJson => Ok(TargetFormat::DeepMindOpfDataJson),
        other => Err(Error::UnknownFormat(format!(
            "JSON classifier returned non-JSON transmission format `{}`",
            other.name()
        ))),
    }
}

/// Output of a conversion: the serialized text plus the fidelity findings:
/// data the target can't represent, defaults synthesized, or blocks mapped
/// best effort. Empty `diagnostics` means a faithful conversion. For
/// [`convert_file`] and [`convert_str`], `diagnostics` carries the read side
/// findings ahead of the write side. Warning is one diagnostic severity;
/// rendered text lines come from [`crate::diagnostics::render_diagnostics`].
///
/// `#[non_exhaustive]`: a returns-only type, so downstream code reads it but
/// never constructs it, leaving room to add fidelity metadata without a breaking
/// change.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Conversion {
    pub text: String,
    /// The findings as structured records: a stable code, a severity, and a
    /// message.
    pub diagnostics: Vec<Diagnostic>,
}

impl Conversion {
    pub(crate) fn new(text: String, diagnostics: Diagnostics) -> Self {
        Self {
            text,
            diagnostics: diagnostics.into_records(),
        }
    }

    /// A conversion that dropped nothing, e.g. a same-format echo.
    pub(crate) fn faithful(text: String) -> Self {
        Self::new(text, Diagnostics::new())
    }

    /// The findings as `CODE: message` lines, rendered on request. Warning is
    /// one diagnostic severity; there is no separately stored text channel.
    #[must_use]
    pub fn rendered_diagnostics(&self) -> Vec<String> {
        crate::diagnostics::render_diagnostics(&self.diagnostics)
    }

    /// Record one finding after the writer has run.
    pub(crate) fn push(&mut self, info: &'static DiagnosticInfo, message: impl Into<String>) {
        self.diagnostics.push(Diagnostic::of(info, message));
    }

    /// Put the read side's findings ahead of the write side's.
    pub(crate) fn prepend(&mut self, read: Vec<Diagnostic>) {
        let mut records = read;
        records.append(&mut self.diagnostics);
        self.diagnostics = records;
    }
}

/// Optional write-time policies layered on top of the neutral [`BalancedNetwork`].
///
/// The default is a no-op and preserves the old `write_as` / `convert_*`
/// behavior. Non-default options work on a cloned network and never mutate the
/// caller's case.
#[derive(Debug, Clone, Default)]
pub struct WriteOptions {
    pub missing_gen_cost: MissingGenCostPolicy,
    pub gen_cost_patches: Vec<GenCostPatch>,
}

impl WriteOptions {
    #[must_use]
    pub fn is_default(&self) -> bool {
        self.missing_gen_cost.is_preserve() && self.gen_cost_patches.is_empty()
    }
}

/// Write a parsed module to `format`. Writing back to the source format of an
/// unchanged parsed module returns the retained source bytes exactly,
/// including a byte order mark; any other target serializes the typed value.
///
/// # Errors
/// [`Error::WriteUnsupported`] for a read only target, and the writer's own
/// [`Error`] on a case it cannot state.
pub fn write_as(
    module: &PioModule<BalancedNetwork>,
    format: TargetFormat,
) -> std::result::Result<Conversion, powerio_core::Error> {
    if let Some(text) = echo_text(module, format) {
        return Ok(Conversion::faithful(text));
    }
    let mut conv = write_conversion(module.value(), format).map_err(core_error)?;
    warn_psse_downgrade(module, format, &mut conv);
    Ok(conv)
}

/// Project a crate failure onto the common operation failure type.
pub(crate) fn core_error(error: Error) -> powerio_core::Error {
    let message = error.to_string();
    powerio_core::Error::new(error.code(), message).with_cause(error)
}

/// The retained source text when writing `module` back to its source format:
/// the echo that reproduces the input byte for byte. `None` sends the write
/// down the semantic path.
fn echo_text(module: &PioModule<BalancedNetwork>, target: TargetFormat) -> Option<String> {
    let source = module.source()?;
    let buffer = source.primary_buffer().ok()?;
    if !same_format(target, module.value().source_format()) {
        return None;
    }
    let text = std::str::from_utf8(buffer.bytes()).ok()?;
    // A PSS/E source echoes only when the requested revision equals the
    // source's own; any other revision goes through write_psse_rev so the
    // caller gets the layout it asked for instead of the original bytes.
    if let TargetFormat::Psse { rev } = target
        && psse::header_rev(text.trim_start_matches('\u{feff}')) != rev
    {
        return None;
    }
    Some(text.to_owned())
}

/// Serialize a typed network to `format` with no source echo: the semantic
/// write used for values constructed in memory or severed from their module.
///
/// # Errors
/// As [`write_as`].
pub fn write_network(
    net: &BalancedNetwork,
    format: TargetFormat,
) -> std::result::Result<Conversion, powerio_core::Error> {
    write_conversion(net, format).map_err(core_error)
}

pub(crate) fn write_conversion(net: &BalancedNetwork, format: TargetFormat) -> Result<Conversion> {
    let mut conv = match format {
        TargetFormat::PowerModelsJson => write_powermodels_json(net),
        TargetFormat::EgretJson => write_egret_json(net),
        TargetFormat::Psse { rev } => write_psse_rev(net, rev),
        TargetFormat::PowerWorld => write_powerworld(net),
        TargetFormat::PandapowerJson => write_pandapower_json(net),
        // From another source (or no retained source): canonical MATPOWER from
        // the folded model, which itemizes what it can't carry (HVDC, gen caps,
        // extras, a partial-cost case).
        TargetFormat::Matpower => matpower::write_matpower_conversion(net),
        TargetFormat::Pslf => write_pslf(net),
        TargetFormat::SurgeJson => write_surge_json(net),
        TargetFormat::Goc3Json => {
            return Err(Error::WriteUnsupported {
                format: "goc3-json",
            });
        }
        TargetFormat::DeepMindOpfDataJson => {
            return Err(Error::WriteUnsupported {
                format: "opfdata-json",
            });
        }
    };
    warn_normalized_tap(net, format, &mut conv);
    warn_missing_reference(net, format, &mut conv);
    warn_dropped_frequency(net, format, &mut conv);
    warn_dropped_locations(net, format, &mut conv);
    warn_dropped_transformer_charging(net, format, &mut conv);
    Ok(conv)
}

/// Write a parsed module to `format` through a destination: the one write
/// operation over file, memory, and (for the directory formats) folder
/// output. Every text target commits a single artifact — a path destination
/// names the exact file, a memory destination names the artifact — staged
/// and renamed into place so a failed write never exposes a partial target.
/// The result carries the complete artifact inventory and the writer's
/// findings. PyPSA CSV folders write through [`write_pypsa_csv`].
///
/// # Errors
/// As [`write_as`], plus the destination's own collision and staging
/// failures.
pub fn write(
    module: &PioModule<BalancedNetwork>,
    format: TargetFormat,
    destination: powerio_core::Destination,
) -> std::result::Result<powerio_core::WriteResult, powerio_core::Error> {
    write_with_options(module, format, &WriteOptions::default(), destination)
}

/// [`write()`] with write-time cost policies, as [`write_as_with_options`].
///
/// # Errors
/// As [`write()`].
///
/// # Panics
/// Never on external input: the fixed artifact name is valid by
/// construction.
pub fn write_with_options(
    module: &PioModule<BalancedNetwork>,
    format: TargetFormat,
    options: &WriteOptions,
    destination: powerio_core::Destination,
) -> std::result::Result<powerio_core::WriteResult, powerio_core::Error> {
    let conv = write_as_with_options(module, format, options)?;
    let artifact = powerio_core::MemoryArtifact::new(
        powerio_core::ArtifactPath::new("case").expect("static name is a valid artifact path"),
        conv.text.into_bytes(),
    );
    destination.__commit_artifacts(false, vec![artifact], conv.diagnostics)
}

/// Write a parsed module as a PyPSA CSV folder through a destination: the
/// directory form of [`write()`]. Either destination names the output root and
/// every returned artifact sits below it; the whole inventory commits
/// atomically.
///
/// # Errors
/// The destination's collision and staging failures.
///
/// # Panics
/// Never on external input: the writer's fixed artifact names are valid by
/// construction.
pub fn write_pypsa_csv(
    module: &PioModule<BalancedNetwork>,
    destination: powerio_core::Destination,
) -> std::result::Result<powerio_core::WriteResult, powerio_core::Error> {
    let (artifacts, diagnostics) = pypsa::pypsa_csv_artifacts(module.value());
    let artifacts = artifacts
        .into_iter()
        .map(|(name, text)| {
            powerio_core::MemoryArtifact::new(
                powerio_core::ArtifactPath::new(name).expect("the writer emits fixed valid names"),
                text.into_bytes(),
            )
        })
        .collect();
    destination.__commit_artifacts(true, artifacts, diagnostics)
}

/// Write a parsed module with write-time cost policies. The plain
/// [`write_as`] behavior is preserved when `options` is default; a non-default
/// policy edits a copy of the typed value, so its write never echoes source
/// bytes the policy no longer matches.
pub fn write_as_with_options(
    module: &PioModule<BalancedNetwork>,
    format: TargetFormat,
    options: &WriteOptions,
) -> std::result::Result<Conversion, powerio_core::Error> {
    if options.is_default() {
        return write_as(module, format);
    }
    let (working, policy_warnings) =
        apply_write_cost_policy(module.value(), options).map_err(core_error)?;
    let mut conv = write_conversion(&working, format).map_err(core_error)?;
    conv.prepend(policy_warnings);
    Ok(conv)
}

/// Apply the write-time cost policy to a copy of `net` and report what it did.
///
/// Shared by the text and directory writers so both surfaces run one policy and
/// describe it with the same findings. The caller's network is never mutated.
pub(crate) fn apply_write_cost_policy(
    net: &BalancedNetwork,
    options: &WriteOptions,
) -> Result<(BalancedNetwork, Vec<Diagnostic>)> {
    let mut working = net.clone();
    let report =
        working.apply_gen_cost_policy(&options.gen_cost_patches, options.missing_gen_cost)?;
    let mut policy_warnings = Diagnostics::new();
    if report.patched > 0 {
        policy_warnings.push(
            &codes::TRANSFORM_GEN_COST_POLICY_APPLIED,
            format!(
                "generator cost patch applied to {} generator(s)",
                report.patched
            ),
        );
    }
    if report.synthesized > 0 {
        policy_warnings.push(
            &codes::TRANSFORM_GEN_COST_POLICY_APPLIED,
            match options.missing_gen_cost {
                MissingGenCostPolicy::Fill {
                    c2,
                    c1,
                    c0,
                    startup,
                    shutdown,
                } => format!(
                    "generator cost synthesized for {} generator(s): model 2, ncost 3, \
                 coeffs [{c2}, {c1}, {c0}], startup {startup}, shutdown {shutdown}",
                    report.synthesized
                ),
                _ => unreachable!("only Fill synthesizes costs"),
            },
        );
    }
    Ok((working, policy_warnings.into_records()))
}

/// Allocate a circuit id for an element keyed by `key` — a bus for loads/shunts,
/// or a `(from, to)` pair for branches: reuse the source-supplied `preferred` id
/// when it is still free on this key, else the lowest free positional id. Keeps
/// parallel devices distinct so the `(key, id)` uniqueness rule the PSS/E and
/// PSLF records require holds even when the source supplies colliding ids.
pub(super) fn allocate_circuit_id<K: Ord + Clone>(
    preferred: Option<&str>,
    key: K,
    used: &mut std::collections::BTreeMap<K, std::collections::BTreeSet<String>>,
) -> String {
    let taken = used.entry(key).or_default();
    if let Some(id) = preferred {
        if taken.insert(id.to_owned()) {
            return id.to_owned();
        }
    }
    let mut n = 1u32;
    loop {
        let candidate = n.to_string();
        if taken.insert(candidate.clone()) {
            return candidate;
        }
        n += 1;
    }
}

/// Warn when a PSS/E source is re-serialized at an older revision than its own.
/// `parse_file` maps every `.raw` to revision 33 and the `psse`/`raw` aliases
/// resolve to 33, so writing a v34/v35 source through the default target skips
/// the echo path (revisions differ) and re-emits the v33 layout, dropping the
/// modern records (12 named ratings, load DG/LOADTYPE columns, the system-wide
/// block) and any unmodeled section the echo would have preserved. Name the
/// downgrade instead of performing it silently.
fn warn_psse_downgrade(
    module: &PioModule<BalancedNetwork>,
    format: TargetFormat,
    conv: &mut Conversion,
) {
    let source_text = module
        .source()
        .and_then(|source| source.primary_buffer().ok())
        .and_then(|buffer| String::from_utf8(buffer.content_bytes().to_vec()).ok());
    if let (TargetFormat::Psse { rev }, SourceFormat::Psse, Some(src)) = (
        format,
        module.value().source_format(),
        source_text.as_deref(),
    ) {
        let src_rev = psse::header_rev(src);
        if src_rev > rev {
            conv.push(
                &codes::EMIT_PSSE_DOWNGRADED,
                format!(
                    "PSS/E source is revision {src_rev} but the write target is revision {rev}; \
                     the older layout drops fields the source carried (write to psse{src_rev} to keep them)"
                ),
            );
        }
    }
}

/// Warn when a non-default system frequency writes to a format with no frequency
/// field. PSS/E (`BASFRQ`) and pandapower (`f_hz`) carry it; MATPOWER,
/// PowerModels, egret, and PowerWorld have nowhere to put it, so a 50 Hz case
/// would silently read back as the 60 Hz default. Report the loss instead.
fn warn_dropped_frequency(net: &BalancedNetwork, format: TargetFormat, conv: &mut Conversion) {
    let carries_frequency = matches!(
        format,
        TargetFormat::Psse { .. } | TargetFormat::PandapowerJson
    );
    if carries_frequency {
        return;
    }
    if (net.base_frequency() - crate::network::DEFAULT_BASE_FREQUENCY).abs() > 1e-9 {
        conv.push(
            &format.emit_family().field_dropped,
            format!(
                "system base frequency {} Hz dropped: {} has no frequency field (reads back as {} Hz)",
                net.base_frequency(),
                format.label(),
                crate::network::DEFAULT_BASE_FREQUENCY
            ),
        );
    }
}

/// Warn when the case carries bus locations and the target has no geometry
/// concept. PowerWorld aux (`Latitude:1`/`Longitude:1`) and pandapower
/// (`geo`) carry them, and the PyPSA folder writer (`x`/`y`) has its own
/// path; MATPOWER, PSS/E, PowerModels, egret, PSLF, and Surge have nowhere to
/// put them, matching the `base_frequency` behavior. `powerio geo extract`
/// writes the sidecar as the escape hatch.
fn warn_dropped_locations(net: &BalancedNetwork, format: TargetFormat, conv: &mut Conversion) {
    let carries_locations = matches!(
        format,
        TargetFormat::PowerWorld | TargetFormat::PandapowerJson
    );
    if carries_locations {
        return;
    }
    let n = net.buses().iter().filter(|b| b.location.is_some()).count();
    let routed = net.branches().iter().filter(|b| b.route.is_some()).count();
    if n > 0 || routed > 0 {
        conv.push(
            &format.emit_family().field_dropped,
            format!(
                "{n} bus location(s) and {routed} branch route(s) dropped: {} has no \
                 coordinate field (write a .geo.json sidecar to keep them)",
                format.label()
            ),
        );
    }
}

/// Warn when a transformer carries line charging and the target's
/// transformer record has no susceptance column to hold it. The PSLF `.epc`
/// transformer record is the one such target; PSS/E writes representable
/// magnetizing admittance and the MATPOWER shaped writers keep the legacy total
/// projection on the branch row, so neither drops it.
fn warn_dropped_transformer_charging(
    net: &BalancedNetwork,
    format: TargetFormat,
    conv: &mut Conversion,
) {
    if !matches!(format, TargetFormat::Pslf) {
        return;
    }
    let n = net
        .branches()
        .iter()
        .filter(|b| b.is_transformer() && b.total_charging_b() != 0.0)
        .count();
    if n > 0 {
        conv.push(
            &codes::EMIT_PSLF.field_dropped,
            format!(
                "{n} transformer(s) carry line charging that the PSLF .epc transformer \
                 record cannot represent; the charging was dropped"
            ),
        );
    }
}

pub(super) fn branch_rating_set_drop_warning(
    target: &str,
    branch_index: usize,
    branch: &Branch,
    rating: &BranchRatingSet,
) -> String {
    format!(
        "branch {} ({} to {}) rating set {}={} MVA dropped: {} has no field for branch rating sets beyond rate_a, rate_b, and rate_c",
        branch_index + 1,
        branch.from,
        branch.to,
        rating.name,
        rating.rate_mva,
        target
    )
}

/// Warn once when elements carry passthrough extras `target`'s writer does not
/// replay. `consumed` is the writer's own rule: the keys it reads back into a
/// record. Everything else was retained by a reader because the source stated
/// more than a rewrite would synthesize, so dropping it without saying so is
/// an undeclared loss (#330). One line, a count and the reason, matching the
/// granularity of the other writer warnings.
pub(super) fn warn_dropped_extras(
    family: &'static EmitFamily,
    target: &str,
    net: &BalancedNetwork,
    consumed: impl Fn(&str) -> bool,
    warnings: &mut Diagnostics,
) {
    let carries = |extras: &crate::network::Extras| extras.keys().any(|k| !consumed(k));
    let dropped = net.buses().iter().filter(|e| carries(&e.extras)).count()
        + net.branches().iter().filter(|e| carries(&e.extras)).count()
        + net.loads().iter().filter(|e| carries(&e.extras)).count()
        + net.shunts().iter().filter(|e| carries(&e.extras)).count()
        + net.switches().iter().filter(|e| carries(&e.extras)).count()
        + net.storage().iter().filter(|e| carries(&e.extras)).count()
        + net.hvdc().iter().filter(|e| carries(&e.extras)).count()
        + net
            .transformers_3w()
            .iter()
            .filter(|e| carries(&e.extras))
            .count();
    if dropped > 0 {
        warnings.push(
            &family.extras_dropped,
            format!(
                "{dropped} element(s) carry source-format passthrough fields (extras) the {target} \
                 writer does not replay; dropped"
            ),
        );
    }
}

/// Warn when a writer drops the area table. Its own line rather than the
/// extras count: `areas` is a typed field, not a passthrough (#330).
pub(super) fn warn_dropped_areas(
    family: &'static EmitFamily,
    target: &str,
    net: &BalancedNetwork,
    warnings: &mut Diagnostics,
) {
    if !net.areas().is_empty() {
        warnings.push(
            &family.areas_dropped,
            format!(
                "{} area record(s) dropped: the {target} writer emits no area table",
                net.areas().len()
            ),
        );
    }
}

pub(super) fn warn_extra_branch_rating_sets(
    family: &'static EmitFamily,
    target: &str,
    net: &BalancedNetwork,
    warnings: &mut Diagnostics,
) {
    for (branch_index, branch) in net.branches().iter().enumerate() {
        for rating in &branch.rating_sets {
            warnings.push(
                &family.rating_set_dropped,
                branch_rating_set_drop_warning(target, branch_index, branch, rating),
            );
        }
    }
}

/// The declared format ID for a caller-supplied token. Tokens are matched
/// case insensitively and accept the historical underscore spelling of a
/// hyphenated alias; the ID itself keeps the stable lower case hyphen
/// grammar.
pub fn format_id_for(
    token: &str,
) -> std::result::Result<powerio_core::FormatId, powerio_core::Error> {
    powerio_core::FormatId::new(token.to_ascii_lowercase().replace('_', "-"))
}

/// Attach a caller-named source format to a source.
fn with_declared_format(
    source: powerio_core::Source,
    from: Option<&str>,
) -> std::result::Result<powerio_core::Source, powerio_core::Error> {
    match from {
        None => Ok(source),
        Some(token) => Ok(source.with_format(format_id_for(token)?)),
    }
}

/// Convert a case file to `to`, optionally forcing the source format with
/// `from`.
///
/// This is the canonical file-conversion helper shared by the bindings. It
/// parses `path` once, writes the parsed module to `to`, and returns the
/// converted text plus any fidelity findings, read side first. An echo
/// (writing back to the source format) returns the retained text with no
/// findings.
///
/// # Errors
/// As [`parse`].
pub fn convert_file(
    path: impl AsRef<std::path::Path>,
    to: TargetFormat,
    from: Option<&str>,
) -> std::result::Result<Conversion, powerio_core::Error> {
    let source = with_declared_format(powerio_core::Source::open(path.as_ref())?, from)?;
    convert_source(source, to, &WriteOptions::default())
}

/// Convert a case file with write-time cost policies.
pub fn convert_file_with_options(
    path: impl AsRef<std::path::Path>,
    to: TargetFormat,
    from: Option<&str>,
    options: &WriteOptions,
) -> std::result::Result<Conversion, powerio_core::Error> {
    let source = with_declared_format(powerio_core::Source::open(path.as_ref())?, from)?;
    convert_source(source, to, options)
}

/// Convert in-memory case `text` of the named source format `from` (see
/// [`target_format_from_name`]) to `to`.
///
/// Parses `text` once and writes the parsed module to `to` without a
/// temporary file. Findings are ordered read side first, as in
/// [`convert_file`].
///
/// # Errors
/// As [`parse`].
pub fn convert_str(
    text: &str,
    to: TargetFormat,
    from: &str,
) -> std::result::Result<Conversion, powerio_core::Error> {
    convert_str_with_options(text, to, from, &WriteOptions::default())
}

/// Convert in-memory case text with write-time cost policies.
pub fn convert_str_with_options(
    text: &str,
    to: TargetFormat,
    from: &str,
    options: &WriteOptions,
) -> std::result::Result<Conversion, powerio_core::Error> {
    let source = with_declared_format(
        powerio_core::Source::from_bytes("<memory>", text.as_bytes().to_vec())?,
        Some(from),
    )?;
    convert_source(source, to, options)
}

fn convert_source(
    source: powerio_core::Source,
    to: TargetFormat,
    options: &WriteOptions,
) -> std::result::Result<Conversion, powerio_core::Error> {
    let module = parse(source)?;
    let echoed = options.is_default() && echo_text(&module, to).is_some();
    let mut conv = write_as_with_options(&module, to, options)?;
    if !echoed {
        conv.prepend(module.diagnostics().to_vec());
    }
    Ok(conv)
}

/// Write `net` into `out_dir` as the named directory format. This function
/// dispatches directory format names for the bindings. PyPSA CSV
/// (`pypsa-csv`/`pypsa`) is the one such
/// format today; a text format name is rejected by name, pointing at
/// [`write_as`]. Returns the write's findings as structured records; render
/// them with `diagnostics::render_diagnostics` for a text channel.
///
/// # Errors
/// [`Error::UnknownFormat`] for a non-directory format name; the writer's own
/// [`Error`] otherwise.
pub fn write_dir(
    net: &BalancedNetwork,
    to: &str,
    out_dir: impl AsRef<std::path::Path>,
) -> std::result::Result<Vec<Diagnostic>, powerio_core::Error> {
    if is_pypsa_csv_name(to) {
        return write_pypsa_csv_folder(net, out_dir.as_ref()).map(|o| o.diagnostics);
    }
    Err(core_error(unknown_directory_format(to)))
}

fn unknown_directory_format(to: &str) -> Error {
    Error::UnknownFormat(format!(
        "{to} is not a directory format (directory targets: pypsa-csv/pypsa); \
         text formats serialize through write_as / to_format"
    ))
}

/// Write `net` into `out_dir` with write-time cost policies: the directory twin
/// of [`write_as_with_options`]. Default options are [`write_dir`] exactly.
/// The policy's own findings come back ahead of the writer's.
///
/// # Errors
/// As [`write_dir`], plus the cost policy's own [`Error`].
pub fn write_dir_with_options(
    net: &BalancedNetwork,
    to: &str,
    out_dir: impl AsRef<std::path::Path>,
    options: &WriteOptions,
) -> std::result::Result<Vec<Diagnostic>, powerio_core::Error> {
    // Refuse an unknown target before the policy runs, so a bad format name is
    // reported as one rather than as whatever the cost pass hits first.
    if !is_pypsa_csv_name(to) {
        return Err(core_error(unknown_directory_format(to)));
    }
    if options.is_default() {
        return write_dir(net, to, out_dir);
    }
    let (working, mut diagnostics) = apply_write_cost_policy(net, options).map_err(core_error)?;
    diagnostics.extend(write_dir(&working, to, out_dir)?);
    Ok(diagnostics)
}

/// Warn when a network with no reference (slack) bus converts to a format
/// whose solvers require one. PowerWorld `.pwb` is the one source that
/// systematically lacks the designation (the binary does not store it), so
/// the silent case would be common; `to_normalized` synthesizes a slack at
/// the largest pmax in service generator bus for consumers that need one.
fn warn_missing_reference(net: &BalancedNetwork, format: TargetFormat, conv: &mut Conversion) {
    let needs_ref = matches!(
        format,
        TargetFormat::Matpower
            | TargetFormat::Psse { .. }
            | TargetFormat::PowerModelsJson
            | TargetFormat::PandapowerJson
            | TargetFormat::Pslf
            | TargetFormat::SurgeJson
    );
    if needs_ref {
        if let Some(message) = missing_reference_warning(net) {
            conv.push(&format.emit_family().reference_missing, message);
        }
    }
}

/// The slackless-network warning itself, shared with the PyPSA folder writer
/// (which produces `PypsaCsvOutputs`, not a [`Conversion`], so it cannot go
/// through [`warn_missing_reference`]).
pub(super) fn missing_reference_warning(net: &BalancedNetwork) -> Option<String> {
    (!net.buses().iter().any(|b| b.kind == BusType::Ref)).then(|| {
        "no reference (slack) bus in the source network; power flow tools \
         reject such cases; to_normalized synthesizes a slack at the \
         largest pmax in service generator bus"
            .to_string()
    })
}

/// A normalized network has its tap canonicalized to `1.0` on every line (the
/// `0 → 1` rule), but [`Branch::is_transformer`](crate::network::Branch::is_transformer),
/// the test these writers use to split lines from transformers, keys off
/// `tap != 0`. So a normalized line is written into the transformer section/type.
/// The power flow is identical (a unity-ratio, zero-shift transformer equals a
/// line), but the label is not, so report the fidelity loss rather than relabel
/// it silently. MATPOWER has no separate transformer representation (just a `TAP`
/// column), so it is exempt.
// `tap == 1.0` / `shift == 0.0` are exact by construction: normalization sets a
// line's tap from `effective_tap()` (the literal `1.0`) and its shift from
// `0.0 * DEG_TO_RAD` (exactly `0.0`), so an epsilon compare would be wrong here.
#[allow(clippy::float_cmp)]
fn warn_normalized_tap(net: &BalancedNetwork, format: TargetFormat, conv: &mut Conversion) {
    if matches!(format, TargetFormat::Matpower) {
        return;
    }
    if let Some(message) = normalized_tap_warning(net) {
        conv.push(&format.emit_family().element_relabeled, message);
    }
}

/// The normalized-label warning itself, shared with the PyPSA folder writer.
// `tap == 1.0` / `shift == 0.0` are exact by construction (see
// `warn_normalized_tap`), so an epsilon compare would be wrong here.
#[allow(clippy::float_cmp)]
pub(super) fn normalized_tap_warning(net: &BalancedNetwork) -> Option<String> {
    if !net.is_normalized() {
        return None;
    }
    // After normalization a line (raw tap 0) and a unity-ratio transformer (raw
    // tap 1) both read as tap 1.0 / shift 0.0, so they cannot be told apart. Count
    // them together as the branches whose line/transformer label is now ambiguous.
    let ambiguous = net
        .branches()
        .iter()
        .filter(|b| b.tap == 1.0 && b.shift == 0.0)
        .count();
    (ambiguous > 0).then(|| {
        format!(
            "normalized network: {ambiguous} branch(es) have unit tap and no phase \
             shift, so the line/transformer label is not preserved (the power flow \
             is identical)"
        )
    })
}

/// True when `value` is set and deviates from `reference`: the shared test for
/// "does this rating column carry information the target cannot" used by the
/// rate_b/rate_c drop warnings.
fn nonzero_differs(value: f64, reference: f64) -> bool {
    value.abs() > f64::EPSILON && (value - reference).abs() > f64::EPSILON
}

/// Set a bus's kind through the `bus_pos` index, leaving Isolated buses alone.
/// Shared by the readers that derive bus kinds from generator/slack tables.
pub(crate) fn set_bus_kind(
    buses: &mut [Bus],
    bus_pos: &HashMap<BusId, usize>,
    bus: BusId,
    kind: BusType,
) {
    if let Some(&idx) = bus_pos.get(&bus) {
        if buses[idx].kind != BusType::Isolated {
            buses[idx].kind = kind;
        }
    }
}

/// `base_kv` of a bus through the `bus_pos` index; 0.0 for an unknown bus.
pub(crate) fn bus_kv(buses: &[Bus], bus_pos: &HashMap<BusId, usize>, bus: BusId) -> f64 {
    bus_pos
        .get(&bus)
        .and_then(|&i| buses.get(i))
        .map_or(0.0, |b| b.base_kv)
}

/// Replace characters that would corrupt a quoted or delimited field with
/// `replacement`, so a free-form name can't shift or truncate the record it sits
/// in. `forbidden` lists the destination's quote, delimiter, and comment chars.
/// Returns the value borrowed unchanged when it holds none of them, so the common
/// clean-name path allocates nothing.
///
/// Each text writer calls this at its quoting seam and warns when the result
/// differs from the input (the substitution silently alters operator-facing
/// names): the PSS/E single-quoted bus name and the PowerWorld double-quoted bus
/// name both interpolate a `BalancedNetwork` name straight into a quoted field, where an
/// embedded quote (or, for PSS/E, the `/` inline-comment delimiter) would shift
/// every later column of the record.
/// A line terminator is always replaced, whatever `forbidden` holds: no text
/// record format can carry one inside a field, so an embedded `\n` does not
/// shift a column, it ends the record and makes everything after it parse as
/// a new one. A crafted name could otherwise forge whole records in the
/// written file.
pub(crate) fn sanitize_quoted<'a>(
    value: &'a str,
    forbidden: &[char],
    replacement: char,
) -> std::borrow::Cow<'a, str> {
    let breaks = |c: char| c == '\n' || c == '\r' || forbidden.contains(&c);
    if value.contains(breaks) {
        value
            .chars()
            .map(|c| if breaks(c) { replacement } else { c })
            .collect::<String>()
            .into()
    } else {
        std::borrow::Cow::Borrowed(value)
    }
}

/// Impedance base `v_kv² / base_mva`; 1.0 when either base is missing, so a
/// per-unit ↔ ohm conversion on it is the identity.
pub(crate) fn zbase(v_kv: f64, base_mva: f64) -> f64 {
    if v_kv > 0.0 && base_mva > 0.0 {
        v_kv * v_kv / base_mva
    } else {
        1.0
    }
}

/// Whether a write target is the same format the network was read from.
fn same_format(target: TargetFormat, source: SourceFormat) -> bool {
    matches!(
        (target, source),
        (TargetFormat::Matpower, SourceFormat::Matpower)
            | (TargetFormat::PowerModelsJson, SourceFormat::PowerModelsJson)
            | (TargetFormat::EgretJson, SourceFormat::EgretJson)
            | (TargetFormat::Psse { .. }, SourceFormat::Psse)
            | (TargetFormat::PowerWorld, SourceFormat::PowerWorld)
            | (TargetFormat::PandapowerJson, SourceFormat::PandapowerJson)
            | (TargetFormat::Pslf, SourceFormat::Pslf)
            | (TargetFormat::Goc3Json, SourceFormat::Goc3Json)
            | (TargetFormat::SurgeJson, SourceFormat::SurgeJson)
            | (
                TargetFormat::DeepMindOpfDataJson,
                SourceFormat::DeepMindOpfDataJson,
            )
    )
}

/// JSON number for a finite `f64`; `Value::Null` for `NaN`/`±Inf`.
pub(crate) fn jnum(x: f64) -> Value {
    serde_json::Number::from_f64(x).map_or(Value::Null, Value::Number)
}

/// Serialize a built JSON tree into a [`Conversion`], appending one warning that
/// names every field where a non-finite `f64` was written as `null` (JSON has no
/// `±Inf`/`NaN`). Shared by the JSON writers.
pub(crate) fn finish(
    family: &'static EmitFamily,
    root: Map<String, Value>,
    mut warnings: Diagnostics,
) -> Conversion {
    let value = Value::Object(root);
    let mut nulls = BTreeSet::new();
    collect_null_keys(&value, &mut nulls);
    if !nulls.is_empty() {
        warnings.push(
            &family.not_a_number,
            format!(
                "non-finite numeric values written as JSON null in field(s): {}",
                nulls.into_iter().collect::<Vec<_>>().join(", ")
            ),
        );
    }
    let text = serde_json::to_string_pretty(&value).expect("a serde_json::Value always serializes");
    Conversion::new(text, warnings)
}

/// Collect the names of object keys whose value is `null`, anywhere in the tree.
fn collect_null_keys(value: &Value, out: &mut BTreeSet<String>) {
    match value {
        Value::Object(map) => {
            for (key, val) in map {
                if val.is_null() {
                    out.insert(key.clone());
                } else {
                    collect_null_keys(val, out);
                }
            }
        }
        Value::Array(items) => items.iter().for_each(|v| collect_null_keys(v, out)),
        _ => {}
    }
}

/// Test-only compatibility parse shapes; production code goes through
/// [`parse`] and the module type.
#[cfg(test)]
pub(crate) mod test_parse {
    use super::*;

    #[derive(Debug)]
    pub(crate) struct TestParsed {
        pub network: BalancedNetwork,
        pub diagnostics: Vec<Diagnostic>,
    }

    impl TestParsed {
        pub(crate) fn rendered_diagnostics(&self) -> Vec<String> {
            crate::diagnostics::render_diagnostics(&self.diagnostics)
        }
    }

    fn declared(
        source: powerio_core::Source,
        from: Option<&str>,
    ) -> std::result::Result<powerio_core::Source, powerio_core::Error> {
        match from {
            None => Ok(source),
            Some(token) => Ok(source.with_format(powerio_core::FormatId::new(
                token.to_ascii_lowercase().replace('_', "-"),
            )?)),
        }
    }

    pub(crate) fn parse_file(
        path: impl AsRef<std::path::Path>,
        from: Option<&str>,
    ) -> std::result::Result<TestParsed, powerio_core::Error> {
        let source = declared(powerio_core::Source::open(path.as_ref())?, from)?;
        parse(source).map(|module| TestParsed {
            diagnostics: module.diagnostics().to_vec(),
            network: module.into_value(),
        })
    }

    pub(crate) fn parse_str(
        text: &str,
        from: &str,
    ) -> std::result::Result<TestParsed, powerio_core::Error> {
        let source = declared(
            powerio_core::Source::from_bytes("<memory>", text.as_bytes().to_vec())?,
            Some(from),
        )?;
        parse(source).map(|module| TestParsed {
            diagnostics: module.diagnostics().to_vec(),
            network: module.into_value(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::test_parse::{parse_file, parse_str};
    use super::*;
    use crate::network::SourceFormat;

    #[test]
    fn sanitize_quoted_always_replaces_line_terminators() {
        // A terminator ends the record, so it is replaced whatever the
        // caller's delimiter set holds: a name carrying one could otherwise
        // forge whole records in a written .raw/.aux/.epc.
        for forbidden in [&[][..], &['\''][..], &['"'][..]] {
            let out = sanitize_quoted("A\n42, 'X'\r\nB", forbidden, ' ');
            assert!(
                !out.contains('\n') && !out.contains('\r'),
                "terminator survived with forbidden={forbidden:?}: {out:?}"
            );
        }
        // A clean value is still borrowed, not copied.
        assert!(matches!(
            sanitize_quoted("clean name", &['\''], ' '),
            std::borrow::Cow::Borrowed(_)
        ));
    }

    #[test]
    fn dss_extension_error_names_the_distribution_surface() {
        let path = std::env::temp_dir().join(format!(
            "powerio-dss-surface-{}-feeder.dss",
            std::process::id()
        ));
        std::fs::write(&path, "New Circuit.feeder\n").unwrap();
        let err = parse_file(&path, None).unwrap_err();
        let _ = std::fs::remove_file(&path);
        assert!(err.to_string().contains("distribution"), "got: {err}");
    }

    #[test]
    fn io_error_names_the_path() {
        let path =
            std::env::temp_dir().join(format!("powerio-no-such-case-{}.m", std::process::id()));
        let err = parse_file(&path, None).unwrap_err();
        assert_eq!(err.category(), powerio_core::ErrorCategory::Io);
        let msg = err.to_string();
        assert!(
            msg.contains(&path.display().to_string()),
            "the io failure must name the path: {msg}"
        );
    }

    #[test]
    fn a_directory_is_refused_as_a_directory() {
        // A versioned dataset directory: extension inference would read ".07"
        // off the name and misdiagnose the mistake as a format problem.
        let dir = std::env::temp_dir().join(format!("pglib-opf-23.07-{}", std::process::id()));
        std::fs::create_dir_all(&dir).unwrap();
        let err = parse_file(&dir, None).unwrap_err();
        std::fs::remove_dir_all(&dir).unwrap();
        let msg = err.to_string();
        assert!(msg.contains("is a directory"), "got: {msg}");
        assert!(msg.contains(&dir.display().to_string()), "got: {msg}");
        assert!(msg.contains("PyPSA CSV folder"), "got: {msg}");
    }

    #[test]
    fn unknown_format_error_lists_the_accepted_names() {
        let err = parse_str("anything", "not-a-format").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("not-a-format"), "got: {msg}");
        assert!(msg.contains("accepted names:"), "got: {msg}");
        assert!(msg.contains(SOURCE_FORMAT_NAMES), "got: {msg}");
    }

    #[test]
    fn the_accepted_name_list_matches_the_matcher() {
        use routing::TransmissionFormat as TF;
        // Every alias in the printed list resolves.
        let mut canonical = Vec::new();
        for clause in SOURCE_FORMAT_NAMES.split(", ") {
            for (i, alias) in clause.split('/').enumerate() {
                let resolved = routing::transmission_format_from_name(alias);
                assert!(
                    resolved.is_some(),
                    "listed alias `{alias}` does not resolve"
                );
                if i == 0 {
                    canonical.push(resolved.unwrap());
                }
            }
        }
        // Every parseable format is listed. Gridfm is the one matcher entry
        // with no parse_file arm (datasets go through the read_dir surface).
        for format in [
            TF::Matpower,
            TF::PowerModelsJson,
            TF::EgretJson,
            TF::Psse,
            TF::Psse34,
            TF::Psse35,
            TF::PowerWorld,
            TF::PandapowerJson,
            TF::PypsaCsv,
            TF::Pslf,
            TF::Pwb,
            TF::Goc3Json,
            TF::SurgeJson,
            TF::DeepMindOpfDataJson,
        ] {
            assert!(
                canonical.contains(&format),
                "{} is missing from SOURCE_FORMAT_NAMES",
                format.name()
            );
        }
    }

    #[test]
    fn a_case_with_generators_and_no_cost_data_warns() {
        let costless = "\
function mpc = nocost
mpc.version = '2';
mpc.baseMVA = 100;
mpc.bus = [
\t1\t3\t0\t0\t0\t0\t1\t1\t0\t230\t1\t1.1\t0.9;
\t2\t1\t50\t10\t0\t0\t1\t1\t0\t230\t1\t1.1\t0.9;
];
mpc.gen = [
\t1\t60\t0\t100\t-100\t1\t100\t1\t100\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0\t0;
];
mpc.branch = [
\t1\t2\t0.01\t0.1\t0\t0\t0\t0\t0\t0\t1\t-360\t360;
];
";
        // The parse itself stays silent: whether a case carries costs is the
        // case's business, and a conversion leg must not count it. The
        // solver-ready copy is where a zero objective becomes real.
        let parsed = parse_str(costless, "matpower").unwrap();
        assert!(
            parsed.rendered_diagnostics().is_empty(),
            "{:?}",
            parsed.rendered_diagnostics()
        );
        let normalized = parsed
            .network
            .to_normalized_with_options(&crate::NormalizeOptions::default())
            .unwrap();
        let absent: Vec<_> = normalized
            .diagnostics
            .iter()
            .filter(|d| d.code() == "CANONICALIZE.NORMALIZE.GEN_COST_ABSENT")
            .collect();
        assert_eq!(absent.len(), 1, "{:?}", normalized.warnings);
        assert!(absent[0].message().contains("no cost data"), "{absent:?}");
        assert!(absent[0].message().contains("1 in-service"), "{absent:?}");

        // The same case with a gencost table is silent.
        let costed = format!("{costless}mpc.gencost = [\n\t2\t0\t0\t3\t0.01\t40\t0;\n];\n");
        let parsed = parse_str(&costed, "matpower").unwrap();
        let normalized = parsed
            .network
            .to_normalized_with_options(&crate::NormalizeOptions::default())
            .unwrap();
        assert!(
            normalized
                .diagnostics
                .iter()
                .all(|d| d.code() != "CANONICALIZE.NORMALIZE.GEN_COST_ABSENT"),
            "{:?}",
            normalized.warnings
        );
    }

    #[test]
    fn distribution_from_token_error_names_the_distribution_surface() {
        for token in ["dss", "pmd", "bmopf"] {
            let err = parse_str("anything", token).unwrap_err();
            assert!(
                err.to_string().contains("one module family"),
                "{token}: {err}"
            );
        }
        // A genuinely unknown token still echoes plainly.
        let err = parse_str("anything", "nonesuch").unwrap_err();
        assert!(err.to_string().contains("nonesuch"));
    }

    #[test]
    fn byte_order_mark_is_retained_and_echoed() {
        // Windows tooling saves case files with a UTF-8 byte order mark. The
        // parser decodes a mark free slice of the one retained buffer, and an
        // unchanged same format write reproduces the original bytes, mark
        // included.
        let case = "\u{feff}function mpc = t\n\
                    mpc.version = '2';\n\
                    mpc.baseMVA = 100;\n\
                    mpc.bus = [1 3 0 0 0 0 1 1.0 0 345 1 1.1 0.9;];\n\
                    mpc.gen = [];\n\
                    mpc.branch = [];\n";
        let source = powerio_core::Source::from_bytes("case.m", case.as_bytes().to_vec()).unwrap();
        let module = parse(source.with_format(format_id_for("matpower").unwrap())).unwrap();
        assert_eq!(module.value().buses().len(), 1);
        assert!(
            module.diagnostics().is_empty(),
            "{:?}",
            module.diagnostics()
        );
        let echo = write_as(&module, TargetFormat::Matpower).unwrap();
        assert_eq!(echo.text, case, "the echo reproduces the mark exactly");
    }

    #[test]
    fn canonical_format_bypasses_same_format_matpower_echo() {
        let case = "function mpc = t\n\
                    % a comment the canonical writer does not keep\n\
                    mpc.version = '2';\n\
                    mpc.baseMVA = 100;\n\
                    mpc.bus = [1 3 0 0 0 0 1 1.0 0 345 1 1.1 0.9;];\n\
                    mpc.gen = [];\n\
                    mpc.branch = [];\n";
        let source = powerio_core::Source::from_bytes("case.m", case.as_bytes().to_vec()).unwrap();
        let module =
            parse(source.with_format(powerio_core::FormatId::new("matpower").unwrap())).unwrap();
        assert_eq!(
            write_as(&module, TargetFormat::Matpower).unwrap().text,
            case
        );

        let net = module.into_value();
        let canonical = net.to_canonical_format(TargetFormat::Matpower).unwrap();
        assert_ne!(canonical.text, case);
        let reparsed = parse_str(&canonical.text, "matpower").unwrap();
        assert_eq!(reparsed.network.buses().len(), 1);
    }

    #[test]
    fn package_json_error_names_the_package_reader() {
        let err = sniff_json(r#"{"model_kind":"balanced","model":{}}"#).unwrap_err();
        assert!(err.to_string().contains(".pio.json"), "got: {err}");
    }

    #[test]
    fn source_format_strings_round_trip_to_a_target() {
        // The bindings expose `source_format` as its `name()` token, and
        // `to_format` routes that string back through `target_format_from_name`.
        // Every writable source format must resolve; the legacy `{:?}` spelling
        // (the pre-0.9 property value, issue #75) must keep resolving for
        // callers that stored it.
        for (sf, want) in [
            (SourceFormat::Matpower, TargetFormat::Matpower),
            (SourceFormat::PowerModelsJson, TargetFormat::PowerModelsJson),
            (SourceFormat::EgretJson, TargetFormat::EgretJson),
            (SourceFormat::Psse, TargetFormat::Psse { rev: 33 }),
            (SourceFormat::PowerWorld, TargetFormat::PowerWorld),
            (SourceFormat::PandapowerJson, TargetFormat::PandapowerJson),
            (SourceFormat::Pslf, TargetFormat::Pslf),
            (SourceFormat::Goc3Json, TargetFormat::Goc3Json),
            (SourceFormat::SurgeJson, TargetFormat::SurgeJson),
            (
                SourceFormat::DeepMindOpfDataJson,
                TargetFormat::DeepMindOpfDataJson,
            ),
        ] {
            let token = sf.name();
            assert_eq!(
                target_format_from_name(token),
                Some(want),
                "source_format {token:?} did not round-trip"
            );
            let legacy = format!("{sf:?}");
            assert_eq!(
                target_format_from_name(&legacy),
                Some(want),
                "legacy spelling {legacy:?} did not round-trip"
            );
        }
        // The derived/in-memory source formats have no writer target, and
        // neither does the read only .pwb binary.
        for sf in [
            SourceFormat::InMemory,
            SourceFormat::Normalized,
            SourceFormat::Gridfm,
            SourceFormat::PypsaCsv,
            SourceFormat::PowerWorldBinary,
        ] {
            assert_eq!(target_format_from_name(&format!("{sf:?}")), None);
        }
    }
}