byob-sheets 0.3.12

Spreadsheets as a native plugin backend (IronCalc): the bundled sheets plugin's backend (L4)
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
//! Charts drawn from a sheet: `chart_sheet` makes a chart artifact from a sheet's columns
//! (the computed values, formulas included) that remembers where it came from
//! ([`byob_types::chart::ChartSource`]), and [`refresh`] draws those charts again from the
//! sheet each time it is saved, by a tool or by the user's edits in the grid, so a chart
//! never goes stale and nobody copies numbers by hand.

use byob_types::chart::{self, ChartSource};
use ironcalc::base::cell::CellValue;
use serde_json::{Map, Value, json};

use crate::SheetError;
use crate::book::Book;
use crate::refs::{col_name, col_num};
use crate::store::{Source, Store, metas, resolve, s};
use crate::tools::{Ctx, sheet_index};

/// The chart artifact type (the bundled charts plugin's).
pub const CHART_TYPE: &str = "application/vnd.byob.chart+json";
/// Most charts kept in step with one sheet per save, and listed for it.
pub const MAX_LINKED: usize = 24;

type R<T> = Result<T, SheetError>;

fn msg<T>(m: impl Into<String>) -> R<T> {
    Err(SheetError::Msg(m.into()))
}

/// The header row: the first non-empty row of the sheet's used area (1 when empty).
fn header_row(book: &Book, sh: u32) -> i64 {
    book.used(sh).map_or(1, |(r1, _, _, _)| r1)
}

/// What a column is called: its header text, else its letter.
fn header(book: &Book, sh: u32, row: i64, col: i64) -> String {
    let t = book.text(sh, row, col).trim().to_owned();
    if t.is_empty() { col_name(col) } else { t }
}

/// The column the model named: a header (any case) or a letter.
fn column(book: &Book, sh: u32, row: i64, name: &str) -> R<i64> {
    let want = name.trim();
    if want.is_empty() {
        return msg("Name the column, by its header or letter.");
    }
    if let Some((_, _, c1, c2)) = book.used(sh) {
        for c in c1..=c2 {
            if book.text(sh, row, c).trim().eq_ignore_ascii_case(want) {
                return Ok(c);
            }
        }
    }
    // A column letter, if it is one the sheet uses ("Net" isn't column NET).
    if (1..=3).contains(&want.len()) && want.bytes().all(|b| b.is_ascii_alphabetic()) {
        let c = col_num(&want.to_ascii_uppercase());
        if book.used(sh).is_some_and(|(_, _, c1, c2)| (c1..=c2).contains(&c)) {
            return Ok(c);
        }
    }
    let headers: Vec<String> = book.used(sh).map(|(_, _, c1, c2)| (c1..=c2).map(|c| header(book, sh, row, c)).collect()).unwrap_or_default();
    msg(format!("There's no column \"{want}\" in row {row}. Columns: {}.", headers.join(", ")))
}

/// A cell as chart data: numbers as numbers, the rest as shown; empty and error cells
/// (`#DIV/0!`) are `null`, a gap, as spreadsheets draw them.
fn cell(book: &Book, sh: u32, r: i64, c: i64, as_text: bool) -> Value {
    if book.kind(sh, r, c) == crate::book::Kind::E {
        return Value::Null;
    }
    match book.value(sh, r, c) {
        CellValue::None => Value::Null,
        CellValue::Number(n) if !as_text => serde_json::Number::from_f64(n).map_or(Value::Null, Value::Number),
        _ => {
            let t = book.text(sh, r, c);
            if t.is_empty() { Value::Null } else { Value::String(t) }
        }
    }
}

/// The chart's rows and series from the sheet: from under the header to the last row with
/// an `x`. Keys are the headers.
fn rows(book: &mut Book, src: &ChartSource, kind: chart::ChartType) -> R<(String, Vec<Value>, Vec<Map<String, Value>>)> {
    let sh = sheet_index(book, (!src.tab.is_empty()).then_some(src.tab.as_str()), false, None)?;
    let hr = i64::from(src.header_row);
    let xc = col_num(&src.x);
    let x = header(book, sh, hr, xc);
    let mut series = Vec::new();
    let mut keys = Vec::new();
    for letter in &src.series {
        let c = col_num(letter);
        let mut key = header(book, sh, hr, c);
        if key == x || keys.iter().any(|(k, _)| *k == key) {
            key = format!("{key} ({letter})");
        }
        series.push(json!({"key": key, "label": key}));
        keys.push((key, c));
    }
    let used_last = book.used(sh).map_or(hr, |(_, r2, _, _)| r2);
    let last = src.last_row.map_or(used_last, |r| i64::from(r).min(used_last));
    let mut out = Vec::new();
    for r in hr + 1..=last {
        let xv = cell(book, sh, r, xc, kind != chart::ChartType::Scatter);
        if xv.is_null() || out.len() >= chart::MAX_ROWS {
            break;
        }
        let mut row = Map::new();
        row.insert(x.clone(), xv);
        for (k, c) in &keys {
            row.insert(k.clone(), cell(book, sh, r, *c, false));
        }
        out.push(row);
    }
    if out.is_empty() {
        return msg(format!("There are no rows under the header (row {hr}) with a value in column {}.", src.x));
    }
    Ok((x, series, out))
}

/// The chart spec for `src`, checked (`base` keeps the chart's type, title, unit, stacking).
fn spec(book: &mut Book, src: &ChartSource, base: &Map<String, Value>) -> R<Value> {
    let kind = base.get("type").and_then(Value::as_str).and_then(chart::ChartType::from_name).unwrap_or(chart::ChartType::Bar);
    let (x, series, data) = rows(book, src, kind)?;
    let mut o = base.clone();
    o.insert("x".into(), json!(x));
    o.insert("series".into(), Value::Array(series));
    o.insert("data".into(), Value::Array(data.into_iter().map(Value::Object).collect()));
    o.insert("source".into(), serde_json::to_value(src).unwrap_or(Value::Null));
    let checked = chart::validate(&Value::Object(o)).map_err(SheetError::Msg)?;
    serde_json::to_value(&checked).map_err(|e| SheetError::Msg(e.to_string()))
}

/// A linked chart of this chat: its latest spec and source.
fn linked_chart(ws: &str, chat: Option<&str>, id: &str) -> R<(Value, ChartSource)> {
    let meta = crate::store::read_meta(ws, id).filter(|m| chat.is_none() || s(m, "chat") == chat);
    let Some(meta) = meta.filter(|m| s(m, "content_type") == Some(CHART_TYPE)) else {
        return msg(format!("There's no chart {id} in this chat."));
    };
    let spec = crate::store::latest(ws, &meta).1.and_then(|p| std::fs::read_to_string(p).ok()).and_then(|t| serde_json::from_str::<Value>(&t).ok());
    let Some(spec) = spec else { return msg(format!("Chart {id} couldn't be read.")) };
    let Some(src) = spec.get("source").and_then(|v| serde_json::from_value::<ChartSource>(v.clone()).ok()) else {
        return msg(format!("Chart {id} isn't drawn from a sheet; make a new one with chart_sheet."));
    };
    Ok((spec, src))
}

/// Column names the model gave (`series`: a list, or text with commas).
fn names(v: Option<&Value>) -> Vec<String> {
    match v {
        Some(Value::Array(a)) => a.iter().filter_map(Value::as_str).map(str::to_owned).collect(),
        Some(Value::String(t)) => t.split(',').map(str::trim).filter(|t| !t.is_empty()).map(str::to_owned).collect(),
        _ => Vec::new(),
    }
}

/// `chart_sheet`: a chart artifact drawn from a sheet, kept in step with it; with `chart`, a
/// change to that linked chart (type, title, columns; what isn't given stays).
pub fn chart_sheet(store: &mut Store, cx: &Ctx<'_>, args: &Value) -> R<Value> {
    let ws = cx.ws()?;
    let given = |k: &str| args.get(k).filter(|v| !v.is_null() && v.as_str() != Some(""));
    let existing = match given("chart").and_then(Value::as_str) {
        Some(id) => Some((id.to_owned(), linked_chart(ws, cx.chat, id)?)),
        None => None,
    };
    let sheet_arg = match (given("sheet"), &existing) {
        (Some(v), _) => Some(v.clone()),
        (None, Some((_, (_, old)))) => Some(json!(old.sheet)),
        (None, None) => None,
    };
    let src = resolve(ws, cx.chat, sheet_arg.as_ref(), true)?;
    if !src.artifact {
        return msg("chart_sheet charts a spreadsheet artifact: make one from the file with create_sheet (`from`) first, then chart that.");
    }
    // A chart follows only its own chat's sheets.
    if cx.chat.is_some() && s(&src.meta, "chat").is_some_and(|c| Some(c) != cx.chat) {
        return msg(format!("{} belongs to another chat; chart a spreadsheet of this chat (import the file with create_sheet `from`).", src.aid));
    }
    let old = existing.as_ref().map(|(_, (spec, src))| (spec, src)).filter(|(_, o)| o.sheet == src.aid);
    let book = store.read_book(&src)?;
    let tab = given("tab").and_then(Value::as_str).map(str::trim).or(old.map(|(_, o)| o.tab.as_str())).filter(|t| !t.is_empty());
    let sh = sheet_index(book, tab, false, None)?;
    let tab = book.sheet_names().get(sh as usize).cloned().unwrap_or_default();
    let hr = match (given("header_row").and_then(Value::as_i64), old) {
        (Some(r), _) if r >= 1 => r,
        (_, Some((_, o))) => i64::from(o.header_row),
        _ => header_row(book, sh),
    };
    let x = match (given("x").and_then(Value::as_str), old) {
        (Some(x), _) => col_name(column(book, sh, hr, x)?),
        (None, Some((_, o))) => o.x.clone(),
        (None, None) => return msg("Give `x`: the column along the bottom, by header or letter."),
    };
    let wanted = names(given("series"));
    let series = if wanted.is_empty() {
        match old {
            Some((_, o)) => o.series.clone(),
            None => return msg("Give `series`: the column(s) with the numbers, by header or letter."),
        }
    } else {
        let mut out = Vec::new();
        for n in wanted.iter().take(chart::MAX_SERIES) {
            out.push(col_name(column(book, sh, hr, n)?));
        }
        out
    };
    let last_row = old.and_then(|(_, o)| o.last_row);
    let source = ChartSource { sheet: src.aid.clone(), tab, header_row: u32::try_from(hr).unwrap_or(1), x, series, last_row };
    let mut base = Map::new();
    let old_spec = old.map(|(spec, _)| spec);
    for k in ["type", "title", "unit", "stacked"] {
        if let Some(v) = given(k).or_else(|| old_spec.and_then(|o| o.get(k)).filter(|v| !v.is_null())) {
            base.insert(k.into(), v.clone());
        }
    }
    base.entry("type").or_insert(json!("bar"));
    let spec = spec(book, &source, &base)?;
    let title = s(&spec, "title").unwrap_or("Chart").to_owned();
    let rows = spec.get("data").and_then(Value::as_array).map_or(0, Vec::len);
    let cut = if rows >= chart::MAX_ROWS {
        format!(" Only the first {} rows fit in a chart; group or sum the sheet into a summary table and chart that instead.", chart::MAX_ROWS)
    } else {
        String::new()
    };
    if let Some((id, _)) = &existing {
        let filename =
            crate::store::read_meta(ws, id).and_then(|m| s(&m, "filename").map(str::to_owned)).unwrap_or_else(|| format!("{}.chart.json", slug(&title)));
        let path = store.save_tmp(spec.to_string().as_bytes(), &filename)?;
        let text = format!(
            "Changed the chart \"{title}\" ({id}): {} chart of {rows} rows, x = column {}, series = {}, from {}. Still linked to the sheet.{cut}",
            s(&spec, "type").unwrap_or("bar"),
            source.x,
            source.series.join(", "),
            src.label()
        );
        return Ok(json!({"text": text, "artifact": {"id": id, "title": title, "file": path, "note": "Changed"}}));
    }
    let filename = format!("{}.chart.json", slug(&title));
    let path = store.save_tmp(spec.to_string().as_bytes(), &filename)?;
    let text = format!(
        "Made the chart \"{title}\" from {} ({rows} rows, x = column {}, series = {}). It is linked to the sheet: whenever the sheet changes (your edits or the user's), the chart is drawn again, so don't remake it; to change its type, title or columns, call chart_sheet with `chart` set to its id. It shows in the chat; give the main takeaway, not the numbers.{cut}",
        src.label(),
        source.x,
        source.series.join(", ")
    );
    Ok(json!({"text": text, "artifact": {"title": title, "filename": filename, "file": path, "note": format!("From {}", or_title(&src))}}))
}

fn or_title(src: &Source) -> String {
    s(&src.meta, "title").filter(|t| !t.is_empty()).unwrap_or(&src.aid).to_owned()
}

/// A file name from a title.
fn slug(title: &str) -> String {
    let mut out = String::new();
    for ch in title.chars() {
        if ch.is_alphanumeric() {
            out.extend(ch.to_lowercase());
        } else if !out.ends_with('-') && !out.is_empty() {
            out.push('-');
        }
        if out.len() >= 60 {
            break;
        }
    }
    let out = out.trim_end_matches('-').to_owned();
    if out.is_empty() { "chart".into() } else { out }
}

/// A structural change a save made to the sheet, which linked charts follow (as formulas
/// do): rows or columns inserted (`delta` > 0) or deleted (`delta` < 0) at `at` (1-based) of
/// tab `tab`, a tab renamed or deleted.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Move {
    Rows { tab: String, at: i64, delta: i64 },
    Cols { tab: String, at: i64, delta: i64 },
    Renamed { from: String, to: String },
    Deleted { tab: String },
}

/// Where row or column `n` is after `delta` were inserted (> 0) or deleted (< 0) at `at`:
/// `None` when it was deleted.
fn shift(n: i64, at: i64, delta: i64) -> Option<i64> {
    if delta >= 0 {
        Some(if n >= at { n + delta } else { n })
    } else if n >= at && n < at - delta {
        None
    } else if n >= at - delta {
        Some(n + delta)
    } else {
        Some(n)
    }
}

/// `src` after `moves` (its tab `first` when it names none, as charts made before tabs were
/// stored): `Err` with why when what it charts is gone.
fn adjust(src: &mut ChartSource, first: &str, moves: &[Move]) -> Result<(), String> {
    if src.tab.is_empty() {
        src.tab = first.to_owned();
    }
    for m in moves {
        match m {
            Move::Renamed { from, to } if *from == src.tab => src.tab.clone_from(to),
            Move::Deleted { tab } if *tab == src.tab => return Err(format!("its sheet {tab:?} was deleted")),
            Move::Rows { tab, at, delta } if *tab == src.tab => {
                let r = shift(i64::from(src.header_row), *at, *delta).ok_or("its header row was deleted")?;
                src.header_row = u32::try_from(r).map_err(|_| "its header row moved off the sheet")?;
                // A selection's bottom: deleted, it becomes the row above what went.
                if let Some(last) = src.last_row {
                    let l = shift(i64::from(last), *at, *delta).unwrap_or(*at - 1);
                    if l <= r {
                        return Err("all its rows were deleted".into());
                    }
                    src.last_row = u32::try_from(l).ok();
                }
            }
            Move::Cols { tab, at, delta } if *tab == src.tab => {
                let x = shift(col_num(&src.x), *at, *delta).ok_or_else(|| format!("its column {} (along the bottom) was deleted", src.x))?;
                src.x = col_name(x);
                src.series = src.series.iter().filter_map(|c| shift(col_num(c), *at, *delta)).map(col_name).collect();
                if src.series.is_empty() {
                    return Err("all its number columns were deleted".into());
                }
            }
            _ => {}
        }
    }
    Ok(())
}

/// A chart linked to a sheet, drawn again from it.
pub struct Redrawn {
    /// The chart artifact.
    pub id: String,
    pub title: String,
    pub filename: String,
    /// Its new spec (to save as its next version).
    pub spec: Value,
}

/// The charts of chat `chat` drawn from sheet artifact `sheet`, each drawn again from `book`
/// (just saved, after `moves`) where it changed; and a line for each that can't follow the
/// sheet any more (left with its last numbers).
pub fn refresh(ws: &str, chat: Option<&str>, sheet: &str, book: &mut Book, moves: &[Move]) -> (Vec<Redrawn>, Vec<String>) {
    let mut out = Vec::new();
    let mut lost = Vec::new();
    let first = book.sheet_names().first().cloned().unwrap_or_default();
    for m in metas(ws) {
        if s(&m, "content_type") != Some(CHART_TYPE) || (chat.is_some() && s(&m, "chat") != chat) {
            continue;
        }
        let Some(path) = crate::store::latest(ws, &m).1 else { continue };
        let Ok(text) = std::fs::read_to_string(&path) else { continue };
        let Ok(old) = serde_json::from_str::<Value>(&text) else { continue };
        if out.len() >= MAX_LINKED {
            if old.get("source").and_then(|v| v.get("sheet")).and_then(Value::as_str) == Some(sheet) {
                lost.push(format!("More than {MAX_LINKED} charts are drawn from this sheet; the others weren't redrawn this time."));
                break;
            }
            continue;
        }
        let Some(mut src) = old.get("source").and_then(|v| serde_json::from_value::<ChartSource>(v.clone()).ok()).filter(|src| src.sheet == sheet) else {
            continue;
        };
        let title = s(&old, "title").unwrap_or("chart").to_owned();
        let id = s(&m, "id").unwrap_or_default().to_owned();
        if let Err(why) = adjust(&mut src, &first, moves) {
            lost.push(format!("The linked chart \"{title}\" ({id}) can't follow the sheet any more: {why}. It keeps its last numbers; make it again with chart_sheet if it's still wanted."));
            continue;
        }
        let base: Map<String, Value> = ["type", "title", "unit", "stacked"].iter().filter_map(|k| old.get(*k).map(|v| ((*k).to_owned(), v.clone()))).collect();
        let new = match spec(book, &src, &base) {
            Ok(n) => n,
            Err(e) => {
                lost.push(format!("The linked chart \"{title}\" ({id}) couldn't be drawn again: {} It keeps its last numbers.", e.text()));
                continue;
            }
        };
        if new.get("data") == old.get("data") && new.get("series") == old.get("series") && new.get("source") == old.get("source") {
            continue;
        }
        out.push(Redrawn {
            id,
            title: s(&new, "title").unwrap_or("chart").to_owned(),
            filename: s(&m, "filename").unwrap_or("chart.chart.json").to_owned(),
            spec: new,
        });
    }
    (out, lost)
}

/// Write each redrawn chart's spec to a file for byob to save: (chart, file).
pub fn files(store: &mut Store, redrawn: Vec<Redrawn>) -> Vec<(Redrawn, String)> {
    redrawn.into_iter().filter_map(|r| store.save_tmp(r.spec.to_string().as_bytes(), &r.filename).ok().map(|f| (r, f))).collect()
}

/// The charts of chat `chat` drawn from sheet artifact `sheet`: (id, latest spec), by id,
/// at most [`MAX_LINKED`].
pub fn linked(ws: &str, chat: Option<&str>, sheet: &str) -> Vec<(String, Value)> {
    let mut out = Vec::new();
    for m in metas(ws) {
        if out.len() >= MAX_LINKED {
            break;
        }
        if s(&m, "content_type") != Some(CHART_TYPE) || (chat.is_some() && s(&m, "chat") != chat) {
            continue;
        }
        let Some(path) = crate::store::latest(ws, &m).1 else { continue };
        let Some(spec) = std::fs::read_to_string(&path).ok().and_then(|t| serde_json::from_str::<Value>(&t).ok()) else { continue };
        if spec.get("source").and_then(|v| v.get("sheet")).and_then(Value::as_str) == Some(sheet) {
            out.push((s(&m, "id").unwrap_or_default().to_owned(), spec));
        }
    }
    out
}

/// A new chart of the selected cells (0-based, inclusive): the top row names the columns,
/// the first column goes along the bottom and the others are series. (title, file name,
/// spec).
pub fn from_selection(book: &mut Book, sheet: &str, sh: u32, (r1, c1, r2, c2): (i64, i64, i64, i64)) -> R<(String, String, Value)> {
    if c2 <= c1 {
        return msg("Select at least two columns: the first goes along the bottom, the others are charted.");
    }
    if r2 <= r1 {
        return msg("Select the header row and the rows under it.");
    }
    let tab = book.sheet_names().get(sh as usize).cloned().unwrap_or_default();
    let series: Vec<String> = (c1 + 2..=c2 + 1).take(chart::MAX_SERIES).map(col_name).collect();
    let source = ChartSource {
        sheet: sheet.to_owned(),
        tab,
        header_row: u32::try_from(r1 + 1).unwrap_or(1),
        x: col_name(c1 + 1),
        series,
        last_row: u32::try_from(r2 + 1).ok(),
    };
    let x = header(book, sh, r1 + 1, c1 + 1);
    let labels: Vec<String> = (c1 + 2..=c2 + 1).take(3).map(|c| header(book, sh, r1 + 1, c)).collect();
    let title = format!("{} by {x}", labels.join(", "));
    let mut base = Map::new();
    base.insert("type".into(), json!("bar"));
    base.insert("title".into(), json!(title));
    let spec = spec(book, &source, &base)?;
    let title = s(&spec, "title").unwrap_or("Chart").to_owned();
    Ok((title.clone(), format!("{}.chart.json", slug(&title)), spec))
}

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

    fn src(x: &str, series: &[&str]) -> ChartSource {
        ChartSource {
            sheet: "s".into(),
            tab: "Data".into(),
            header_row: 3,
            x: x.into(),
            series: series.iter().map(|s| (*s).to_owned()).collect(),
            last_row: None,
        }
    }
    fn cols(tab: &str, at: i64, delta: i64) -> Move {
        Move::Cols { tab: tab.into(), at, delta }
    }

    #[test]
    fn linked_columns_and_rows_follow_inserts_and_deletes() {
        let mut s = src("A", &["C", "D"]);
        adjust(&mut s, "Data", &[cols("Data", 2, 2)]).unwrap();
        assert_eq!((s.x.as_str(), s.series.clone()), ("A", vec!["E".to_owned(), "F".to_owned()]), "inserted before C");
        adjust(&mut s, "Data", &[cols("Data", 5, -1)]).unwrap();
        assert_eq!(s.series, ["E"], "a deleted series column goes, the next moves up");
        adjust(&mut s, "Data", &[cols("Other", 1, 5)]).unwrap();
        assert_eq!(s.series, ["E"], "another tab's columns don't matter");
        assert!(adjust(&mut s, "Data", &[cols("Data", 5, -1)]).unwrap_err().contains("number columns"));
        let mut s = src("B", &["C"]);
        assert!(adjust(&mut s, "Data", &[cols("Data", 1, -2)]).unwrap_err().contains("along the bottom"));
        // Rows: the header row moves with rows above it, and can be deleted.
        let mut s = src("A", &["B"]);
        adjust(&mut s, "Data", &[Move::Rows { tab: "Data".into(), at: 1, delta: 2 }, Move::Rows { tab: "Data".into(), at: 9, delta: -3 }]).unwrap();
        assert_eq!(s.header_row, 5);
        assert!(adjust(&mut s, "Data", &[Move::Rows { tab: "Data".into(), at: 4, delta: -2 }]).unwrap_err().contains("header row"));
        // Tabs: renamed follows; deleted is gone; a chart with no tab is on the first.
        let mut s = src("A", &["B"]);
        adjust(&mut s, "Data", &[Move::Renamed { from: "Data".into(), to: "2025".into() }, cols("2025", 1, 1)]).unwrap();
        assert_eq!((s.tab.as_str(), s.x.as_str()), ("2025", "B"));
        assert!(adjust(&mut s, "Data", &[Move::Deleted { tab: "2025".into() }]).is_err());
        let mut s = ChartSource { tab: String::new(), ..src("A", &["B"]) };
        adjust(&mut s, "First", &[]).unwrap();
        assert_eq!(s.tab, "First");
    }
}