tuitab 0.5.0

Terminal tabular data explorer — CSV/JSON/YAML/TOML/Parquet/Excel/SQLite viewer with filtering, sorting, pivot tables, and charts
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
//! Projection of a [`Doc`] subtree into a table.
//!
//! A [`View`] names *which* node is being shown (`anchor`) and *how* (`mode`).  Building
//! it produces a throwaway [`DataFrame`] for rendering plus the bookkeeping needed to map
//! a table cell back to the [`NodePath`] it came from — that mapping is what makes editing
//! write into the tree rather than into the table.

use crate::data::dataframe::DataFrame;
use crate::data::doc::{Node, NodePath, Seg};
use color_eyre::{eyre::eyre, Result};
use indexmap::IndexSet;
use polars::prelude::*;

/// How a subtree is laid out as rows and columns.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ViewMode {
    /// Array of objects → one row per element, columns are the union of keys.
    Records,
    /// Object → one row per key; columns are `key` / `value` / `type`.
    ///
    /// This is deliberately different from VisiData, which shows an object as a single
    /// very wide row.  A 40-key config file is readable as 40 rows and unreadable as
    /// one row of 40 columns.  The cost: the `value` column is heterogeneous by
    /// construction, so typing, sorting and aggregating it are meaningless.
    KeyValue,
    /// Array of scalars → a single column.
    Scalars,
}

/// What a column of the projection means, and therefore where an edit to it goes.
#[derive(Debug, Clone, PartialEq)]
pub enum ColRole {
    /// Cell node is at `path` relative to the row node.  Normally one segment; an
    /// expanded column carries the whole chain (`meta`, `ok` for `meta.ok`).
    Field(Vec<Seg>),
    /// Cell node is the row node itself.
    Row,
    /// The object key naming this row; editing it renames the key.
    Key,
    /// Read-only type name of the row node.
    Type,
}

#[derive(Debug, Clone)]
pub struct View {
    pub anchor: NodePath,
    pub mode: ViewMode,
    /// Paths (relative to the row node) shown as their children instead of as one
    /// container cell — the state behind `(` / `)`.  Kept on the view, not on the
    /// columns, so it survives every reprojection.
    pub expanded: Vec<Vec<Seg>>,
}

/// A built projection: the table plus the mapping back into the tree.
pub struct Projection {
    pub df: DataFrame,
    /// Absolute path of each row's node, indexed by physical row.
    pub row_paths: Vec<NodePath>,
    /// Role of each column, indexed by column.
    pub col_roles: Vec<ColRole>,
}

impl View {
    /// Pick the layout that fits `node`, matching the DWIM rules of the loaders it
    /// replaces: arrays of objects become records, objects become key/value rows,
    /// everything else becomes a single column.
    pub fn auto_mode(node: &Node) -> ViewMode {
        match node {
            Node::Arr(items) => {
                if items.iter().any(|n| matches!(n, Node::Obj(_))) {
                    ViewMode::Records
                } else {
                    ViewMode::Scalars
                }
            }
            Node::Obj(_) => ViewMode::KeyValue,
            _ => ViewMode::Scalars,
        }
    }

    pub fn auto(anchor: NodePath, node: &Node) -> View {
        View {
            mode: View::auto_mode(node),
            anchor,
            expanded: Vec::new(),
        }
    }

    /// Modes worth offering for `node`, in the order the `m` key cycles them.
    pub fn modes_for(node: &Node) -> Vec<ViewMode> {
        match node {
            Node::Arr(_) => vec![ViewMode::Records, ViewMode::Scalars, ViewMode::KeyValue],
            Node::Obj(_) => vec![ViewMode::KeyValue, ViewMode::Records],
            _ => vec![ViewMode::Scalars],
        }
    }

    pub fn project(&self, root: &Node) -> Result<Projection> {
        let node = root
            .get(&self.anchor)
            .ok_or_else(|| eyre!("no node at the anchor path"))?;
        match self.mode {
            ViewMode::Records => self.project_records(node),
            ViewMode::KeyValue => self.project_keyvalue(node),
            ViewMode::Scalars => self.project_scalars(node),
        }
    }

    fn project_records(&self, node: &Node) -> Result<Projection> {
        // Rows: array elements, or the single object itself when a lone record is
        // viewed as a table of one.
        let (rows, row_paths): (Vec<&Node>, Vec<NodePath>) = match node {
            Node::Arr(items) => (
                items.iter().collect(),
                (0..items.len())
                    .map(|i| child_path(&self.anchor, Seg::Idx(i)))
                    .collect(),
            ),
            other => (vec![other], vec![self.anchor.clone()]),
        };

        // Columns are discovered incrementally, in order of first appearance, exactly
        // as VisiData's JsonSheet does — rows are legitimately sparse and heterogeneous.
        let mut keys: IndexSet<String> = IndexSet::new();
        for r in &rows {
            if let Node::Obj(m) = r {
                for k in m.keys() {
                    keys.insert(k.clone());
                }
            }
        }
        // Rows that are not objects (scalars, nested arrays) still need somewhere to go.
        // That extra column is identified by position, never by name: an object key
        // literally called `value` must stay an ordinary field, or editing it would
        // replace the whole row instead of one key.
        let needs_bare_col = rows.iter().any(|r| !matches!(r, Node::Obj(_))) || keys.is_empty();

        let mut names: Vec<String> = Vec::with_capacity(keys.len() + 1);
        let mut col_roles: Vec<ColRole> = Vec::with_capacity(keys.len() + 1);
        let mut cells: Vec<Vec<Option<&Node>>> = Vec::with_capacity(keys.len() + 1);

        if needs_bare_col {
            names.push(unique_name(DEFAULT_COLNAME, &keys));
            col_roles.push(ColRole::Row);
            cells.push(
                rows.iter()
                    .map(|r| {
                        if matches!(r, Node::Obj(_)) {
                            None
                        } else {
                            Some(*r)
                        }
                    })
                    .collect(),
            );
        }
        for key in &keys {
            let path = vec![Seg::Key(key.clone())];
            // An expanded column is replaced in place by its children, so the expansion
            // reads as a wider version of the same column rather than a new group at
            // the end.  Children of children are expanded too, recursively.
            self.push_column(&path, &rows, &mut names, &mut col_roles, &mut cells);
        }

        Ok(Projection {
            df: build_df(&names, &cells)?,
            row_paths,
            col_roles,
        })
    }

    /// Emit one column for `path`, or — if it is expanded — the columns of its children.
    fn push_column<'a>(
        &self,
        path: &[Seg],
        rows: &[&'a Node],
        names: &mut Vec<String>,
        col_roles: &mut Vec<ColRole>,
        cells: &mut Vec<Vec<Option<&'a Node>>>,
    ) {
        let values: Vec<Option<&Node>> = rows.iter().map(|r| r.get(path)).collect();

        if self.is_expanded(path) {
            let children = child_segments(&values);
            if !children.is_empty() {
                for seg in children {
                    let mut child = path.to_vec();
                    child.push(seg);
                    self.push_column(&child, rows, names, col_roles, cells);
                }
                return;
            }
            // Nothing to expand into any more (the values stopped being containers);
            // fall through and show the column as it is rather than dropping it.
        }

        names.push(crate::data::doc::path_to_string(path));
        col_roles.push(ColRole::Field(path.to_vec()));
        cells.push(values);
    }

    fn is_expanded(&self, path: &[Seg]) -> bool {
        self.expanded.iter().any(|p| p == path)
    }

    /// Mark `path` expanded.  Returns false if it was already.
    pub fn expand(&mut self, path: Vec<Seg>) -> bool {
        if self.is_expanded(&path) {
            return false;
        }
        self.expanded.push(path);
        true
    }

    /// Collapse `path` and everything expanded beneath it.  Returns false if nothing
    /// was expanded there.
    pub fn contract(&mut self, path: &[Seg]) -> bool {
        let before = self.expanded.len();
        self.expanded
            .retain(|p| !(p.len() >= path.len() && &p[..path.len()] == path));
        self.expanded.len() != before
    }

    /// Collapse one level: the deepest expansion that is a prefix of `path`, so `)` on
    /// `a.b.c` folds `a.b` back into a single column and leaves `a` expanded.
    pub fn contract_one(&mut self, path: &[Seg]) -> bool {
        let Some(target) = self
            .expanded
            .iter()
            .filter(|p| p.len() <= path.len() && path[..p.len()] == p[..])
            .max_by_key(|p| p.len())
            .cloned()
        else {
            return false;
        };
        self.contract(&target)
    }

    fn project_keyvalue(&self, node: &Node) -> Result<Projection> {
        let Node::Obj(map) = node else {
            // A non-object anchored in key/value mode degrades to a single row rather
            // than failing — the user may have switched modes on the wrong node.
            return self.project_scalars(node);
        };
        let row_paths: Vec<NodePath> = map
            .keys()
            .map(|k| child_path(&self.anchor, Seg::Key(k.clone())))
            .collect();

        let key_col: Vec<String> = map.keys().cloned().collect();
        let value_col: Vec<Option<&Node>> = map.values().map(Some).collect();
        let type_col: Vec<String> = map.values().map(|v| v.type_name().to_string()).collect();

        let df = polars::prelude::DataFrame::new(
            map.len(),
            vec![
                Column::new("key".into(), &key_col),
                node_series("value", &value_col),
                Column::new("type".into(), &type_col),
            ],
        )?;

        Ok(Projection {
            df: wrap(df)?,
            row_paths,
            col_roles: vec![ColRole::Key, ColRole::Row, ColRole::Type],
        })
    }

    fn project_scalars(&self, node: &Node) -> Result<Projection> {
        let (items, row_paths): (Vec<&Node>, Vec<NodePath>) = match node {
            Node::Arr(v) => (
                v.iter().collect(),
                (0..v.len())
                    .map(|i| child_path(&self.anchor, Seg::Idx(i)))
                    .collect(),
            ),
            other => (vec![other], vec![self.anchor.clone()]),
        };
        let name = self
            .anchor
            .last()
            .map(|s| match s {
                Seg::Key(k) => k.clone(),
                Seg::Idx(i) => format!("[{}]", i),
            })
            .unwrap_or_else(|| DEFAULT_COLNAME.to_string());
        let cells: Vec<Vec<Option<&Node>>> = vec![items.into_iter().map(Some).collect()];
        Ok(Projection {
            df: build_df(&[name], &cells)?,
            row_paths,
            col_roles: vec![ColRole::Row],
        })
    }
}

/// Column name used for rows that are not objects, mirroring VisiData's
/// `options.default_colname`.
pub const DEFAULT_COLNAME: &str = "value";

/// Segments to expand a column into, derived from every value in it.
///
/// Objects contribute the union of their keys in order of first appearance; arrays
/// contribute indices up to the longest one.  Mixed columns are allowed — a row that is
/// not a container simply has no value under the child columns.  Returns empty when no
/// value is a container, which is what makes `(` a no-op on a scalar column.
fn child_segments(values: &[Option<&Node>]) -> Vec<Seg> {
    let mut keys: IndexSet<String> = IndexSet::new();
    let mut max_len = 0usize;
    for v in values.iter().flatten() {
        match v {
            Node::Obj(m) => keys.extend(m.keys().cloned()),
            Node::Arr(a) => max_len = max_len.max(a.len()),
            _ => {}
        }
    }
    // Objects win when a column mixes both: named columns beat positional ones.
    if !keys.is_empty() {
        return keys.into_iter().map(Seg::Key).collect();
    }
    (0..max_len).map(Seg::Idx).collect()
}

/// Pick a column name for the bare-value column that no real key already uses.
fn unique_name(base: &str, taken: &IndexSet<String>) -> String {
    if !taken.contains(base) {
        return base.to_string();
    }
    (1..)
        .map(|n| format!("{}_{}", base, n))
        .find(|c| !taken.contains(c))
        .unwrap()
}

fn child_path(base: &[Seg], seg: Seg) -> NodePath {
    let mut p = base.to_vec();
    p.push(seg);
    p
}

/// Resolve the tree path of one cell.  `None` for columns that do not address a node
/// (the read-only `type` column).
pub fn cell_path(
    row_paths: &[NodePath],
    col_roles: &[ColRole],
    row: usize,
    col: usize,
) -> Option<NodePath> {
    let base = row_paths.get(row)?;
    match col_roles.get(col)? {
        ColRole::Row | ColRole::Key => Some(base.clone()),
        ColRole::Field(path) => Some(base.iter().chain(path).cloned().collect()),
        ColRole::Type => None,
    }
}

// ── DataFrame construction ───────────────────────────────────────────────────

fn build_df(names: &[String], cells: &[Vec<Option<&Node>>]) -> Result<DataFrame> {
    let height = cells.first().map(|c| c.len()).unwrap_or(0);
    let cols: Vec<Column> = names
        .iter()
        .zip(cells)
        .map(|(name, col)| node_series(name, col))
        .collect();
    wrap(polars::prelude::DataFrame::new(height, cols)?)
}

/// Build one Polars column from a column of tree nodes.
///
/// Homogeneous scalar columns get a real numeric/boolean dtype so that sorting,
/// filtering and aggregation behave; anything mixed (and anything containing a
/// container) falls back to the compact text rendering.
fn node_series(name: &str, col: &[Option<&Node>]) -> Column {
    let mut any_int = false;
    let mut any_float = false;
    let mut any_bool = false;
    let mut any_other = false;
    for n in col.iter().flatten() {
        match n {
            Node::Int(_) => any_int = true,
            Node::Float(_) => any_float = true,
            Node::Bool(_) => any_bool = true,
            Node::Null => {}
            _ => any_other = true,
        }
    }

    if !any_other {
        if any_bool && !any_int && !any_float {
            let v: Vec<Option<bool>> = col
                .iter()
                .map(|n| match n {
                    Some(Node::Bool(b)) => Some(*b),
                    _ => None,
                })
                .collect();
            return Column::new(name.into(), v);
        }
        if any_int && !any_float && !any_bool {
            let v: Vec<Option<i64>> = col
                .iter()
                .map(|n| match n {
                    Some(Node::Int(i)) => Some(*i),
                    _ => None,
                })
                .collect();
            return Column::new(name.into(), v);
        }
        if (any_int || any_float) && !any_bool {
            let v: Vec<Option<f64>> = col
                .iter()
                .map(|n| match n {
                    Some(Node::Int(i)) => Some(*i as f64),
                    Some(Node::Float(f)) => Some(*f),
                    _ => None,
                })
                .collect();
            return Column::new(name.into(), v);
        }
    }

    let v: Vec<Option<String>> = col
        .iter()
        .map(|n| match n {
            None | Some(Node::Null) => None,
            Some(node) => Some(node.render_compact(RENDER_LIMIT)),
        })
        .collect();
    Column::new(name.into(), v)
}

/// Upper bound on how much of a container is rendered into a cell.  The table view
/// clips to the real column width; this only keeps a huge subtree from being
/// materialised into a string in the first place.
const RENDER_LIMIT: usize = 512;

fn wrap(pdf: polars::prelude::DataFrame) -> Result<DataFrame> {
    crate::data::io::wrap_polars_df(pdf)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data::doc::{Doc, Format};
    use crate::types::ColumnType;

    fn root_of(src: &str, f: Format) -> Node {
        Doc::from_str(src, f).unwrap().root
    }

    #[test]
    fn array_of_objects_becomes_records_with_union_of_keys() {
        let root = root_of(r#"[{"a":1,"b":2},{"a":3,"c":4}]"#, Format::Json);
        let view = View::auto(vec![], &root);
        assert_eq!(view.mode, ViewMode::Records);
        let p = view.project(&root).unwrap();
        let names: Vec<String> = p.df.columns.iter().map(|c| c.name.clone()).collect();
        assert_eq!(
            names,
            vec!["a", "b", "c"],
            "keys in order of first sighting"
        );
        assert_eq!(p.df.df.height(), 2);
        // second row has no `b`
        assert_eq!(p.df.get_physical(1, 1), "");
        assert_eq!(
            cell_path(&p.row_paths, &p.col_roles, 1, 2),
            Some(vec![Seg::Idx(1), Seg::Key("c".into())])
        );
    }

    #[test]
    fn toml_document_opens_as_key_value_rows() {
        let root = root_of(
            "name = \"x\"\nport = 8080\n\n[db]\nhost = \"h\"\n",
            Format::Toml,
        );
        let view = View::auto(vec![], &root);
        assert_eq!(view.mode, ViewMode::KeyValue);
        let p = view.project(&root).unwrap();
        assert_eq!(p.df.df.height(), 3, "one row per top-level key");
        let names: Vec<String> = p.df.columns.iter().map(|c| c.name.clone()).collect();
        assert_eq!(names, vec!["key", "value", "type"]);
        // the nested table renders compactly rather than as an empty cell
        assert_eq!(p.df.get_physical(2, 1), "{1} host=h");
        assert_eq!(p.df.get_physical(2, 2), "dict");
        assert_eq!(
            cell_path(&p.row_paths, &p.col_roles, 2, 1),
            Some(vec![Seg::Key("db".into())])
        );
        assert_eq!(
            cell_path(&p.row_paths, &p.col_roles, 2, 2),
            None,
            "type column addresses no node"
        );
    }

    #[test]
    fn homogeneous_numeric_column_keeps_a_numeric_dtype() {
        let root = root_of(r#"[{"n":1},{"n":2},{"n":3}]"#, Format::Json);
        let p = View::auto(vec![], &root).project(&root).unwrap();
        assert_eq!(p.df.df.dtypes()[0], DataType::Int64);
        assert_eq!(p.df.columns[0].col_type, ColumnType::Integer);
    }

    #[test]
    fn mixed_column_falls_back_to_text() {
        let root = root_of(r#"[{"n":1},{"n":"two"}]"#, Format::Json);
        let p = View::auto(vec![], &root).project(&root).unwrap();
        assert_eq!(p.df.df.dtypes()[0], DataType::String);
        assert_eq!(p.df.get_physical(1, 0), "two");
    }

    #[test]
    fn array_of_scalars_becomes_one_column() {
        let root = root_of("[1, 2, 3]", Format::Json);
        let view = View::auto(vec![], &root);
        assert_eq!(view.mode, ViewMode::Scalars);
        let p = view.project(&root).unwrap();
        assert_eq!(p.df.df.width(), 1);
        assert_eq!(p.df.df.height(), 3);
        assert_eq!(
            cell_path(&p.row_paths, &p.col_roles, 2, 0),
            Some(vec![Seg::Idx(2)])
        );
    }

    #[test]
    fn records_with_scalar_rows_get_a_default_column() {
        let root = root_of(r#"[{"a":1}, 7]"#, Format::Json);
        let p = View {
            anchor: vec![],
            mode: ViewMode::Records,
            expanded: Vec::new(),
        }
        .project(&root)
        .unwrap();
        let names: Vec<String> = p.df.columns.iter().map(|c| c.name.clone()).collect();
        assert_eq!(names, vec![DEFAULT_COLNAME, "a"]);
        assert_eq!(p.df.get_physical(1, 0), "7");
        assert_eq!(
            cell_path(&p.row_paths, &p.col_roles, 1, 0),
            Some(vec![Seg::Idx(1)])
        );
    }

    #[test]
    fn anchoring_into_a_subtree_projects_only_that_subtree() {
        let root = root_of(r#"{"servers":[{"host":"a"},{"host":"b"}]}"#, Format::Json);
        let anchor = vec![Seg::Key("servers".into())];
        let node = root.get(&anchor).unwrap();
        let p = View::auto(anchor.clone(), node).project(&root).unwrap();
        assert_eq!(p.df.df.height(), 2);
        assert_eq!(
            cell_path(&p.row_paths, &p.col_roles, 1, 0),
            Some(vec![
                Seg::Key("servers".into()),
                Seg::Idx(1),
                Seg::Key("host".into())
            ])
        );
    }

    #[test]
    fn expanding_a_column_replaces_it_with_its_children_in_place() {
        let root = root_of(
            r#"[{"id":1,"meta":{"ok":true,"n":2}},{"id":2,"meta":{"ok":false}}]"#,
            Format::Json,
        );
        let mut view = View::auto(vec![], &root);
        assert!(view.expand(vec![Seg::Key("meta".into())]));
        let p = view.project(&root).unwrap();

        let names: Vec<String> = p.df.columns.iter().map(|c| c.name.clone()).collect();
        assert_eq!(
            names,
            vec!["id", "meta.ok", "meta.n"],
            "children sit where the parent was"
        );
        assert_eq!(p.df.get_physical(0, 2), "2");
        assert_eq!(
            p.df.get_physical(1, 2),
            "",
            "row without the key is empty, not an error"
        );
        assert_eq!(
            cell_path(&p.row_paths, &p.col_roles, 0, 1),
            Some(vec![
                Seg::Idx(0),
                Seg::Key("meta".into()),
                Seg::Key("ok".into())
            ]),
            "an expanded cell still addresses its real node, so it stays editable"
        );
    }

    #[test]
    fn expanding_a_list_column_uses_the_longest_row() {
        let root = root_of(r#"[{"t":["a","b","c"]},{"t":["x"]}]"#, Format::Json);
        let mut view = View::auto(vec![], &root);
        view.expand(vec![Seg::Key("t".into())]);
        let p = view.project(&root).unwrap();
        let names: Vec<String> = p.df.columns.iter().map(|c| c.name.clone()).collect();
        assert_eq!(names, vec!["t[0]", "t[1]", "t[2]"]);
        assert_eq!(
            p.df.get_physical(1, 1),
            "",
            "short row has no second element"
        );
    }

    #[test]
    fn expansion_nests_and_contracts_one_level_at_a_time() {
        let root = root_of(r#"[{"a":{"b":{"c":1}}}]"#, Format::Json);
        let mut view = View::auto(vec![], &root);
        view.expand(vec![Seg::Key("a".into())]);
        view.expand(vec![Seg::Key("a".into()), Seg::Key("b".into())]);
        let names = |v: &View| -> Vec<String> {
            v.project(&root)
                .unwrap()
                .df
                .columns
                .iter()
                .map(|c| c.name.clone())
                .collect()
        };
        assert_eq!(names(&view), vec!["a.b.c"]);

        // `)` on a.b.c folds a.b, leaving a expanded
        assert!(view.contract_one(&[
            Seg::Key("a".into()),
            Seg::Key("b".into()),
            Seg::Key("c".into())
        ]));
        assert_eq!(names(&view), vec!["a.b"]);

        assert!(view.contract_one(&[Seg::Key("a".into()), Seg::Key("b".into())]));
        assert_eq!(names(&view), vec!["a"]);

        assert!(
            !view.contract_one(&[Seg::Key("a".into())]),
            "nothing left to fold"
        );
    }

    #[test]
    fn contracting_a_parent_drops_deeper_expansions_too() {
        let root = root_of(r#"[{"a":{"b":{"c":1}}}]"#, Format::Json);
        let mut view = View::auto(vec![], &root);
        view.expand(vec![Seg::Key("a".into())]);
        view.expand(vec![Seg::Key("a".into()), Seg::Key("b".into())]);
        assert!(view.contract(&[Seg::Key("a".into())]));
        assert!(view.expanded.is_empty(), "{:?}", view.expanded);
    }

    #[test]
    fn expanding_a_column_of_scalars_leaves_it_alone() {
        let root = root_of(r#"[{"n":1},{"n":2}]"#, Format::Json);
        let mut view = View::auto(vec![], &root);
        view.expand(vec![Seg::Key("n".into())]);
        let p = view.project(&root).unwrap();
        let names: Vec<String> = p.df.columns.iter().map(|c| c.name.clone()).collect();
        assert_eq!(names, vec!["n"], "a scalar column has no children to show");
    }
}