powerio-cli 0.11.7

Command line interface and ratatui TUI for powerio: parse and convert power system case files and emit matrices.
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
//! Case file discovery, family inference, and loading, shared by the CLI
//! subcommands and the TUI.

use std::path::{Path, PathBuf};

use anyhow::Context;
use powerio::BalancedNetwork;
use powerio::transform::{MulticonductorToBalancedOptions, to_balanced_network};
use powerio_tx::format::routing::{Detection, JsonClass, SourceFormat as DetectedFormat};

/// Extensions (lowercase) that identify a transmission case file. `.cdf`
/// names an IEEE CDF case; the archives' `.txt` spelling is not discovered
/// because a text file of any kind carries that extension.
pub const TRANSMISSION_EXTENSIONS: &[&str] = &["m", "raw", "aux", "epc", "pwb", "uct", "cdf"];

/// Extensions (lowercase) that identify a distribution case file. `.pwd` is
/// the PowerWorld display sibling with no case data and stays excluded.
pub const DISTRIBUTION_EXTENSIONS: &[&str] = &["dss", "glm"];

/// `.json` carries no family signal; the shared JSON shape classifier decides.
const JSON_EXTENSION: &str = "json";

/// Extension list for error and empty-state messages; a unit test keeps it in
/// sync with the constants above.
pub const CASE_EXTENSIONS_LABEL: &str = ".m, .raw, .aux, .epc, .pwb, .uct, .cdf, .json, .dss, .glm";

/// Infer the case family from clear extensions, for `.json` the shared JSON
/// shape classifier, and for `.xml` files and directories the distribution
/// CIM check. `Some(true)` is distribution, `Some(false)` is transmission, and
/// `None` means the input carries no family signal.
pub fn infer_input_family(input: &Path) -> anyhow::Result<Option<bool>> {
    let ext = input
        .extension()
        .and_then(|e| e.to_str())
        .map(str::to_ascii_lowercase);
    if looks_like_cim_dist_input(input, ext.as_deref()) {
        return Ok(Some(true));
    }
    let Some(ext) = ext.as_deref() else {
        return Ok(None);
    };
    if TRANSMISSION_EXTENSIONS.contains(&ext) {
        return Ok(Some(false));
    }
    if DISTRIBUTION_EXTENSIONS.contains(&ext) {
        return Ok(Some(true));
    }
    Ok(classified_json(input)?.map(|case| case.is_distribution()))
}

/// Whether a `.xml` file, or a directory holding one at its top level, reads
/// as distribution CIM.
fn looks_like_cim_dist_input(input: &Path, ext: Option<&str>) -> bool {
    cim_dist_source_with_ext(input, ext).is_some()
}

/// The opened source of a `.xml` file, or of a directory holding one at its
/// top level, that reads as distribution CIM. Each file's first 16 KiB settle
/// the header checks first, so a CGMES or XIIDM input is not read in full here
/// before its own parse reads it. The source keeps the documents the check
/// read, so the parse that takes it reads no file twice.
pub fn cim_dist_source(input: &Path) -> Option<powerio_core::Source> {
    let ext = input
        .extension()
        .and_then(|e| e.to_str())
        .map(str::to_ascii_lowercase);
    cim_dist_source_with_ext(input, ext.as_deref())
}

fn cim_dist_source_with_ext(input: &Path, ext: Option<&str>) -> Option<powerio_core::Source> {
    let candidate = if input.is_dir() {
        std::fs::read_dir(input).is_ok_and(|entries| {
            entries.flatten().any(|entry| {
                let path = entry.path();
                path.is_file()
                    && path
                        .extension()
                        .and_then(|e| e.to_str())
                        .is_some_and(|e| e.eq_ignore_ascii_case("xml"))
                    && cim_dist_head(&path)
            })
        })
    } else {
        ext == Some("xml") && cim_dist_head(input)
    };
    if !candidate {
        return None;
    }
    let source = powerio_core::Source::open(input).ok()?;
    powerio_dist::source_looks_like_cim_dist(&source).then_some(source)
}

/// Whether a file's first 16 KiB open like a distribution CIM document.
fn cim_dist_head(path: &Path) -> bool {
    use std::io::Read;
    let Ok(file) = std::fs::File::open(path) else {
        return false;
    };
    let mut head = Vec::with_capacity(powerio_dist::CIM_DIST_HEAD_BYTES);
    if file
        .take(powerio_dist::CIM_DIST_HEAD_BYTES as u64)
        .read_to_end(&mut head)
        .is_err()
    {
        return false;
    }
    powerio_dist::looks_like_cim_dist_header(&head)
}

/// A `.json` case read and classified in one pass. The text rides along so the
/// typed parse reuses it instead of reading the file a second time.
pub struct ClassifiedCase {
    pub text: String,
    pub format: DetectedFormat,
}

impl ClassifiedCase {
    pub fn is_distribution(&self) -> bool {
        matches!(self.format, DetectedFormat::Distribution(_))
    }
}

/// The PowerIO IR document text when `input` is one, `Ok(None)` for anything
/// else. Single file commands send these to `deserialize` instead of the grid
/// exchange format classifier.
pub fn powerio_ir_text(input: &Path) -> anyhow::Result<Option<String>> {
    let ext = input
        .extension()
        .and_then(|e| e.to_str())
        .map(str::to_ascii_lowercase);
    if ext.as_deref() != Some(JSON_EXTENSION) {
        return Ok(None);
    }
    let text = std::fs::read_to_string(input)
        .with_context(|| format!("reading JSON format markers from {}", input.display()))?;
    if matches!(
        powerio_tx::format::routing::classify_json_text(&text),
        JsonClass::Module
    ) {
        return Ok(Some(text));
    }
    Ok(None)
}

/// Read and classify `input` when it is a `.json` file; `Ok(None)` for every
/// other extension, whose family (if any) is named without touching the file.
/// The single-file routes hand the returned text straight to the typed parser
/// — the read-once rule #260 established for `batch` and the TUI.
pub fn classified_json(input: &Path) -> anyhow::Result<Option<ClassifiedCase>> {
    let ext = input
        .extension()
        .and_then(|e| e.to_str())
        .map(str::to_ascii_lowercase);
    if ext.as_deref() != Some(JSON_EXTENSION) {
        return Ok(None);
    }
    let text = std::fs::read_to_string(input)
        .with_context(|| format!("reading JSON format markers from {}", input.display()))?;
    let format = classify_case_json(&text, input)?;
    if !matches!(
        format,
        DetectedFormat::Transmission(_) | DetectedFormat::Distribution(_)
    ) {
        return Err(crate::cli_failure(
            &crate::codes::REQUEST_CLI_OPTION_INVALID,
            format!(
                "unrecognized JSON format family `{}` in {}; pass --from to choose a format",
                format.name(),
                input.display()
            ),
        ));
    }
    Ok(Some(ClassifiedCase { text, format }))
}

/// Classify `.json` case text to its detected format, turning the non-case
/// outcomes (package, ambiguous markers, no markers) into errors
/// that name the fix.
fn classify_case_json(text: &str, path: &Path) -> anyhow::Result<DetectedFormat> {
    let refuse =
        |message: String| crate::cli_failure(&crate::codes::REQUEST_CLI_OPTION_INVALID, message);
    match powerio_tx::format::routing::classify_json_text(text) {
        JsonClass::Case(Detection::Known(format)) => Ok(format),
        JsonClass::Module => Err(refuse(format!(
            "{} is PowerIO IR; `summary`, `convert`, `verify`, `dcopf`, and \
             `sensitivities` read it directly, and `deserialize` loads it in the \
             language APIs",
            path.display()
        ))),
        JsonClass::Case(Detection::Ambiguous) => Err(refuse(format!(
            "ambiguous JSON markers in {}; pass --from to choose a format",
            path.display()
        ))),
        JsonClass::Case(Detection::Unknown) => Err(refuse(format!(
            "cannot infer JSON format for {}; pass --from to choose a format",
            path.display()
        ))),
    }
}

pub fn looks_like_distribution_input(input: &Path) -> anyhow::Result<bool> {
    Ok(infer_input_family(input)?.unwrap_or(false))
}

/// Recursively list case files under `root`, sorted by path. Hidden entries
/// are pruned, as is the `exclude` directory subtree (the batch output dir,
/// so a rerun never rediscovers its own exports) unless it is `root` itself.
/// A missing or unreadable `root` yields an empty list.
pub fn discover_cases(root: &Path, exclude: Option<&Path>) -> Vec<PathBuf> {
    let excluded = exclude
        .and_then(|p| p.canonicalize().ok())
        .filter(|p| root.canonicalize().ok().as_ref() != Some(p));
    let mut cases: Vec<PathBuf> = walkdir::WalkDir::new(root)
        .into_iter()
        .filter_entry(|e| !is_hidden(e) && !is_excluded_dir(e, excluded.as_deref()))
        .filter_map(std::result::Result::ok)
        .filter(|e| e.file_type().is_file() && has_case_extension(e.path()))
        .map(walkdir::DirEntry::into_path)
        .collect();
    cases.sort();
    cases
}

fn is_hidden(entry: &walkdir::DirEntry) -> bool {
    entry.depth() > 0
        && entry
            .file_name()
            .to_str()
            .is_some_and(|n| n.starts_with('.'))
}

fn is_excluded_dir(entry: &walkdir::DirEntry, excluded: Option<&Path>) -> bool {
    let Some(excluded) = excluded else {
        return false;
    };
    entry.file_type().is_dir() && entry.path().canonicalize().is_ok_and(|p| p == excluded)
}

fn has_case_extension(path: &Path) -> bool {
    let Some(ext) = path
        .extension()
        .and_then(|e| e.to_str())
        .map(str::to_ascii_lowercase)
    else {
        return false;
    };
    let ext = ext.as_str();
    TRANSMISSION_EXTENSIONS.contains(&ext)
        || DISTRIBUTION_EXTENSIONS.contains(&ext)
        || ext == JSON_EXTENSION
}

/// A case loaded to the transmission model, whatever family it came from.
pub struct LoadedCase {
    pub network: BalancedNetwork,
    pub warnings: Vec<String>,
}

/// Load one case file as a transmission [`BalancedNetwork`]. Distribution inputs
/// (`.dss`, BMOPF/PMD `.json`) parse to the multiconductor model and go
/// through the explicit balanced lowering pass, whose approximations and
/// dropped fields surface as warnings. A `.dss` redirect fragment (no voltage
/// source) fails the lowering preflight, so a recursive scan skips it instead
/// of exporting a partial feeder.
///
/// A `.json` case is read and classified once; the classifier's verdict names
/// the exact format, so the typed parse is the only other pass over the text.
pub fn load_network(path: &Path) -> anyhow::Result<LoadedCase> {
    let ext = path
        .extension()
        .and_then(|e| e.to_str())
        .map(str::to_ascii_lowercase);
    let stem = path.file_stem().and_then(|s| s.to_str());
    match ext.as_deref() {
        Some(JSON_EXTENSION) => {
            let text = std::fs::read_to_string(path)
                .with_context(|| format!("reading {}", path.display()))?;
            match classify_case_json(&text, path)? {
                DetectedFormat::Distribution(format) => {
                    let parsed = crate::module_io::load_multiconductor_memory(&text, format.name())
                        .with_context(|| format!("parse {}", path.display()))?;
                    lower_to_balanced(parsed, stem, path)
                }
                DetectedFormat::Transmission(format) => {
                    let parsed =
                        crate::module_io::load_balanced_memory_named(&text, format.name(), stem)
                            .with_context(|| format!("parse {}", path.display()))?;
                    Ok(LoadedCase {
                        warnings: powerio_core::render_diagnostics(parsed.diagnostics()),
                        network: parsed.into_value(),
                    })
                }
                other => anyhow::bail!(
                    "unrecognized JSON format family `{}` in {}; pass --from to choose a format",
                    other.name(),
                    path.display()
                ),
            }
        }
        Some(ext) if DISTRIBUTION_EXTENSIONS.contains(&ext) => {
            let parsed = crate::module_io::load_multiconductor_module(path, None)
                .with_context(|| format!("parse {}", path.display()))?;
            lower_to_balanced(parsed, stem, path)
        }
        Some(ext) if TRANSMISSION_EXTENSIONS.contains(&ext) => {
            let parsed = crate::module_io::load_balanced_module(path, None)
                .with_context(|| format!("parse {}", path.display()))?;
            Ok(LoadedCase {
                warnings: powerio_core::render_diagnostics(parsed.diagnostics()),
                network: parsed.into_value(),
            })
        }
        _ => anyhow::bail!("cannot infer a case format for {}", path.display()),
    }
}

/// Lower a parsed multiconductor network to the balanced model. A nameless
/// case takes its file stem as the network name first (the role the stem
/// plays for transmission formats); otherwise every nameless case in a batch
/// lowers to the same `lowered-multiconductor` fallback and the exports
/// overwrite each other.
fn lower_to_balanced(
    parsed: powerio_core::PioModule<powerio_dist::MulticonductorNetwork>,
    stem: Option<&str>,
    path: &Path,
) -> anyhow::Result<LoadedCase> {
    let mut warnings = powerio_core::render_diagnostics(parsed.diagnostics());
    let mut net = parsed.into_value();
    if net.name().is_none() {
        *net.name_mut() = stem.map(str::to_owned);
    }
    let lowered =
        to_balanced_network(&net, MulticonductorToBalancedOptions::default()).map_err(|e| {
            let diagnostics = e
                .diagnostics
                .iter()
                .map(powerio_core::Diagnostic::message)
                .collect::<Vec<_>>()
                .join("; ");
            if diagnostics.is_empty() {
                // Diagnostics should always be present on a refusal, but
                // an empty list must not render a bare trailing colon.
                anyhow::anyhow!("lower {} to balanced: {e}", path.display())
            } else {
                anyhow::anyhow!("lower {} to balanced: {diagnostics}", path.display())
            }
        })?;
    warnings.extend(powerio_core::render_diagnostics(&lowered.diagnostics));
    Ok(LoadedCase {
        network: lowered.network,
        warnings,
    })
}

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

    fn touch(path: &Path) {
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(path, "").unwrap();
    }

    #[test]
    fn discovers_recursively_sorted_and_prunes() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        for rel in [
            "case1.m",
            "a/case2.json",
            "a/net.aux",
            "a/b/c/deep.raw",
            "a/b/c/w.pwb",
            "a/b/c/sys.epc",
            "a/b/feeder.dss",
            "notes.txt",
            "display.pwd",
            ".hidden.m",
            ".git/skip.m",
            "out/case1_meta.json",
        ] {
            touch(&root.join(rel));
        }
        let found = discover_cases(root, Some(&root.join("out")));
        let expected: Vec<PathBuf> = [
            "a/b/c/deep.raw",
            "a/b/c/sys.epc",
            "a/b/c/w.pwb",
            "a/b/feeder.dss",
            "a/case2.json",
            "a/net.aux",
            "case1.m",
        ]
        .iter()
        .map(|rel| root.join(rel))
        .collect();
        assert_eq!(found, expected);
    }

    #[test]
    fn extension_match_ignores_case() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        for rel in ["CASE.M", "net.RAW", "feeder.DSS", "case.Json", "w.PwB"] {
            touch(&root.join(rel));
        }
        assert_eq!(discover_cases(root, None).len(), 5);
    }

    #[test]
    fn exclude_equal_to_root_is_ignored() {
        let tmp = tempfile::tempdir().unwrap();
        let root = tmp.path();
        touch(&root.join("case1.m"));
        assert_eq!(discover_cases(root, Some(root)), vec![root.join("case1.m")]);
    }

    #[test]
    fn missing_root_yields_empty() {
        let tmp = tempfile::tempdir().unwrap();
        assert_eq!(
            discover_cases(&tmp.path().join("nope"), None),
            [] as [std::path::PathBuf; 0]
        );
    }

    #[test]
    fn nameless_distribution_json_takes_the_file_stem_name() {
        let dss = concat!(
            env!("CARGO_MANIFEST_DIR"),
            "/../tests/data/dist/micro/fourwire_linecode.dss"
        );
        let source = powerio_core::Source::open(dss).unwrap();
        let module = powerio::dist::parse(source).unwrap();
        let emission = crate::module_io::emit_multiconductor_module(
            &module,
            powerio_dist::DistTargetFormat::BmopfJson,
        )
        .unwrap();
        let mut doc: serde_json::Value = serde_json::from_str(&emission.text).unwrap();
        doc.as_object_mut().unwrap().remove("name");
        let tmp = tempfile::tempdir().unwrap();
        let path = tmp.path().join("myfeeder.json");
        std::fs::write(&path, doc.to_string()).unwrap();
        let loaded = load_network(&path).unwrap();
        assert_eq!(loaded.network.name(), "myfeeder");
    }

    #[test]
    fn label_lists_every_extension() {
        for ext in TRANSMISSION_EXTENSIONS
            .iter()
            .chain(DISTRIBUTION_EXTENSIONS)
            .chain(std::iter::once(&JSON_EXTENSION))
        {
            assert!(
                CASE_EXTENSIONS_LABEL.contains(&format!(".{ext}")),
                "label is missing .{ext}"
            );
        }
    }
}