powerio-tx 0.11.7

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
//! Map a parsed [`AuxFile`] to the typed [`BalancedNetwork`], and write a `BalancedNetwork`
//! back out as aux text.
//!
//! Only the power flow core object types (Bus, Load, Shunt, Gen, Branch) feed
//! the typed model; every other `DATA` section stays reachable through the
//! generic layer (see [`super::aux`]) and survives the same format round trip
//! via the retained source.

use std::collections::{BTreeMap, HashMap};
use std::fmt::Write as _;

use super::auxiliary::{AuxFile, AuxObject, parse_aux};
use crate::diagnostics::codes::EMIT_POWERWORLD as F;
use crate::diagnostics::{Diagnostics, codes};
use crate::format::{TextEmission, sanitize_quoted, warn_extra_branch_rating_sets};
use crate::network::{
    BalancedNetwork, BalancedNetworkTables, Branch, Bus, BusId, BusType, Extras, GenCost,
    Generator, GeneratorEnergySource, Load, LoadVoltageModel, Shunt, SourceFormat,
};
use crate::{Error, Result};

const FMT: &str = "PowerWorld .aux";

/// The double quote would close a PowerWorld quoted value early on re-read (the
/// tokenizer toggles on `"` with no un-escaping), shifting every later column.
const NAME_FORBIDDEN: &[char] = &['"'];

/// Branch identity extras keys, shared with the `.pwb` reader. They double as
/// the aux field names (extras keep PowerWorld fields verbatim), so every
/// PowerWorld reader produces the same extras.
pub(super) const LINE_CIRCUIT: &str = "LineCircuit";
pub(super) const BRANCH_DEVICE_TYPE: &str = "BranchDeviceType";
/// The `Gen` fields a vendored PowerWorld export states a generator's fuel
/// input-output curve and fuel price in.
const GEN_COST_MODEL: &str = "GenCostModel";
const GEN_FUEL_COST: &str = "GenFuelCost";
const GEN_FIXED_COST: &str = "GenFixedCost";
const GEN_IO_B: &str = "GenIOB";
const GEN_IO_C: &str = "GenIOC";
const GEN_IO_D: &str = "GenIOD";
/// Coefficients a cubic input-output curve holds: the constant plus three
/// powers of MW.
const GEN_IO_COEFFICIENTS: usize = 4;

// ---- Reader -----------------------------------------------------------------

/// Owned-source entry used by the format hub: parse by borrowing `source`, then
/// move the buffer into the retained source (no copy). `name_hint` (e.g. a file
/// stem) names the network when the `.aux` carries no export marker.
#[expect(clippy::too_many_lines)]
pub(in crate::format) fn parse_powerworld_source(
    source: &str,
    name_hint: Option<&str>,
    warnings: &mut Diagnostics,
) -> Result<BalancedNetwork> {
    let content: &str = source;
    // PowerWorld `.aux` does not carry the system base in the case data, so we
    // default to 100 MVA (the de-facto standard, and what our own writer records
    // in the `// baseMVA` marker below). Reading a real base from PowerWorld's
    // project files is tracked separately; defaulting here is deliberate, not a
    // silent guess — erroring would reject every base-less third-party `.aux`.
    let mut base_mva = 100.0;
    let mut name = name_hint.unwrap_or("case").to_string();
    for line in content.lines() {
        let t = line.trim();
        if let Some(rest) = t.strip_prefix("// baseMVA ") {
            if let Ok(v) = rest.trim().parse::<f64>() {
                base_mva = v;
            }
        } else if let Some((_, n)) = t.split_once("powerio export: ") {
            name = n.trim().to_string();
        }
    }

    let aux = parse_aux(content)?;
    if aux.data().next().is_none() {
        return Err(Error::FormatRead {
            format: FMT,
            message: "no DATA blocks found".into(),
        });
    }

    // A complete case export spreads one object type over several DATA
    // sections, each declaring a different field group for the same objects
    // (Simulator 19 era exports write Bus twice, Gen three times, and put the
    // transformer regulation fields in a separate `Transformer` object).
    // Merge sections by the type's key fields before mapping; a later section
    // updates the fields it declares, exactly like loading the aux into
    // Simulator would.
    let mut merged_buses = Merge::new(&[&["BusNum", "Number"]]);
    let mut merged_loads = Merge::new(&[&["BusNum", "BusName_NomVolt"], &["LoadID", "ID"]]);
    let mut merged_shunts = Merge::new(&[&["BusNum", "BusName_NomVolt"], &["ShuntID", "ID"]]);
    let mut merged_gens = Merge::new(&[&["BusNum", "BusName_NomVolt"], &["GenID", "ID"]]);
    let mut merged_branches = Merge::new(&[
        &["BusNum", "BusNumFrom", "BusName_NomVolt"],
        &["BusNum:1", "BusNumTo", "BusName_NomVolt:1"],
        &[LINE_CIRCUIT, "Circuit"],
    ]);
    let mut unmodeled: BTreeMap<&str, usize> = BTreeMap::new();
    for blk in aux.data() {
        // Simulator resolves object names case insensitively, and real
        // exports use that: the UIUC 150 bus case spells every section
        // `BUS`/`GEN`/`LOAD`. An exact match read such a file as a case with
        // no buses.
        match blk.object_type.to_ascii_lowercase().as_str() {
            "bus" => merged_buses.absorb(
                blk,
                blk.field_index("BusNum").is_some() || blk.field_index("Number").is_some(),
            ),
            "load" => merged_loads.absorb(blk, true),
            "shunt" => merged_shunts.absorb(blk, true),
            "gen" => merged_gens.absorb(blk, true),
            "branch" => merged_branches.absorb(blk, true),
            // Transformer sections augment existing branches with regulation
            // fields; a transformer with no Branch record carries no impedance
            // and cannot stand alone, so unmatched rows are not created.
            "transformer" => merged_branches.absorb(blk, false),
            _ => {
                if !blk.rows.is_empty() {
                    *unmodeled.entry(&blk.object_type).or_default() += blk.rows.len();
                }
            }
        }
    }
    for (object, rows) in unmodeled {
        warnings.push(
            &codes::READ_POWERWORLD_RETAINED_SOURCE_ONLY,
            format!(
                "PowerWorld .aux DATA {object} has {rows} row(s) not modeled in BalancedNetwork; \
                 retained only in source text for same-format writeback"
            ),
        );
    }

    let mut buses = Vec::new();
    let mut bus_labels = HashMap::new();
    for r in merged_buses.rows() {
        let bus = read_bus(r)?;
        if let Some(label) = first(r, &["BusName_NomVolt"]) {
            bus_labels.insert(label, bus.id);
        }
        buses.push(bus);
    }
    let mut loads = Vec::new();
    for r in merged_loads.rows() {
        loads.push(read_load(r, &bus_labels, loads.len())?);
    }
    let mut shunts = Vec::new();
    for r in merged_shunts.rows() {
        shunts.push(read_shunt(r, &bus_labels, shunts.len())?);
    }
    let mut generators = Vec::new();
    for r in merged_gens.rows() {
        generators.push(read_gen(r, &bus_labels)?);
    }
    let mut branches = Vec::new();
    let mut parallel: HashMap<(BusId, BusId), u32> = HashMap::new();
    for r in merged_branches.rows() {
        branches.push(read_branch(r, &bus_labels, &mut parallel)?);
    }
    derive_bus_kinds(&mut buses, &generators);

    let net = BalancedNetwork::from_tables(BalancedNetworkTables {
        name,
        base_mva,
        base_frequency: crate::network::DEFAULT_BASE_FREQUENCY,
        geo: super::super::geographic_meta(&buses),
        case_metadata: crate::network::CaseMetadata::default(),
        detailed_connectivity: None,
        generated_uids: std::sync::Arc::default(),
        buses: buses.into(),
        loads: loads.into(),
        shunts: shunts.into(),
        static_var_compensators: Vec::new().into(),
        branches: branches.into(),
        switches: Vec::new().into(),
        generators: generators.into(),
        storage: Vec::new().into(),
        hvdc: Vec::new().into(),
        transformers_3w: Vec::new().into(),
        areas: Vec::new().into(),
        solver: None,
        source_format: SourceFormat::PowerWorld,
    });
    net.check_references(FMT)?;
    Ok(net)
}

/// Parse the auxiliary sections of a PowerWorld-sourced [`BalancedNetwork`]'s retained
/// source. The typed model carries the power flow core; everything else in the
/// original file (contingencies, limit sets, substations, ...) is reachable
/// here.
///
/// Returns `None` when the network was not read from a `.aux` source.
///
/// # Errors
/// As [`parse_aux`], on retained source text that no longer parses.
pub fn aux_sections(source_text: &str) -> Result<AuxFile<'_>> {
    parse_aux(source_text)
}

type Row<'a> = HashMap<&'a str, &'a str>;

/// Merges the rows of one object type across its DATA sections, keyed by the
/// type's key fields. Insertion order is kept, so the first section fixes the
/// element order and later sections update fields in place.
#[derive(PartialEq, Eq, Hash)]
enum MergeKey<'a> {
    Fields(Vec<&'a str>),
    /// A section with none of the type's key columns identifies its rows by
    /// position (our own writer's output identifies devices by order).
    Ordinal(usize),
}

struct Merge<'a> {
    /// Key columns as alias groups: each group lists the same key under its
    /// naming generations (`BusNum`/`Number`, `LineCircuit`/`Circuit`, ...);
    /// a section keys on whichever name it declares.
    key_fields: &'static [&'static [&'static str]],
    index: HashMap<MergeKey<'a>, usize>,
    merged: Vec<Row<'a>>,
}

impl<'a> Merge<'a> {
    fn new(key_fields: &'static [&'static [&'static str]]) -> Self {
        Merge {
            key_fields,
            index: HashMap::new(),
            merged: Vec::new(),
        }
    }

    /// Fold a DATA section in. With `create`, rows whose key is unseen become
    /// new elements; otherwise they are dropped (augmentation only sections,
    /// like `Transformer`).
    fn absorb(&mut self, blk: &'a AuxObject, create: bool) {
        let positions: Vec<Vec<usize>> = self
            .key_fields
            .iter()
            .map(|group| group.iter().filter_map(|k| blk.field_index(k)).collect())
            .collect();
        let keyless = positions.iter().all(Vec::is_empty);
        for (at, row) in blk.rows.iter().enumerate() {
            let key = if keyless {
                MergeKey::Ordinal(at)
            } else {
                MergeKey::Fields(
                    positions
                        .iter()
                        .map(|aliases| {
                            aliases
                                .iter()
                                .filter_map(|i| row.values.get(*i).map(|v| v.as_ref().trim()))
                                .find(|v| !v.is_empty())
                                .unwrap_or("")
                        })
                        .collect(),
                )
            };
            let slot = match self.index.get(&key) {
                Some(&i) => i,
                None if create => {
                    self.index.insert(key, self.merged.len());
                    self.merged.push(HashMap::with_capacity(blk.fields.len()));
                    self.merged.len() - 1
                }
                None => continue,
            };
            let entry = &mut self.merged[slot];
            for (f, v) in blk.fields.iter().zip(&row.values) {
                entry.insert(f.as_str(), v.as_ref());
            }
        }
    }

    fn rows(&self) -> impl Iterator<Item = &Row<'a>> {
        self.merged.iter()
    }
}

fn bad_field(key: &str, tok: &str) -> Error {
    Error::FormatRead {
        format: FMT,
        message: format!("field {key} {tok:?} is not a number"),
    }
}

/// Field `key` as f64, defaulting to 0.0 when absent. Present but unparseable is
/// a hard error: a malformed number must not silently become a plausible default
/// and corrupt the matrices downstream.
fn f(r: &Row, key: &str) -> Result<f64> {
    f_or(r, key, 0.0)
}
/// Field `key` as f64, absent → `default`, present-but-unparseable → error.
fn f_or(r: &Row, key: &str, default: f64) -> Result<f64> {
    match r.get(key).copied() {
        None | Some("") => Ok(default),
        Some(s) => s.trim().parse().map_err(|_| bad_field(key, s)),
    }
}
/// Field `key` as a bus id (parsed as f64 then truncated). Absent → 0;
/// present-but-unparseable → error.
fn uid(r: &Row, key: &str) -> Result<usize> {
    match r.get(key).copied() {
        None | Some("") => Ok(0),
        Some(s) => bus_id_from_field(key, s.trim()),
    }
}

/// A bus id string validated the way [`uid`] validates every bus reference:
/// parse through f64 (some exports print ids with a decimal point) but reject
/// anything a float to integer cast would silently bend — NaN and negatives
/// would saturate to 0 and rewire the network, huge values to usize::MAX, and
/// fractions would truncate.
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
fn bus_id_from_field(key: &str, s: &str) -> Result<usize> {
    match s.parse::<f64>() {
        Ok(v) if v.is_finite() && v.fract() == 0.0 && (0.0..=4_294_967_295.0).contains(&v) => {
            Ok(v as usize)
        }
        // A fractional, non-finite, or out-of-range value is a number, so
        // `bad_field`'s "is not a number" would misdescribe it: name the bus
        // id constraint instead.
        Ok(_) => Err(Error::FormatRead {
            format: FMT,
            message: format!(
                "field {key} {s:?} is not a valid bus id \
                 (a whole number in 0..=4294967295)"
            ),
        }),
        Err(_) => Err(bad_field(key, s)),
    }
}
fn on(r: &Row, key: &str) -> Result<bool> {
    // A closed vocabulary: an unrecognized status token must not silently
    // mean energized (the same rule applies to numbers in f_or). Absent
    // or empty keeps the documented in service default.
    match r.get(key).copied().map(str::trim) {
        None | Some("") => Ok(true),
        Some(tok) if tok.eq_ignore_ascii_case("Closed") || tok == "1" => Ok(true),
        Some(tok) if tok.eq_ignore_ascii_case("Open") || tok == "0" => Ok(false),
        Some(tok) => Err(bad_field(key, tok)),
    }
}
/// [`on`] over the first present field among `keys` (naming generations).
fn on_alias(r: &Row, keys: &[&str]) -> Result<bool> {
    match keys.iter().find(|k| r.contains_key(*k)) {
        Some(k) => on(r, k),
        None => Ok(true),
    }
}
/// [`uid`] over the first present, non-empty field among `keys`.
fn uid_alias(r: &Row, keys: &[&str]) -> Result<usize> {
    match keys
        .iter()
        .find(|k| matches!(r.get(*k), Some(v) if !v.trim().is_empty()))
    {
        Some(k) => uid(r, k),
        None => Ok(0),
    }
}

fn bus_ref(
    r: &Row,
    num_keys: &[&str],
    label_keys: &[&str],
    bus_labels: &HashMap<&str, BusId>,
) -> Result<BusId> {
    let id = uid_alias(r, num_keys)?;
    if id != 0 {
        return Ok(BusId(id));
    }
    if let Some(label) = first(r, label_keys) {
        return bus_labels
            .get(label)
            .copied()
            .ok_or_else(|| Error::FormatRead {
                format: FMT,
                message: format!("unknown BusName_NomVolt label {label:?}"),
            });
    }
    Err(Error::FormatRead {
        format: FMT,
        message: format!(
            "row missing a bus key (expected one of {} or {})",
            num_keys.join("/"),
            label_keys.join("/")
        ),
    })
}

/// First present, non-empty field among `keys`, trimmed.
fn first<'a>(r: &Row<'a>, keys: &[&str]) -> Option<&'a str> {
    keys.iter()
        .find_map(|k| r.get(k).copied())
        .map(str::trim)
        .filter(|v| !v.is_empty())
}

/// First present, non-empty field among `keys` as f64; absent → `default`.
fn f_alias(r: &Row, keys: &[&str], default: f64) -> Result<f64> {
    match keys
        .iter()
        .find(|k| matches!(r.get(*k), Some(v) if !v.trim().is_empty()))
    {
        Some(k) => f_or(r, k, default),
        None => Ok(default),
    }
}

/// Copy `keys` into `extras` verbatim (trimmed of the padding PowerWorld pads
/// quoted values with), skipping absent or empty fields. The PowerWorld field
/// name is the extras key, so the provenance is self describing and the writer
/// can put the value back in the same field.
fn keep_extras(r: &Row, keys: &[&str], extras: &mut Extras) {
    for k in keys {
        if let Some(v) = r.get(k) {
            let v = v.trim();
            if !v.is_empty() {
                extras.insert((*k).to_string(), serde_json::Value::String(v.to_string()));
            }
        }
    }
}

/// `BusCat` (our writer's vocabulary) when present; real exports carry
/// `BusSlack` instead and the PV/PQ split is derived from the generators in
/// [`derive_bus_kinds`].
fn bus_kind(r: &Row) -> BusType {
    match r.get("BusCat").copied().map(str::trim) {
        Some("PV") => BusType::Pv,
        Some("Slack") => BusType::Ref,
        Some("Disconnected") => BusType::Isolated,
        _ => {
            if first(r, &["BusSlack", "Slack"]).is_some_and(|v| v.eq_ignore_ascii_case("YES")) {
                BusType::Ref
            } else {
                BusType::Pq
            }
        }
    }
}

/// PowerWorld stores no PV/PQ bus type; it follows from the machines. A bus
/// with an in-service generator regulates voltage (PV) unless it is the slack.
/// Only buses left at the PQ default are promoted, so an explicit `BusCat`
/// from our own writer is never overridden.
pub(super) fn derive_bus_kinds(buses: &mut [Bus], generators: &[Generator]) {
    use std::collections::HashSet;
    let gen_buses: HashSet<BusId> = generators
        .iter()
        .filter(|g| g.in_service)
        .map(|g| g.bus)
        .collect();
    for b in buses {
        if b.kind == BusType::Pq && gen_buses.contains(&b.id) {
            b.kind = BusType::Pv;
        }
    }
}

fn read_bus(r: &Row) -> Result<Bus> {
    // The bus's own identity goes through the same validation as every bus
    // reference (`uid`): a fractional or out-of-range id is a read error, not
    // a silently truncated or saturated id. Report whichever key carried the
    // value so an error names the column the file actually used.
    let (id_key, id_field) = ["BusNum", "Number"]
        .into_iter()
        .find_map(|k| first(r, &[k]).map(|v| (k, v)))
        .ok_or_else(|| Error::FormatRead {
            format: FMT,
            message: "Bus block row missing a numeric BusNum/Number".into(),
        })?;
    let id = bus_id_from_field(id_key, id_field)?;
    let name = first(r, &["BusName", "Name"]).map(ToString::to_string);
    let mut extras = Extras::new();
    // `SubNum` stays in extras: it is identity rather than geometry, and the
    // substation join reads it back.
    let promoted = bus_location(r);
    let location = promoted.map(|(location, _)| location);
    keep_extras(
        r,
        &["SubNum", "SubNumber", "OwnerNum", "OwnerNumber", "BANumber"],
        &mut extras,
    );
    // Only the pair that promoted leaves extras. A complete case export
    // carries both pairs, and the one the location did not come from is
    // unmodeled data like any other column.
    let used = promoted.map_or([""; 2], |(_, pair)| pair);
    for key in ["Latitude:1", "Longitude:1", "Latitude", "Longitude"] {
        if !used.contains(&key) {
            keep_extras(r, &[key], &mut extras);
        }
    }
    Ok(Bus {
        id: BusId(id),
        kind: bus_kind(r),
        vm: f_alias(r, &["BusPUVolt", "Vpu"], 1.0)?,
        va: f_alias(r, &["BusAngle", "Vangle"], 0.0)?,
        base_kv: f_alias(r, &["BusNomVolt", "NomkV"], 0.0)?,
        // Real exports carry per rating set voltage limits; set 1 (set A in
        // the 2022 vocabulary) is the default set. Our writer's
        // BusVMax/BusVMin are the fallback aliases.
        vmax: f_alias(r, &["BusVoltLimHigh:1", "LimitHighA", "BusVMax"], 1.1)?,
        vmin: f_alias(r, &["BusVoltLimLow:1", "LimitLowA", "BusVMin"], 0.9)?,
        evhi: None,
        evlo: None,
        area: uid_alias(r, &["AreaNum", "AreaNumber"])?,
        zone: uid_alias(r, &["ZoneNum", "ZoneNumber"])?,
        name,
        uid: None,
        location,
        extras,
    })
}

/// The promoted geographic location and the column pair it came from: the
/// substation `Latitude:1`/`Longitude:1` pair, else the bus's own bare
/// `Latitude`/`Longitude` pair (older complete case exports). A pair promotes
/// only when both of its own columns parse finite, so a half filled pair never
/// pairs a latitude with the other pair's longitude.
fn bus_location(r: &Row) -> Option<(crate::geo::Location, [&'static str; 2])> {
    for pair in [["Latitude:1", "Longitude:1"], ["Latitude", "Longitude"]] {
        let (Some(lat), Some(lon)) = (
            first(r, &pair[..1]).and_then(|v| v.parse::<f64>().ok()),
            first(r, &pair[1..]).and_then(|v| v.parse::<f64>().ok()),
        ) else {
            continue;
        };
        if lat.is_finite() && lon.is_finite() {
            return Some((
                crate::geo::Location {
                    x: lon,
                    y: lat,
                    kind: None,
                },
                pair,
            ));
        }
    }
    None
}

fn read_load(r: &Row, bus_labels: &HashMap<&str, BusId>, index: usize) -> Result<Load> {
    // Complete case exports write ZIP components (constant power S, constant
    // current I, constant impedance Z, each MW/MVAr at nominal voltage); the
    // simple LoadMW/LoadMVR pair is our own writer's form. The typed model
    // carries the total at nominal voltage; nonzero I/Z components are kept in
    // extras so nothing about the voltage dependence is lost.
    let (p, q);
    let mut extras = Extras::new();
    if r.contains_key("LoadMW") || r.contains_key("LoadMVR") {
        p = f(r, "LoadMW")?;
        q = f(r, "LoadMVR")?;
    } else {
        let smw = f_alias(r, &["LoadSMW", "SMW"], 0.0)?;
        let imw = f_alias(r, &["LoadIMW", "IMW"], 0.0)?;
        let zmw = f_alias(r, &["LoadZMW", "ZMW"], 0.0)?;
        let smvr = f_alias(r, &["LoadSMVR", "SMvar"], 0.0)?;
        let imvr = f_alias(r, &["LoadIMVR", "IMvar"], 0.0)?;
        let zmvr = f_alias(r, &["LoadZMVR", "ZMvar"], 0.0)?;
        p = smw + imw + zmw;
        q = smvr + imvr + zmvr;
        if imw != 0.0 || zmw != 0.0 || imvr != 0.0 || zmvr != 0.0 {
            keep_extras(
                r,
                &[
                    "LoadSMW", "LoadSMVR", "LoadIMW", "LoadIMVR", "LoadZMW", "LoadZMVR",
                ],
                &mut extras,
            );
        }
    }
    keep_extras(r, &["LoadID", "ID"], &mut extras);
    super::drop_positional_id(&mut extras, &["LoadID", "ID"], index);
    Ok(Load {
        bus: bus_ref(r, &["BusNum"], &["BusName_NomVolt"], bus_labels)?,
        p,
        q,
        voltage_model: None,
        in_service: on_alias(r, &["LoadStatus", "Status"])?,
        uid: None,
        extras,
    })
}

fn read_shunt(r: &Row, bus_labels: &HashMap<&str, BusId>, index: usize) -> Result<Shunt> {
    let mut extras = Extras::new();
    keep_extras(r, &["ShuntID", "ID", "SSCMode", "ShuntMode"], &mut extras);
    super::drop_positional_id(&mut extras, &["ShuntID", "ID"], index);
    Ok(Shunt {
        bus: bus_ref(r, &["BusNum"], &["BusName_NomVolt"], bus_labels)?,
        // Switched shunt nominal MW/MVAr in real exports (MWNom/MvarNom in
        // the 2022 vocabulary); ShuntMW/ShuntMVR from our writer.
        g: f_alias(r, &["ShuntMW", "SSNMW", "MWNom"], 0.0)?,
        b: f_alias(r, &["ShuntMVR", "SSNMVR", "MvarNom"], 0.0)?,
        in_service: on_alias(r, &["ShuntStatus", "SSStatus", "Status"])?,
        section_count: None,
        control: None,
        uid: None,
        extras,
    })
}

// `Generator` has no extras map (a deliberate parse-performance decision; see
// the `GenCaps` doc), so GenID and the regulation fields are not retained on
// the typed model. They stay reachable through the generic layer and survive
// aux → aux via the retained source.
fn read_gen(r: &Row, bus_labels: &HashMap<&str, BusId>) -> Result<Generator> {
    Ok(Generator {
        bus: bus_ref(r, &["BusNum"], &["BusName_NomVolt"], bus_labels)?,
        energy_source: GeneratorEnergySource::default(),
        // GenMW is the solved output; complete case exports write the
        // dispatch setpoint instead.
        pg: f_alias(r, &["GenMW", "GenMWSetPoint", "MWSetPoint"], 0.0)?,
        qg: f_alias(r, &["GenMVR", "GenMvrSetPoint", "MvarSetPoint"], 0.0)?,
        pmax: f_alias(r, &["GenMWMax", "MWMax"], 0.0)?,
        pmin: f_alias(r, &["GenMWMin", "MWMin"], 0.0)?,
        qmax: f_alias(r, &["GenMVRMax", "MvarMax"], 0.0)?,
        qmin: f_alias(r, &["GenMVRMin", "MvarMin"], 0.0)?,
        vg: f_alias(r, &["GenVoltSet", "VoltSet"], 1.0)?,
        mbase: f_alias(r, &["GenMVABase", "MVABase"], 100.0)?,
        in_service: on_alias(r, &["GenStatus", "Status"])?,
        cost: read_gen_cost(r)?,
        caps: Default::default(),
        voltage_regulation_on: true,
        regulating_terminal: None,
        regulated_bus: None,
        active_power_control: None,
        uid: None,
    })
}

/// The generator input-output curve as a polynomial cost in currency per hour.
///
/// A PowerWorld `Gen` row states the curve as a cubic in MBtu/hr
/// (`GenFixedCost` plus `GenIOB`, `GenIOC`, and `GenIOD` on the first three
/// powers of MW) together with the fuel cost per MBtu, so the product is the
/// cost curve. The four slots are read as four coefficients, highest order
/// first, which is the polynomial shape the row states.
fn read_gen_cost(r: &Row) -> Result<Option<GenCost>> {
    let Some(model) = first(r, &[GEN_COST_MODEL]) else {
        return Ok(None);
    };
    if !model.trim_matches('"').trim().eq_ignore_ascii_case("cubic") {
        return Ok(None);
    }
    let fuel = f_or(r, GEN_FUEL_COST, 1.0)?;
    let coefficients = [GEN_IO_D, GEN_IO_C, GEN_IO_B, GEN_FIXED_COST]
        .iter()
        .map(|key| f_or(r, key, 0.0).map(|value| value * fuel))
        .collect::<Result<Vec<f64>>>()?;
    if coefficients.iter().all(|value| *value == 0.0) {
        return Ok(None);
    }
    Ok(Some(GenCost::new(2, 0.0, 0.0, coefficients)))
}

fn read_branch(
    r: &Row,
    bus_labels: &HashMap<&str, BusId>,
    parallel: &mut HashMap<(BusId, BusId), u32>,
) -> Result<Branch> {
    let is_xf = first(r, &[BRANCH_DEVICE_TYPE]).is_some_and(|v| v == "Transformer");
    let from = bus_ref(
        r,
        &["BusNum", "BusNumFrom"],
        &["BusName_NomVolt"],
        bus_labels,
    )?;
    let to = bus_ref(
        r,
        &["BusNum:1", "BusNumTo"],
        &["BusName_NomVolt:1"],
        bus_labels,
    )?;
    let nth = parallel.entry((from, to)).or_insert(0);
    *nth += 1;
    let mut extras = Extras::new();
    // Branch identity beyond the bus pair: circuit ID and device type. Kept
    // verbatim (PowerWorld pads circuit IDs) so aux → aux through the typed
    // model reproduces them exactly — except when a value states exactly what
    // the writer derives on its own: the positional circuit for this bus pair,
    // or the Line/Transformer kind the tap already encodes. Exotic device
    // types (Breaker, Series Cap, ...) are always kept.
    if let Some(v) = r.get(LINE_CIRCUIT).or_else(|| r.get("Circuit")) {
        extras.insert(
            LINE_CIRCUIT.to_string(),
            serde_json::Value::String((*v).to_string()),
        );
    }
    keep_extras(r, &[BRANCH_DEVICE_TYPE, "LineLength"], &mut extras);
    // The circuit counter is per bus pair, so the second parallel branch's "2"
    // is as much a positional default as the first's "1". Dropped through the
    // shared rule rather than by a filter on the insert, so the aux and pwb
    // readers cannot disagree about what a default is.
    super::drop_positional_id(&mut extras, &[LINE_CIRCUIT], *nth as usize - 1);
    // Transformer records in complete case exports carry their impedance and
    // tap under `:1` locations (values on the system base after correction);
    // line records use the bare names. Our writer's LineXFRatio is the tap
    // fallback.
    // 2016 era exports use the bare name here like everywhere else.
    let tap = f_alias(
        r,
        &["LineTap:1", "Tapxfbase", "LineXFRatio", "LineTap"],
        1.0,
    )?;
    let stored_tap = if is_xf { tap } else { 0.0 };
    let shift = f_alias(r, &["LinePhase", "Phase"], 0.0)?;
    // Drop the device type only when the writer would derive this exact value
    // back. It derives from `tap != 0 || shift != 0`, so a record that states
    // `Line` while stating a phase shift does not round trip: dropping it here
    // rewrites the branch as a `Transformer`. Comparing against the rule
    // rather than against the pair of names is what keeps the two in step.
    let derived = if stored_tap != 0.0 || shift != 0.0 {
        "Transformer"
    } else {
        "Line"
    };
    if extras.get(BRANCH_DEVICE_TYPE).and_then(|v| v.as_str()) == Some(derived) {
        extras.remove(BRANCH_DEVICE_TYPE);
    }
    Ok(Branch {
        name: None,
        from,
        to,
        r: f_alias(r, &["LineR", "LineR:1", "R", "Rxfbase"], 0.0)?,
        x: f_alias(r, &["LineX", "LineX:1", "X", "Xxfbase"], 0.0)?,
        b: f_alias(r, &["LineC", "LineC:1", "B", "Bxfbase"], 0.0)?,
        charging: None,
        rate_a: f_alias(r, &["LineAMVA", "LimitMVAA"], 0.0)?,
        rate_b: f_alias(r, &["LineAMVA:1", "LineBMVA", "LimitMVAB"], 0.0)?,
        rate_c: f_alias(r, &["LineAMVA:2", "LineCMVA", "LimitMVAC"], 0.0)?,
        rating_sets: Vec::new(),
        current_ratings: None,
        tap: stored_tap,
        shift,
        in_service: on_alias(r, &["LineStatus", "Status"])?,
        angmin: -360.0,
        angmax: 360.0,
        control: None,
        solution: None,
        uid: None,
        route: None,
        extras,
    })
}

// ---- Writer -----------------------------------------------------------------

#[must_use]
// A flat serializer: one section per PowerWorld object type; splitting it would
// add indirection without clarity.
#[expect(clippy::too_many_lines)]
pub(crate) fn write_powerworld(net: &BalancedNetwork) -> TextEmission {
    let mut warnings = Diagnostics::new();
    let mut nonfinite = false;
    let mut sanitized_names = 0usize;
    let mut n = |x: f64| -> String {
        if x.is_finite() {
            format!("{x}")
        } else {
            nonfinite = true;
            format!(
                "{}",
                if x > 0.0 {
                    1.0e10
                } else if x < 0.0 {
                    -1.0e10
                } else {
                    0.0
                }
            )
        }
    };
    let mut s = String::new();
    // A `//` comment ends at the line break, so a terminator in the name
    // would leave the rest of it uncommented as an aux DATA block.
    let _ = writeln!(
        s,
        "// PowerWorld auxiliary file — powerio export: {}",
        sanitize_quoted(net.name(), NAME_FORBIDDEN, ' ')
    );
    let _ = writeln!(s, "// baseMVA {}", net.base_mva());
    let _ = writeln!(s);

    // The coordinate columns appear only when the case carries locations, so
    // a case without geometry writes exactly as before. A located case still
    // writes `""` for the odd bus without a point; the reader leaves those
    // unpromoted.
    let write_locations = net.buses().iter().any(|b| b.location.is_some());
    // The input-output curve columns appear only when a generator states a
    // polynomial cost; a case with no cost data writes the plain Gen row.
    let write_gen_cost = net
        .generators()
        .iter()
        .any(|g| g.cost.as_ref().is_some_and(|cost| cost.model == 2));
    block(
        &mut s,
        "Bus",
        if write_locations {
            "[BusNum, BusName, BusNomVolt, BusPUVolt, BusAngle, AreaNum, ZoneNum, BusVMax, BusVMin, BusCat, Latitude:1, Longitude:1]"
        } else {
            "[BusNum, BusName, BusNomVolt, BusPUVolt, BusAngle, AreaNum, ZoneNum, BusVMax, BusVMin, BusCat]"
        },
        |rows| {
            for b in net.buses() {
                let raw_name = b.name.as_deref().unwrap_or("");
                let name = sanitize_quoted(raw_name, NAME_FORBIDDEN, ' ');
                if matches!(name, std::borrow::Cow::Owned(_)) {
                    sanitized_names += 1;
                }
                let mut row = format!(
                    "{} \"{}\" {} {} {} {} {} {} {} \"{}\"",
                    b.id,
                    name,
                    n(b.base_kv),
                    n(b.vm),
                    n(b.va),
                    b.area,
                    b.zone,
                    n(b.vmax),
                    n(b.vmin),
                    bus_cat(b.kind)
                );
                if write_locations {
                    match b.location {
                        Some(location) => {
                            let _ = write!(row, " {} {}", n(location.y), n(location.x));
                        }
                        None => row.push_str(" \"\" \"\""),
                    }
                }
                rows.push(row);
            }
        },
    );

    block(
        &mut s,
        "Load",
        "[BusNum, LoadID, LoadMW, LoadMVR, LoadStatus]",
        |rows| {
            for (i, l) in net.loads().iter().enumerate() {
                rows.push(format!(
                    "{} \"{}\" {} {} \"{}\"",
                    l.bus,
                    id_of(&l.extras, "LoadID", i),
                    n(l.p),
                    n(l.q),
                    status(l.in_service)
                ));
            }
        },
    );

    block(
        &mut s,
        "Shunt",
        "[BusNum, ShuntID, ShuntMW, ShuntMVR, ShuntStatus]",
        |rows| {
            for (i, sh) in net.shunts().iter().enumerate() {
                rows.push(format!(
                    "{} \"{}\" {} {} \"{}\"",
                    sh.bus,
                    id_of(&sh.extras, "ShuntID", i),
                    n(sh.g),
                    n(sh.b),
                    status(sh.in_service)
                ));
            }
        },
    );

    block(
        &mut s,
        "Gen",
        if write_gen_cost {
            "[BusNum, GenID, GenMW, GenMVR, GenMWMax, GenMWMin, GenMVRMax, GenMVRMin, GenVoltSet, GenMVABase, GenStatus, GenCostModel, GenFuelCost, GenFixedCost, GenIOB, GenIOC, GenIOD]"
        } else {
            "[BusNum, GenID, GenMW, GenMVR, GenMWMax, GenMWMin, GenMVRMax, GenMVRMin, GenVoltSet, GenMVABase, GenStatus]"
        },
        |rows| {
            for (i, g) in net.generators().iter().enumerate() {
                let mut row = format!(
                    "{} \"{}\" {} {} {} {} {} {} {} {} \"{}\"",
                    g.bus,
                    i + 1,
                    n(g.pg),
                    n(g.qg),
                    n(g.pmax),
                    n(g.pmin),
                    n(g.qmax),
                    n(g.qmin),
                    n(g.vg),
                    n(g.mbase),
                    status(g.in_service)
                );
                if write_gen_cost {
                    // Highest order last in the row: the constant rides
                    // GenFixedCost and the three powers of MW ride
                    // GenIOB/GenIOC/GenIOD, at a unit fuel price so the
                    // input-output curve is the cost curve itself.
                    let mut coefficients = [0.0f64; GEN_IO_COEFFICIENTS];
                    if let Some(cost) = g.cost.as_ref().filter(|cost| cost.model == 2) {
                        for (slot, value) in coefficients
                            .iter_mut()
                            .zip(cost.coeffs.iter().rev().take(GEN_IO_COEFFICIENTS))
                        {
                            *slot = *value;
                        }
                    }
                    let _ = write!(
                        row,
                        " \"Cubic\" 1 {} {} {} {}",
                        n(coefficients[0]),
                        n(coefficients[1]),
                        n(coefficients[2]),
                        n(coefficients[3])
                    );
                }
                rows.push(row);
            }
        },
    );

    block(
        &mut s,
        "Branch",
        "[BusNum, BusNum:1, LineCircuit, LineR, LineX, LineC, LineAMVA, LineBMVA, LineCMVA, LineXFRatio, LinePhase, LineStatus, BranchDeviceType]",
        |rows| {
            // Parallel branches need distinct circuit IDs: the bus pair plus
            // circuit is the PowerWorld branch identity, and a reader (ours
            // included) treats equal identities as one device.
            let mut parallel: HashMap<(BusId, BusId), u32> = HashMap::new();
            for br in net.branches() {
                let kind = match br.extras.get(BRANCH_DEVICE_TYPE).and_then(|v| v.as_str()) {
                    Some(v) => v,
                    None if br.is_transformer() => "Transformer",
                    None => "Line",
                };
                let nth = parallel.entry((br.from, br.to)).or_insert(0);
                *nth += 1;
                let fallback = nth.to_string();
                let circuit = br
                    .extras
                    .get(LINE_CIRCUIT)
                    .and_then(|v| v.as_str())
                    .unwrap_or(&fallback);
                rows.push(format!(
                    "{} {} \"{}\" {} {} {} {} {} {} {} {} \"{}\" \"{}\"",
                    br.from,
                    br.to,
                    circuit,
                    n(br.r),
                    n(br.x),
                    n(br.calc_total_charging_b()),
                    n(br.rate_a),
                    n(br.rate_b),
                    n(br.rate_c),
                    n(br.calc_effective_tap()),
                    n(br.shift),
                    status(br.in_service),
                    kind
                ));
            }
        },
    );

    // A polynomial cost rides the input-output curve columns. What the row has
    // no field for is stated per field.
    let piecewise_costs = net
        .generators()
        .iter()
        .filter(|g| g.cost.as_ref().is_some_and(|cost| cost.model != 2))
        .count();
    if piecewise_costs > 0 {
        warnings.push(&F.field_dropped, format!(
            "{piecewise_costs} piecewise generator cost curve(s) dropped: a PowerWorld .aux Gen row states its cost as a cubic input-output curve and has no breakpoint field"
        ));
    }
    let long_costs = net
        .generators()
        .iter()
        .filter(|g| {
            g.cost
                .as_ref()
                .is_some_and(|cost| cost.model == 2 && cost.coeffs.len() > GEN_IO_COEFFICIENTS)
        })
        .count();
    if long_costs > 0 {
        warnings.push(&F.value_truncated, format!(
            "{long_costs} generator cost curve(s) truncated to a cubic: a PowerWorld .aux Gen row states GenFixedCost, GenIOB, GenIOC, and GenIOD and no higher power of MW"
        ));
    }
    let commitment_costs = net
        .generators()
        .iter()
        .filter(|g| {
            g.cost
                .as_ref()
                .is_some_and(|cost| cost.startup != 0.0 || cost.shutdown != 0.0)
        })
        .count();
    if commitment_costs > 0 {
        warnings.push(&F.field_dropped, format!(
            "{commitment_costs} generator startup and shutdown cost(s) dropped: a PowerWorld .aux Gen row states a running cost curve and no commitment cost"
        ));
    }
    // A cost curve with fewer than four coefficients reads back padded with
    // leading zeros, which states the same cost at every MW but not the same
    // coefficient count.
    let padded_costs = net
        .generators()
        .iter()
        .filter(|g| {
            g.cost
                .as_ref()
                .is_some_and(|cost| cost.model == 2 && cost.coeffs.len() < GEN_IO_COEFFICIENTS)
        })
        .count();
    if padded_costs > 0 {
        warnings.push(&F.value_defaulted, format!(
            "{padded_costs} generator cost curve(s) read back as four coefficients: a PowerWorld .aux Gen row states GenFixedCost, GenIOB, GenIOC, and GenIOD and no coefficient count, so a shorter curve returns padded with leading zeros"
        ));
    }
    if !net.hvdc().is_empty() {
        warnings.push(
            &F.record_dropped,
            format!(
                "{} dcline(s) dropped: a PowerWorld DC line is a separate object family, and no vendored \
                 .aux export states its field names, so writing one would invent a vocabulary",
                net.hvdc().len()
            ),
        );
    }
    if !net.transformers_3w().is_empty() {
        warnings.push(&F.record_dropped, format!(
            "{} 3-winding transformer(s) dropped: a PowerWorld .aux Branch row states two terminals, so a three winding record needs the star bus and three branches a projection would synthesize",
            net.transformers_3w().len()
        ));
    }
    if net
        .buses()
        .iter()
        .any(|b| b.evhi.is_some() || b.evlo.is_some())
    {
        warnings.push(
            &F.field_dropped,
            "emergency voltage band(s) (EVHI/EVLO) dropped: a PowerWorld .aux Bus row states one BusVMax/BusVMin pair",
        );
    }
    if !net.storage().is_empty() {
        warnings.push(
            &F.record_dropped,
            format!(
                "{} storage unit(s) dropped: a PowerWorld energy storage object is a separate object family, \
                 and no vendored .aux export states its field names, so writing one would invent \
                 a vocabulary",
                net.storage().len()
            ),
        );
    }
    let voltage_loads = net
        .loads()
        .iter()
        .filter(|l| {
            l.voltage_model
                .as_ref()
                .is_some_and(LoadVoltageModel::has_non_matpower_fields)
        })
        .count();
    if voltage_loads > 0 {
        warnings.push(&F.field_dropped, format!(
            "{voltage_loads} voltage dependent load model(s) dropped: a PowerWorld .aux Load row states constant MW/MVR, constant current, and constant impedance components and no exponential model or model nominal voltage"
        ));
    }
    let terminal_charging = net
        .branches()
        .iter()
        .filter(|b| b.has_non_matpower_charging())
        .count();
    if terminal_charging > 0 {
        warnings.push(&F.value_collapsed, format!(
            "{terminal_charging} branch terminal admittance record(s) collapsed to total susceptance: a PowerWorld .aux Branch row states one LineC and no terminal conductance"
        ));
    }
    let current_ratings = net
        .branches()
        .iter()
        .filter(|b| b.current_ratings.is_some())
        .count();
    if current_ratings > 0 {
        warnings.push(&F.field_dropped, format!(
            "{current_ratings} branch current rating record(s) dropped: a PowerWorld .aux Branch row states its ratings in MVA through LineAMVA, LineBMVA, and LineCMVA"
        ));
    }
    warn_extra_branch_rating_sets(&F, "PowerWorld .aux", net, &mut warnings);
    // The keys this writer replays: the two device ids, the branch circuit,
    // and the device type. Anything else a reader retained — a foreign
    // format's ids, ZIP components, LineLength — is dropped, and says so
    // (#330).
    super::super::warn_dropped_extras(
        &F,
        "PowerWorld .aux",
        net,
        |key| matches!(key, "LoadID" | "ShuntID" | "BranchDeviceType") || key == LINE_CIRCUIT,
        &mut warnings,
    );
    super::super::warn_dropped_areas(&F, "PowerWorld .aux", true, net, &mut warnings);
    let branch_solutions = net
        .branches()
        .iter()
        .filter(|b| b.solution.is_some())
        .count();
    if branch_solutions > 0 {
        warnings.push(&F.field_dropped, format!(
            "{branch_solutions} branch solution value set(s) dropped: a PowerWorld .aux flow field is a case result, and no vendored .aux export states its field names, so writing one would invent a vocabulary"
        ));
    }
    if net.branches().iter().any(Branch::has_angle_limits) {
        warnings.push(
            &F.field_dropped,
            "branch angle limits (angmin/angmax) dropped: a PowerWorld .aux Branch row states no angle difference limit",
        );
    }
    if net.generators().iter().any(Generator::has_caps) {
        warnings.push(
            &F.field_dropped,
            "generator ramp/capability columns dropped: a PowerWorld .aux Gen row states no reactive capability curve point, and its ramp rate fields are named by no vendored export",
        );
    }
    if nonfinite {
        warnings.push(
            &F.not_a_number,
            "non-finite values written as ±1e10 sentinels",
        );
    }
    if sanitized_names > 0 {
        warnings.push(
            &F.value_substituted,
            format!(
                "{sanitized_names} bus name(s) contained a double quote that would corrupt a \
             PowerWorld value; replaced with spaces"
            ),
        );
    }

    TextEmission::new(s, warnings)
}

/// Device ID for the writer: the retained PowerWorld ID from `extras` when the
/// element came from an aux read, else the 1-based position.
fn id_of(extras: &Extras, key: &str, index: usize) -> String {
    match extras.get(key).and_then(serde_json::Value::as_str) {
        Some(v) => v.to_string(),
        None => (index + 1).to_string(),
    }
}

fn block(s: &mut String, object: &str, fields: &str, fill: impl FnOnce(&mut Vec<String>)) {
    let mut rows = Vec::new();
    fill(&mut rows);
    let _ = writeln!(s, "DATA ({object}, {fields})");
    let _ = writeln!(s, "{{");
    for r in &rows {
        let _ = writeln!(s, "  {r}");
    }
    let _ = writeln!(s, "}}");
    let _ = writeln!(s);
}

fn status(on: bool) -> &'static str {
    if on { "Closed" } else { "Open" }
}

fn bus_cat(kind: BusType) -> &'static str {
    match kind {
        BusType::Pq => "PQ",
        BusType::Pv => "PV",
        BusType::Ref => "Slack",
        BusType::Isolated => "Disconnected",
    }
}

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

    #[test]
    fn read_bus_rejects_non_integral_and_out_of_range_ids() {
        // The bus's own identity goes through the same guard as every bus
        // reference: a fractional or oversized BusNum is a read error, not a
        // silently truncated or saturated id.
        for bad in ["5.7", "1e20", "-3", "NaN", "inf"] {
            let mut row: Row = HashMap::new();
            row.insert("BusNum", bad);
            assert!(
                read_bus(&row).is_err(),
                "BusNum={bad} should be refused, not bent to an id"
            );
        }
        let mut ok: Row = HashMap::new();
        ok.insert("BusNum", "42");
        assert_eq!(read_bus(&ok).unwrap().id, BusId(42));
    }
}