bob 1.0.0

Fast, robust, powerful, user-friendly pkgsrc package builder
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
pub mod build;
pub mod db;
pub mod diff;
pub mod history;
pub mod list;
pub mod log;
pub mod prune;
pub mod publish;
pub mod rebuild;
pub mod sandbox;
pub mod simulate;
pub mod status;
pub mod util;

use bob::PackageState;
use strum::VariantArray;

/**
 * Sort key for one column of one row.  `Num`/`OptStr` order missing
 * values last regardless of direction, since "no data" is rarely the
 * first thing the reader wants to see.  `Idx` carries a precomputed
 * ordinal for enum-like columns where domain ordering beats
 * alphabetical (e.g. status outcome).
 *
 * Each variant carries a natural direction: `Num` defaults to
 * descending (biggest first), everything else to ascending.  The
 * user's `-` prefix inverts that default rather than meaning
 * "descending" outright -- so `-S disk_usage` puts the largest
 * package on top and `-S pkgname` is A-Z, with `-` flipping either.
 */
pub enum SortKey {
    Str(String),
    OptStr(Option<String>),
    Num(Option<u64>),
    Idx(usize),
}

impl SortKey {
    fn natural_desc(&self) -> bool {
        matches!(self, SortKey::Num(_))
    }
}

fn cmp_keys(a: &SortKey, b: &SortKey, invert: bool) -> std::cmp::Ordering {
    use std::cmp::Ordering::*;
    let ord = match (a, b) {
        (SortKey::Str(x), SortKey::Str(y)) => x.cmp(y),
        (SortKey::OptStr(Some(x)), SortKey::OptStr(Some(y))) => x.cmp(y),
        (SortKey::OptStr(Some(_)), SortKey::OptStr(None)) => return Less,
        (SortKey::OptStr(None), SortKey::OptStr(Some(_))) => return Greater,
        (SortKey::OptStr(None), SortKey::OptStr(None)) => Equal,
        (SortKey::Num(Some(x)), SortKey::Num(Some(y))) => x.cmp(y),
        (SortKey::Num(Some(_)), SortKey::Num(None)) => return Less,
        (SortKey::Num(None), SortKey::Num(Some(_))) => return Greater,
        (SortKey::Num(None), SortKey::Num(None)) => Equal,
        (SortKey::Idx(x), SortKey::Idx(y)) => x.cmp(y),
        _ => Equal,
    };
    let desc = a.natural_desc() ^ invert;
    if desc { ord.reverse() } else { ord }
}

/**
 * Parse comma-separated sort specs of the form `col` or `-col`,
 * mapping each name through `lookup`.  The boolean is the user's
 * `-` prefix flag, interpreted as "invert this column's natural
 * direction" by [`sort_indexed_rows`].
 */
pub fn parse_sort_specs<C>(
    values: &[String],
    lookup: impl Fn(&str) -> Option<C>,
    valid_names: &[&str],
) -> anyhow::Result<Vec<(C, bool)>> {
    values
        .iter()
        .map(|s| {
            let (invert, name) = match s.strip_prefix('-') {
                Some(rest) => (true, rest),
                None => (false, s.as_str()),
            };
            lookup(name).map(|c| (c, invert)).ok_or_else(|| {
                anyhow::anyhow!(
                    "Unknown sort column '{}'. Valid columns: {}",
                    name,
                    valid_names.join(", ")
                )
            })
        })
        .collect()
}

/**
 * Stable in-place multi-key sort of `(keys, payload)` pairs.  Each
 * `inverts[i]` flag flips the natural direction of the corresponding
 * key position.  Its length must match each row's keys vec.
 */
pub fn sort_indexed_rows<T>(rows: &mut [(Vec<SortKey>, T)], inverts: &[bool]) {
    rows.sort_by(|(a, _), (b, _)| {
        for (i, &invert) in inverts.iter().enumerate() {
            let ord = cmp_keys(&a[i], &b[i], invert);
            if ord != std::cmp::Ordering::Equal {
                return ord;
            }
        }
        std::cmp::Ordering::Equal
    });
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, clap::ValueEnum)]
pub enum OutputFormat {
    /// Padded columns
    #[default]
    Table,
    /// Comma-separated values
    Csv,
    /// JSON array of objects
    Json,
}

/**
 * Render-time column metadata used by the writer.  Independent of any
 * particular command's column repository (see [`Column`]).
 */
pub struct Col {
    pub key: std::borrow::Cow<'static, str>,
    pub title: std::borrow::Cow<'static, str>,
    pub align: bob::Align,
    pub max_width: Option<usize>,
}

impl Col {
    pub fn new(key: impl Into<std::borrow::Cow<'static, str>>, align: bob::Align) -> Self {
        let key = key.into();
        Self {
            title: key.clone(),
            key,
            align,
            max_width: None,
        }
    }

    pub fn max(mut self, w: usize) -> Self {
        self.max_width = Some(w);
        self
    }
}

/**
 * Repository of every column the CLI can emit, with its display
 * metadata.  Each command picks the subset it supports and provides a
 * source-specific extraction; nothing else varies across commands.
 */
#[allow(dead_code)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Column {
    // Package identity
    Pkgname,
    Pkgpath,
    Pkgbase,
    // diff baselines
    PkgnamePrev,
    OutcomePrev,
    StagePrev,
    // Diff/scheduler
    Breaks,
    // Build outcome
    Outcome,
    Stage,
    // Build sizing/timing
    Duration,
    DiskUsage,
    MakeJobs,
    Wrkobjdir,
    Timestamp,
    BuildId,
    // bob list builds counters
    Packages,
    Succeeded,
    Uptodate,
    Failed,
    Masked,
    // bob status fields
    Status,
    Reason,
    MultiVersion,
    Deps,
    Priority,
    Cpu,
    // bob history per-stage durations / cpu time
    StageDuration(crate::build::Stage),
    StageCpu(crate::build::Stage),
}

impl Column {
    /// Machine identifier used for CSV/JSON field names and -o lookups.
    pub fn key(self) -> std::borrow::Cow<'static, str> {
        match self {
            Self::Pkgname => "pkgname".into(),
            Self::Pkgpath => "pkgpath".into(),
            Self::Pkgbase => "pkgbase".into(),
            Self::PkgnamePrev => "pkgname_prev".into(),
            Self::OutcomePrev => "outcome_prev".into(),
            Self::StagePrev => "stage_prev".into(),
            Self::Breaks => "breaks".into(),
            Self::Outcome => "outcome".into(),
            Self::Stage => "stage".into(),
            Self::Duration => "duration".into(),
            Self::DiskUsage => "disk_usage".into(),
            Self::MakeJobs => "make_jobs".into(),
            Self::Wrkobjdir => "wrkobjdir".into(),
            Self::Timestamp => "timestamp".into(),
            Self::BuildId => "build_id".into(),
            Self::Packages => "packages".into(),
            Self::Succeeded => "succeeded".into(),
            Self::Uptodate => "uptodate".into(),
            Self::Failed => "failed".into(),
            Self::Masked => "masked".into(),
            Self::Status => "status".into(),
            Self::Reason => "reason".into(),
            Self::MultiVersion => "multi_version".into(),
            Self::Deps => "deps".into(),
            Self::Priority => "priority".into(),
            Self::Cpu => "cpu".into(),
            Self::StageDuration(s) => <&str>::from(s).into(),
            Self::StageCpu(s) => format!("cpu:{}", <&str>::from(s)).into(),
        }
    }

    pub fn align(self) -> bob::Align {
        use bob::Align::*;
        match self {
            Self::Breaks
            | Self::Duration
            | Self::DiskUsage
            | Self::MakeJobs
            | Self::Packages
            | Self::Succeeded
            | Self::Uptodate
            | Self::Failed
            | Self::Masked
            | Self::Deps
            | Self::Priority
            | Self::Cpu
            | Self::StageDuration(_)
            | Self::StageCpu(_) => Right,
            _ => Left,
        }
    }

    pub fn desc(self) -> std::borrow::Cow<'static, str> {
        match self {
            Self::Pkgname => "Package name".into(),
            Self::Pkgpath => "Package path in pkgsrc".into(),
            Self::Pkgbase => "Package name without version".into(),
            Self::PkgnamePrev => "Package name from the previous build".into(),
            Self::OutcomePrev => "Outcome from the previous build".into(),
            Self::StagePrev => "Build stage that failed in the previous build".into(),
            Self::Breaks => "Number of packages broken by this failure".into(),
            Self::Outcome => "Build outcome".into(),
            Self::Stage => "Build stage that failed".into(),
            Self::Duration => "Wall-clock duration".into(),
            Self::DiskUsage => "WRKDIR size at end of build".into(),
            Self::MakeJobs => "MAKE_JOBS used".into(),
            Self::Wrkobjdir => "WRKOBJDIR type".into(),
            Self::Timestamp => "Build start time".into(),
            Self::BuildId => "Build session identifier".into(),
            Self::Packages => "Total packages in the build".into(),
            Self::Succeeded => "Packages built successfully".into(),
            Self::Uptodate => "Packages already up-to-date".into(),
            Self::Failed => "Packages that failed".into(),
            Self::Masked => "Packages skipped or masked".into(),
            Self::Status => "Current build status".into(),
            Self::Reason => "Status detail or reason".into(),
            Self::MultiVersion => "MULTI_VERSION variables".into(),
            Self::Deps => "Number of dependent packages".into(),
            Self::Priority => "Scheduler priority order".into(),
            Self::Cpu => "Previous build CPU time".into(),
            Self::StageDuration(s) => format!("Wall time for {} stage", <&str>::from(s)).into(),
            Self::StageCpu(s) => format!("CPU time for {} stage", <&str>::from(s)).into(),
        }
    }

    /// Build the render-time `Col` for this column.
    pub fn col(self) -> Col {
        Col::new(self.key(), self.align())
    }

    /// Parse a column name (e.g. from `-o`).
    pub fn parse(s: &str) -> Option<Self> {
        if let Some(stage) = s.strip_prefix("cpu:") {
            return stage.parse().ok().map(Self::StageCpu);
        }
        Some(match s {
            "pkgname" => Self::Pkgname,
            "pkgpath" => Self::Pkgpath,
            "pkgbase" => Self::Pkgbase,
            "pkgname_prev" => Self::PkgnamePrev,
            "outcome_prev" => Self::OutcomePrev,
            "stage_prev" => Self::StagePrev,
            "breaks" => Self::Breaks,
            "outcome" => Self::Outcome,
            "stage" => Self::Stage,
            "duration" => Self::Duration,
            "disk_usage" => Self::DiskUsage,
            "make_jobs" => Self::MakeJobs,
            "wrkobjdir" => Self::Wrkobjdir,
            "timestamp" => Self::Timestamp,
            "build_id" => Self::BuildId,
            "packages" => Self::Packages,
            "succeeded" => Self::Succeeded,
            "uptodate" => Self::Uptodate,
            "failed" => Self::Failed,
            "masked" => Self::Masked,
            "status" => Self::Status,
            "reason" => Self::Reason,
            "multi_version" => Self::MultiVersion,
            "deps" => Self::Deps,
            "priority" => Self::Priority,
            "cpu" => Self::Cpu,
            _ => return s.parse().ok().map(Self::StageDuration),
        })
    }
}

/**
 * Resolve user-supplied column names against a command's supported set.
 * `long` selects the full `supported` list; otherwise `requested` is
 * validated against `supported`, or `defaults` is used when no
 * selection is given.
 */
pub fn select_columns(
    requested: Option<&[String]>,
    all: bool,
    defaults: &[Column],
    supported: &[Column],
) -> anyhow::Result<Vec<Column>> {
    if let Some(names) = requested {
        return names
            .iter()
            .map(|n| {
                Column::parse(n)
                    .filter(|c| supported.contains(c))
                    .ok_or_else(|| {
                        let valid: Vec<String> =
                            supported.iter().map(|c| c.key().into_owned()).collect();
                        anyhow::anyhow!(
                            "Unknown column '{}'. Valid columns: {}",
                            n,
                            valid.join(", ")
                        )
                    })
            })
            .collect();
    }
    if all {
        return Ok(supported.to_vec());
    }
    Ok(defaults.to_vec())
}

/**
 * Instantiate the render-time `Col` metadata for the chosen columns.
 */
pub fn col_defs(chosen: &[Column]) -> Vec<Col> {
    chosen.iter().map(|c| c.col()).collect()
}

/**
 * `Columns:` help block listing each supported column with its
 * description, and the default selection.  Suitable as a clap
 * `long_help` value.
 */
pub fn cols_help(supported: &[Column], defaults: &[Column]) -> String {
    use std::fmt::Write as _;
    let entries: Vec<(String, std::borrow::Cow<'static, str>)> = supported
        .iter()
        .map(|c| (c.key().into_owned(), c.desc()))
        .collect();
    let width = entries.iter().map(|(k, _)| k.len()).max().unwrap_or(0);
    let mut out = String::from("Columns:\n");
    for (k, d) in &entries {
        let _ = writeln!(out, "  {:<width$}  {}", k, d);
    }
    let default_names: Vec<String> = defaults.iter().map(|c| c.key().into_owned()).collect();
    let _ = write!(out, "\nDefault columns: {}", default_names.join(","));
    out
}

/**
 * Project a per-command data type into typed `Cell`s, one column at a
 * time.  Each command implements this for its row type so that the
 * shared [`Writer`] can build rows without per-command boilerplate.
 */
pub trait ColumnSource {
    type Ctx;
    fn cell(&self, col: Column, ctx: &Self::Ctx) -> Cell;
}

/**
 * Output options threaded through the formatter.  `raw` disables
 * human-friendly cell rendering.
 */
#[derive(Clone, Copy, Debug, Default)]
pub struct OutputOptions {
    pub format: OutputFormat,
    pub no_header: bool,
    pub raw: bool,
}

pub use bob::fmt::Cell;

enum OutputEvent {
    Row {
        prefix: Option<char>,
        cells: Vec<Cell>,
    },
    Message(String),
}

/**
 * Buffered output writer with optional row prefixes and free-form
 * messages.  Generic over the underlying writer.
 */
pub struct Formatter<W: std::io::Write> {
    writer: W,
    cols: Vec<Col>,
    opts: OutputOptions,
    events: Vec<OutputEvent>,
}

impl<W: std::io::Write> Formatter<W> {
    pub fn new(writer: W, cols: Vec<Col>, opts: OutputOptions) -> Self {
        Self {
            writer,
            cols,
            opts,
            events: Vec::new(),
        }
    }

    pub fn row<I: IntoIterator<Item = Cell>>(&mut self, cells: I) {
        self.row_with_prefix(None, cells)
    }

    pub fn row_with_prefix<I: IntoIterator<Item = Cell>>(
        &mut self,
        prefix: Option<char>,
        cells: I,
    ) {
        self.events.push(OutputEvent::Row {
            prefix,
            cells: cells.into_iter().collect(),
        });
    }

    pub fn message(&mut self, msg: &str) {
        self.events.push(OutputEvent::Message(msg.to_string()));
    }

    pub fn finish(self) -> anyhow::Result<()> {
        use std::io::Write;
        let Self {
            writer,
            cols,
            opts,
            events,
        } = self;
        let mut w = std::io::BufWriter::new(writer);
        match opts.format {
            OutputFormat::Table => write_table(&cols, &opts, &events, &mut w)?,
            OutputFormat::Csv => write_csv(&cols, &opts, &events, &mut w)?,
            OutputFormat::Json => write_json(&cols, &events, &mut w)?,
        }
        w.flush()?;
        Ok(())
    }
}

/**
 * Column-aware wrapper around `Formatter`.  Holds the chosen column
 * list and projects [`ColumnSource`] values into rows.
 */
pub struct Writer<W: std::io::Write> {
    formatter: Formatter<W>,
    chosen: Vec<Column>,
}

impl<W: std::io::Write> Writer<W> {
    pub fn new(writer: W, chosen: Vec<Column>, opts: OutputOptions) -> Self {
        let cols = col_defs(&chosen);
        Self {
            formatter: Formatter::new(writer, cols, opts),
            chosen,
        }
    }

    pub fn message(&mut self, msg: &str) {
        self.formatter.message(msg)
    }

    pub fn write<S: ColumnSource>(&mut self, prefix: Option<char>, source: &S, ctx: &S::Ctx) {
        let cells: Vec<Cell> = self.chosen.iter().map(|&c| source.cell(c, ctx)).collect();
        self.formatter.row_with_prefix(prefix, cells)
    }

    pub fn finish(self) -> anyhow::Result<()> {
        self.formatter.finish()
    }
}

impl Writer<std::io::StdoutLock<'static>> {
    pub fn stdout(chosen: Vec<Column>, opts: OutputOptions) -> Self {
        Writer::new(std::io::stdout().lock(), chosen, opts)
    }
}

/**
 * True if any cause in the error chain is a `BrokenPipe`.
 */
pub fn is_broken_pipe(e: &anyhow::Error) -> bool {
    e.chain().any(|c| {
        if let Some(io) = c.downcast_ref::<std::io::Error>() {
            io.kind() == std::io::ErrorKind::BrokenPipe
        } else if let Some(ce) = c.downcast_ref::<csv::Error>() {
            matches!(
                ce.kind(),
                csv::ErrorKind::Io(io) if io.kind() == std::io::ErrorKind::BrokenPipe
            )
        } else {
            false
        }
    })
}

fn write_table<W: std::io::Write>(
    cols: &[Col],
    opts: &OutputOptions,
    events: &[OutputEvent],
    w: &mut W,
) -> anyhow::Result<()> {
    let raw = opts.raw;
    let mut rendered: Vec<RenderedEvent> = Vec::with_capacity(events.len());
    for ev in events {
        match ev {
            OutputEvent::Row { prefix, cells } => {
                let mut strs = Vec::with_capacity(cells.len());
                for c in cells {
                    strs.push(c.render_table(raw)?);
                }
                rendered.push(RenderedEvent::Row {
                    prefix: *prefix,
                    cells: strs,
                });
            }
            OutputEvent::Message(m) => rendered.push(RenderedEvent::Message(m.clone())),
        }
    }

    let widths: Vec<usize> = cols
        .iter()
        .enumerate()
        .map(|(i, col)| {
            let header_len = col.title.len();
            let max_data = rendered
                .iter()
                .filter_map(|e| match e {
                    RenderedEvent::Row { cells, .. } => cells.get(i).map(String::len),
                    _ => None,
                })
                .max()
                .unwrap_or(0);
            header_len
                .max(max_data)
                .min(col.max_width.unwrap_or(usize::MAX))
        })
        .collect();

    let any_prefix = rendered.iter().any(|e| {
        matches!(
            e,
            RenderedEvent::Row {
                prefix: Some(_),
                ..
            }
        )
    });
    let pad_prefix = if any_prefix { " " } else { "" };

    let mut header_emitted = false;
    for ev in &rendered {
        match ev {
            RenderedEvent::Row { prefix, cells } => {
                if !header_emitted {
                    if !opts.no_header {
                        let header: Vec<String> = cols
                            .iter()
                            .zip(&widths)
                            .map(|(col, &width)| pad(&col.title.to_uppercase(), col.align, width))
                            .collect();
                        writeln!(w, "{}{}", pad_prefix, header.join("  ").trim_end())?;
                    }
                    header_emitted = true;
                }
                let values: Vec<String> = cols
                    .iter()
                    .zip(&widths)
                    .enumerate()
                    .map(|(i, (col, &width))| {
                        let s = cells.get(i).map(String::as_str).unwrap_or("");
                        pad(s, col.align, width)
                    })
                    .collect();
                let line = values.join("  ");
                let line = line.trim_end();
                match prefix {
                    Some(c) => writeln!(w, "{}{}", c, line)?,
                    None => writeln!(w, "{}{}", pad_prefix, line)?,
                }
            }
            RenderedEvent::Message(m) => writeln!(w, "{}", m)?,
        }
    }
    Ok(())
}

fn write_csv<W: std::io::Write>(
    cols: &[Col],
    opts: &OutputOptions,
    events: &[OutputEvent],
    w: &mut W,
) -> anyhow::Result<()> {
    let mut wtr = csv::Writer::from_writer(w);
    if !opts.no_header {
        let header: Vec<&str> = cols.iter().map(|c| c.key.as_ref()).collect();
        wtr.write_record(&header)?;
    }
    for ev in events {
        if let OutputEvent::Row { cells, .. } = ev {
            let mut record = Vec::with_capacity(cols.len());
            for i in 0..cols.len() {
                record.push(cells.get(i).map(Cell::render_csv).unwrap_or_default());
            }
            wtr.write_record(&record)?;
        }
    }
    wtr.flush()?;
    Ok(())
}

fn write_json<W: std::io::Write>(
    cols: &[Col],
    events: &[OutputEvent],
    w: &mut W,
) -> anyhow::Result<()> {
    let mut array: Vec<serde_json::Map<String, serde_json::Value>> = Vec::new();
    for ev in events {
        if let OutputEvent::Row { cells, .. } = ev {
            let mut obj = serde_json::Map::new();
            for (i, col) in cols.iter().enumerate() {
                let v = match cells.get(i) {
                    Some(c) => c.render_json()?,
                    None => serde_json::Value::Null,
                };
                obj.insert(col.key.to_string(), v);
            }
            array.push(obj);
        }
    }
    let s = serde_json::to_string_pretty(&array)?;
    writeln!(w, "{}", s)?;
    Ok(())
}

enum RenderedEvent {
    Row {
        prefix: Option<char>,
        cells: Vec<String>,
    },
    Message(String),
}

fn pad(s: &str, align: bob::Align, width: usize) -> String {
    match align {
        bob::Align::Right => format!("{:>width$}", s, width = width),
        bob::Align::Left => format!("{:<width$}", s, width = width),
    }
}

struct FilterAlias {
    name: &'static str,
    desc: &'static str,
    matches: fn(PackageState) -> bool,
}

const FILTER_ALIASES: &[FilterAlias] = &[
    FilterAlias {
        name: "ok",
        desc: "Any successful outcome (freshly built or up-to-date)",
        matches: PackageState::is_success,
    },
    FilterAlias {
        name: "skipped",
        desc: "Any pre-skipped or pre-failed package",
        matches: PackageState::is_skipped,
    },
    FilterAlias {
        name: "blocked",
        desc: "Any package blocked by another",
        matches: PackageState::is_blocked,
    },
    FilterAlias {
        name: "masked",
        desc: "Any skipped or indirectly blocked package",
        matches: PackageState::is_masked,
    },
];

/**
 * Iterate over `(alias_name, description)` for every status filter alias
 * recognised by [`parse_status_filter`].
 */
pub fn status_filter_aliases() -> impl Iterator<Item = (&'static str, &'static str)> {
    FILTER_ALIASES.iter().map(|a| (a.name, a.desc))
}

/**
 * Parse a status filter string into the states it matches.
 *
 * A filter string is either the name of a single state, for example
 * `failed` or `indirect-failed`, which matches just that state (see
 * [`PackageState::as_str`](bob::PackageState::as_str) for the full set of
 * names); or an alias for a group of states, listed by
 * [`status_filter_aliases`].
 */
pub fn parse_status_filter(s: &str) -> Result<Vec<PackageState>, String> {
    if let Ok(k) = s.parse::<PackageState>() {
        return Ok(vec![k]);
    }
    for alias in FILTER_ALIASES {
        if alias.name == s {
            return Ok(PackageState::VARIANTS
                .iter()
                .copied()
                .filter(|k| (alias.matches)(*k))
                .collect());
        }
    }
    Err(format!("unknown status '{s}'"))
}