diurn-cli 0.1.0

Command-line tool for ISO 10383 Market Identifier Codes and Diurn market calendars
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
//! Turning library values into output.
//!
//! `diurn-mic` deliberately ships no `Display` shaped like a table row and no
//! notion of a terminal (SPEC 1.6). This module is where that boundary is paid
//! for: every column choice, every JSON shape, lives here.

use std::io::Write;

use anyhow::Result;
use diurn_mic::{Issue, MicDiff, MicRecord, MicRegistry, Severity};
use serde::Serialize;

use crate::cli::Format;
use crate::output::Table;

/// A record as the CLI emits it: exactly `MicRecord`'s own serialization, plus
/// the one thing a record cannot know about itself.
///
/// Deliberately a thin wrapper rather than a hand-listed copy of every field.
/// `diurn-api` will serialize the same `MicRecord`, and two hand-maintained
/// field lists would drift — a consumer would then see `GET /v1/venues/XNYS`
/// and `diurn mic get XNYS --format json` disagree for no good reason. Adding a
/// field to the library now flows through here automatically.
#[derive(Serialize)]
pub struct RecordView<'a> {
    #[serde(flatten)]
    record: &'a MicRecord,

    /// Whether this record's change is published but not yet in force.
    ///
    /// Not on `MicRecord`: it is a comparison against the registry's
    /// publication date, which a single record has no access to.
    pending: bool,
}

impl<'a> RecordView<'a> {
    pub fn new(record: &'a MicRecord, registry: &MicRegistry) -> Self {
        Self {
            record,
            pending: registry.is_pending(record.mic),
        }
    }
}

/// CSV columns, in order.
///
/// Written out rather than derived, for two reasons. `serde(flatten)`
/// serializes as a map and the `csv` crate refuses those outright. And column
/// order is part of a CSV's contract in a way it is not for JSON — a consumer
/// indexing by position deserves it to be deliberate rather than an accident of
/// struct layout.
///
/// Keep these in step with the JSON field names above.
const CSV_COLUMNS: [&str; 18] = [
    "mic",
    "operating_mic",
    "kind",
    "market_name",
    "legal_entity_name",
    "lei",
    "category",
    "acronym",
    "country",
    "city",
    "website",
    "status",
    "created",
    "last_updated",
    "last_validated",
    "expires",
    "comments",
    "pending",
];

fn csv_row(v: &RecordView<'_>) -> Vec<String> {
    let r = v.record;
    let date = |d: Option<jiff::civil::Date>| d.map(|d| d.to_string()).unwrap_or_default();
    vec![
        r.mic.to_string(),
        r.operating_mic.to_string(),
        r.kind.to_string(),
        r.market_name.to_string(),
        r.legal_entity_name.as_deref().unwrap_or("").to_string(),
        r.lei.map(|l| l.to_string()).unwrap_or_default(),
        r.category.to_string(),
        r.acronym.as_deref().unwrap_or("").to_string(),
        r.country.map(|c| c.to_string()).unwrap_or_default(),
        r.city.as_deref().unwrap_or("").to_string(),
        r.website.as_deref().unwrap_or("").to_string(),
        r.status.to_string(),
        date(r.created),
        date(r.last_updated),
        date(r.last_validated),
        date(r.expires),
        r.comments.as_deref().unwrap_or("").to_string(),
        v.pending.to_string(),
    ]
}

fn write_csv_records(w: &mut impl Write, views: &[RecordView<'_>]) -> Result<()> {
    let mut wtr = csv::Writer::from_writer(w);
    wtr.write_record(CSV_COLUMNS)?;
    for v in views {
        wtr.write_record(csv_row(v))?;
    }
    wtr.flush()?;
    Ok(())
}

/// Trim a cell so one 102-character market name does not blow out the table.
/// Machine formats never truncate.
fn clip(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        return s.to_string();
    }
    let kept: String = s.chars().take(max.saturating_sub(1)).collect();
    format!("{kept}")
}

fn write_json<T: Serialize>(w: &mut impl Write, value: &T) -> Result<()> {
    serde_json::to_writer_pretty(&mut *w, value)?;
    writeln!(w)?;
    Ok(())
}

fn write_jsonl<T: Serialize>(w: &mut impl Write, values: &[T]) -> Result<()> {
    for v in values {
        serde_json::to_writer(&mut *w, v)?;
        writeln!(w)?;
    }
    Ok(())
}

/// Render a set of records.
pub fn records(
    w: &mut impl Write,
    format: Format,
    registry: &MicRegistry,
    records: &[&MicRecord],
) -> Result<()> {
    let views: Vec<RecordView> = records
        .iter()
        .map(|r| RecordView::new(r, registry))
        .collect();

    match format {
        Format::Json => write_json(w, &views)?,
        Format::Jsonl => write_jsonl(w, &views)?,
        Format::Csv => write_csv_records(w, &views)?,
        Format::Table => {
            let mut t = Table::new(&["MIC", "OPER", "TYPE", "CC", "STATUS", "CATEGORY", "NAME"]);
            for r in records {
                t.push(vec![
                    r.mic.to_string(),
                    r.operating_mic.to_string(),
                    r.kind.to_string(),
                    r.country
                        .map(|c| c.to_string())
                        .unwrap_or_else(|| "--".into()),
                    // A pending record is visibly distinct from a settled one.
                    if registry.is_pending(r.mic) {
                        format!("{}*", r.status)
                    } else {
                        r.status.to_string()
                    },
                    r.category.to_string(),
                    clip(&r.market_name, 52),
                ]);
            }
            if t.is_empty() {
                writeln!(w, "no matching records")?;
            } else {
                t.write(w)?;
            }
        }
    }
    Ok(())
}

/// Render one record in full. Table format goes long rather than wide, since a
/// single record has no columns to align against.
pub fn record_detail(
    w: &mut impl Write,
    format: Format,
    registry: &MicRegistry,
    record: &MicRecord,
) -> Result<()> {
    let v = RecordView::new(record, registry);
    match format {
        Format::Json => write_json(w, &v)?,
        Format::Jsonl => write_jsonl(w, std::slice::from_ref(&v))?,
        Format::Csv => write_csv_records(w, std::slice::from_ref(&v))?,
        Format::Table => {
            let r = record;
            let mut field = |k: &str, val: &str| -> Result<()> {
                if !val.is_empty() {
                    writeln!(w, "{k:<18}{val}")?;
                }
                Ok(())
            };
            let date = |d: Option<jiff::civil::Date>| d.map(|d| d.to_string()).unwrap_or_default();

            field("MIC", r.mic.as_str())?;
            field("Name", &r.market_name)?;
            field("Operating MIC", r.operating_mic.as_str())?;
            // A human reading a table wants the expansion; the machine formats
            // carry only the code, which is what the library and the API speak.
            field("Type", &format!("{} ({})", r.kind, r.kind.description()))?;
            field(
                "Category",
                &match r.category.description() {
                    Some(n) => format!("{} ({n})", r.category),
                    None => format!("{} (unrecognised)", r.category),
                },
            )?;
            field("Status", r.status.as_str())?;
            if registry.is_pending(r.mic) {
                field(
                    "Pending",
                    &format!(
                        "yes — takes effect {}",
                        r.last_updated
                            .map(|d| d.to_string())
                            .unwrap_or_else(|| "?".into())
                    ),
                )?;
            }
            field(
                "Country",
                &r.country.map(|c| c.to_string()).unwrap_or_default(),
            )?;
            field("City", r.city.as_deref().unwrap_or(""))?;
            field("Legal entity", r.legal_entity_name.as_deref().unwrap_or(""))?;
            field("LEI", &r.lei.map(|l| l.to_string()).unwrap_or_default())?;
            field("Acronym", r.acronym.as_deref().unwrap_or(""))?;
            field("Website", r.website.as_deref().unwrap_or(""))?;
            field("Created", &date(r.created))?;
            field("Last updated", &date(r.last_updated))?;
            field("Last validated", &date(r.last_validated))?;
            field("Expires", &date(r.expires))?;
            field("Comments", r.comments.as_deref().unwrap_or(""))?;
        }
    }
    Ok(())
}

#[derive(Serialize)]
struct IssueView {
    severity: String,
    kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    mic: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    line: Option<u64>,
    message: String,
}

fn severity_str(s: Severity) -> &'static str {
    match s {
        Severity::Info => "info",
        Severity::Warning => "warning",
        Severity::Error => "error",
    }
}

/// The variant name without its payload, so issues can be counted by kind.
fn kind_name(i: &Issue) -> String {
    let dbg = format!("{:?}", i.kind);
    dbg.split([' ', '(', '{'])
        .next()
        .unwrap_or(&dbg)
        .to_string()
}

fn issue_views(issues: &[Issue]) -> Vec<IssueView> {
    issues
        .iter()
        .map(|i| IssueView {
            severity: severity_str(i.severity).to_string(),
            kind: kind_name(i),
            mic: i.mic.map(|m| m.as_str().to_string()),
            line: i.line,
            message: i.kind.to_string(),
        })
        .collect()
}

/// Every issue, individually.
pub fn issues(w: &mut impl Write, format: Format, issues: &[Issue]) -> Result<()> {
    let views = issue_views(issues);
    match format {
        Format::Json => write_json(w, &views)?,
        Format::Jsonl => write_jsonl(w, &views)?,
        Format::Csv => {
            let mut wtr = csv::Writer::from_writer(&mut *w);
            for v in &views {
                wtr.serialize(v)?;
            }
            wtr.flush()?;
        }
        Format::Table => {
            let mut t = Table::new(&["SEVERITY", "LINE", "MIC", "PROBLEM"]);
            for v in &views {
                t.push(vec![
                    v.severity.clone(),
                    v.line.map(|l| l.to_string()).unwrap_or_else(|| "--".into()),
                    v.mic.clone().unwrap_or_else(|| "--".into()),
                    v.message.clone(),
                ]);
            }
            if t.is_empty() {
                writeln!(w, "no issues")?;
            } else {
                t.write(w)?;
            }
        }
    }
    Ok(())
}

#[derive(Serialize)]
struct SummaryView {
    published: String,
    records: usize,
    errors: usize,
    warnings: usize,
    info: usize,
    pending: usize,
    by_kind: Vec<KindCount>,
}

#[derive(Serialize)]
struct KindCount {
    kind: String,
    severity: String,
    count: usize,
}

/// A count per issue kind, which is what `mic load` reports by default.
pub fn summary(
    w: &mut impl Write,
    format: Format,
    registry: &MicRegistry,
    all: &[Issue],
) -> Result<()> {
    // BTreeMap keeps the output stable between runs, which matters when the
    // command ends up in a CI log that somebody diffs.
    let mut counts: std::collections::BTreeMap<String, (String, usize)> = Default::default();
    for i in all {
        let e = counts
            .entry(kind_name(i))
            .or_insert((severity_str(i.severity).to_string(), 0));
        e.1 += 1;
    }

    let view = SummaryView {
        published: registry.published().to_string(),
        records: registry.len(),
        errors: all.iter().filter(|i| i.severity == Severity::Error).count(),
        warnings: all
            .iter()
            .filter(|i| i.severity == Severity::Warning)
            .count(),
        info: all.iter().filter(|i| i.severity == Severity::Info).count(),
        pending: registry.pending().count(),
        by_kind: counts
            .iter()
            .map(|(k, (sev, n))| KindCount {
                kind: k.clone(),
                severity: sev.clone(),
                count: *n,
            })
            .collect(),
    };

    match format {
        Format::Json => write_json(w, &view)?,
        Format::Jsonl => write_jsonl(w, std::slice::from_ref(&view))?,
        Format::Csv => {
            let mut wtr = csv::Writer::from_writer(&mut *w);
            for k in &view.by_kind {
                wtr.serialize(k)?;
            }
            wtr.flush()?;
        }
        Format::Table => {
            writeln!(w, "vintage    {}", view.published)?;
            writeln!(w, "records    {}", view.records)?;
            writeln!(w, "pending    {}", view.pending)?;
            writeln!(
                w,
                "issues     {} error, {} warning, {} info",
                view.errors, view.warnings, view.info
            )?;
            if !view.by_kind.is_empty() {
                writeln!(w)?;
                let mut t = Table::new(&["COUNT", "SEVERITY", "KIND"]);
                for k in &view.by_kind {
                    t.push(vec![
                        k.count.to_string(),
                        k.severity.clone(),
                        k.kind.clone(),
                    ]);
                }
                t.write(w)?;
            }
        }
    }
    Ok(())
}

#[derive(Serialize)]
struct DiffView {
    added: Vec<String>,
    removed: Vec<String>,
    changed: Vec<ChangedView>,
}

#[derive(Serialize)]
struct ChangedView {
    mic: String,
    changes: Vec<FieldChangeView>,
}

#[derive(Serialize)]
struct FieldChangeView {
    field: String,
    old: Option<String>,
    new: Option<String>,
}

pub fn diff(w: &mut impl Write, format: Format, d: &MicDiff) -> Result<()> {
    let view = DiffView {
        added: d.added.iter().map(|m| m.to_string()).collect(),
        removed: d.removed.iter().map(|m| m.to_string()).collect(),
        changed: d
            .changed
            .iter()
            .map(|c| ChangedView {
                mic: c.mic.to_string(),
                changes: c
                    .changes
                    .iter()
                    .map(|f| FieldChangeView {
                        field: f.field.to_string(),
                        old: f.old.as_ref().map(|s| s.to_string()),
                        new: f.new.as_ref().map(|s| s.to_string()),
                    })
                    .collect(),
            })
            .collect(),
    };

    match format {
        Format::Json => write_json(w, &view)?,
        Format::Jsonl => {
            // One line per affected MIC, so a diff streams like everything else.
            #[derive(Serialize)]
            struct Row<'a> {
                change: &'a str,
                mic: &'a str,
                #[serde(skip_serializing_if = "Option::is_none")]
                fields: Option<&'a [FieldChangeView]>,
            }
            let mut rows: Vec<Row> = Vec::new();
            for m in &view.added {
                rows.push(Row {
                    change: "added",
                    mic: m,
                    fields: None,
                });
            }
            for m in &view.removed {
                rows.push(Row {
                    change: "removed",
                    mic: m,
                    fields: None,
                });
            }
            for c in &view.changed {
                rows.push(Row {
                    change: "changed",
                    mic: &c.mic,
                    fields: Some(&c.changes),
                });
            }
            write_jsonl(w, &rows)?;
        }
        Format::Csv => {
            let mut wtr = csv::Writer::from_writer(&mut *w);
            wtr.write_record(["change", "mic", "field", "old", "new"])?;
            for m in &view.added {
                wtr.write_record(["added", m, "", "", ""])?;
            }
            for m in &view.removed {
                wtr.write_record(["removed", m, "", "", ""])?;
            }
            for c in &view.changed {
                for f in &c.changes {
                    wtr.write_record([
                        "changed",
                        &c.mic,
                        &f.field,
                        f.old.as_deref().unwrap_or(""),
                        f.new.as_deref().unwrap_or(""),
                    ])?;
                }
            }
            wtr.flush()?;
        }
        Format::Table => {
            if view.added.is_empty() && view.removed.is_empty() && view.changed.is_empty() {
                writeln!(w, "no differences")?;
                return Ok(());
            }
            writeln!(
                w,
                "{} added, {} removed, {} changed\n",
                view.added.len(),
                view.removed.len(),
                view.changed.len()
            )?;
            let mut t = Table::new(&["CHANGE", "MIC", "FIELD", "OLD", "NEW"]);
            for m in &view.added {
                t.push(vec![
                    "added".into(),
                    m.clone(),
                    String::new(),
                    String::new(),
                    String::new(),
                ]);
            }
            for m in &view.removed {
                t.push(vec![
                    "removed".into(),
                    m.clone(),
                    String::new(),
                    String::new(),
                    String::new(),
                ]);
            }
            for c in &view.changed {
                for f in &c.changes {
                    t.push(vec![
                        "changed".into(),
                        c.mic.clone(),
                        f.field.clone(),
                        clip(f.old.as_deref().unwrap_or("--"), 30),
                        clip(f.new.as_deref().unwrap_or("--"), 30),
                    ]);
                }
            }
            t.write(w)?;
        }
    }
    Ok(())
}

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

    #[test]
    fn clip_leaves_short_strings_alone() {
        assert_eq!(clip("NYSE", 10), "NYSE");
        assert_eq!(clip("NYSE", 4), "NYSE");
    }

    #[test]
    fn clip_marks_truncation() {
        assert_eq!(clip("NEW YORK STOCK EXCHANGE", 10), "NEW YORK …");
        assert_eq!(clip("NEW YORK STOCK EXCHANGE", 10).chars().count(), 10);
    }

    /// Non-ASCII names must clip on character boundaries, not bytes.
    #[test]
    fn clip_handles_non_ascii() {
        let s = "ČESKOSLOVENSKÁ OBCHODNÍ BANKA";
        assert_eq!(clip(s, 8).chars().count(), 8);
        assert!(clip(s, 8).starts_with('Č'));
    }
}