Variables: Import… in the panel menu reads a CSV or an XML data source

The Variables panel menu gets Import…, which opens one file dialog for
CSV, TSV, TXT and XML files. `variable.import {path}` picks the reader by
the file's extension, in any case: a .csv, .tsv or .txt file is read as
Import Data Sets… reads it, and an .xml file as Load Variable Library…
reads it. Any other extension, or none, is refused before the file is
read. Import Data Sets… and Load Variable Library… stay in the menu.

Docs: docs/mcp.md, docs/target-app-parity.md.
This commit is contained in:
dotCooCoo
2026-11-15 06:28:41 -08:00
parent 0995c130d1
commit bd19ed1623
29 changed files with 142 additions and 0 deletions
+26
View File
@@ -11,6 +11,7 @@
//! visibility, `@` for image, no prefix for text). `variable.loadLibrary` replaces the variables and data sets with the
//! variables and data sets of a variable library, the XML file of the reference app's Save
//! Variable Library, and `variable.saveLibrary` writes the variables and data sets as one.
//! `variable.import` (the panel's Import…) runs one of the two readers by the file's extension.
//! Agents reach every command through `run_command`, like all engine commands.
use std::collections::{BTreeMap, BTreeSet};
@@ -697,6 +698,22 @@ fn dataset_step(s: &mut Session, cmd: &str, forward: bool) -> Result<Value> {
apply_dataset(s, &name)
}
/// The Variables panel's Import…: a data source file read by its extension, a CSV, TSV or TXT
/// file as `dataset.import` reads it and an XML file as `variable.loadLibrary` reads it. The
/// result is that command's, with `format` set to `csv` or `xml`.
fn variable_import(s: &mut Session, p: &Value) -> Result<Value> {
const C: &str = "variable.import";
let path = str_param(p, "path").ok_or_else(|| bad(C, "missing `path`"))?;
let ext = std::path::Path::new(path).extension().and_then(|e| e.to_str()).map(str::to_ascii_lowercase);
let (format, mut out) = match ext.as_deref() {
Some("csv" | "tsv" | "txt") => ("csv", dataset_import(s, &json!({"path": path}))?),
Some("xml") => ("xml", load_library(s, &json!({"path": path}))?),
_ => return Err(bad(C, "Import reads a data source file ending in .csv, .tsv, .txt or .xml")),
};
out["format"] = json!(format);
Ok(out)
}
pub fn specs() -> Vec<CommandSpec> {
vec![
cmd!(
@@ -872,6 +889,15 @@ pub fn specs() -> Vec<CommandSpec> {
has_doc,
dataset_import
),
cmd!(
"variable.import",
"Import…",
["Window", "Variables"],
None,
"{path} import a data source file, chosen by its extension (any case): a .csv, .tsv or .txt file is read as dataset.import reads it, and an .xml file as variable.loadLibrary reads it, with their refusals and undo steps; any other extension, or none, is refused before the file is read → the result of that command with `format`: `csv` or `xml`",
has_doc,
variable_import
),
cmd!(
query "dataset.export",
"Export Data Sets…",