rs-rich-ext 0.0.21

Extensions for the `rich` Rust port: diagnostics, structured data, clap and tracing integration, diffs, workflow renderables and a plugin registry
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
//! A schema's evolution over versions (#347), on a
//! [`Timeline`](crate::chart::Timeline).

use rich::{Console, ConsoleOptions, Renderable, Segment, Text, Tree};
use serde_json::Value;

use super::{ChangeKind, STYLES, Schema, SchemaDiff, json, theme_style};
use crate::chart::{Charset, Span, Timeline};

/// The most field rows drawn; past this the rest are counted on a last
/// line.
pub const MAX_ROWS: usize = 200;

/// One version: a JSON Schema or a model schema.
#[derive(Clone, Debug)]
enum Source {
    Json(Value),
    Model(Schema),
}

#[derive(Clone, Debug)]
struct Version {
    label: String,
    at: Option<f64>,
    source: Source,
}

impl Version {
    /// The model schema, for the field rows.
    fn model(&self) -> Schema {
        match &self.source {
            Source::Json(value) => json::to_model(value),
            Source::Model(schema) => schema.clone(),
        }
    }
}

/// A series of schema versions on a [`Timeline`]: a row per field (a
/// table's columns as `table.column`) spanning the versions it is in, and a
/// milestone per version with what changed in it. A span is drawn in the
/// added, changed or breaking style in the version its field was added or
/// changed, and the [details](SchemaTimeline::details) under the timeline
/// list every change, so neither the timeline nor the list reads colour
/// alone.
///
/// Consecutive versions are compared with [`SchemaDiff`]: two JSON Schemas
/// as JSON Schema, anything else through the model.
///
/// ```
/// use rich::Console;
/// use rich_ext::schema::{sql, SchemaTimeline};
///
/// let ddl = |columns: &str| sql::parse(&format!("CREATE TABLE t ({columns});")).unwrap().schema;
/// let timeline = SchemaTimeline::new()
///     .push("v1", ddl("id INT"))
///     .push("v2", ddl("id INT, name TEXT"))
///     .push("v3", ddl("id BIGINT, name TEXT"))
///     .details(false)
///     .charset(rich_ext::chart::Charset::Ascii);
/// let console = Console::builder().width(60).color_system(None).build();
/// assert_eq!(
///     console.render_to_string(&timeline),
///     concat!(
///         "t.id   ################================#################    \n",
///         "t.name                 ################=================    \n",
///         "       * v1            * v2: +1        * v3: ~1, 1 breaking \n",
///         "       +-------+-------+-------+-------+-------+-------+----\n",
///         "       0      0.5      1      1.5      2      2.5      3    ",
///     )
/// );
/// ```
#[derive(Clone, Debug)]
#[must_use = "builder methods return a new value; it does nothing until it is rendered or used"]
pub struct SchemaTimeline {
    versions: Vec<Version>,
    details: bool,
    charset: Charset,
}

impl Default for SchemaTimeline {
    fn default() -> Self {
        Self::new()
    }
}

impl SchemaTimeline {
    /// No versions yet; add them in order with [`push`](Self::push).
    pub fn new() -> Self {
        SchemaTimeline {
            versions: Vec::new(),
            details: true,
            charset: Charset::Auto,
        }
    }

    /// Add a model schema as the next version.
    pub fn push(mut self, label: impl Into<String>, schema: Schema) -> Self {
        self.versions.push(Version {
            label: label.into(),
            at: None,
            source: Source::Model(schema),
        });
        self
    }

    /// Add a JSON Schema as the next version.
    pub fn push_json(mut self, label: impl Into<String>, schema: Value) -> Self {
        self.versions.push(Version {
            label: label.into(),
            at: None,
            source: Source::Json(schema),
        });
        self
    }

    /// Place the last version added at `at` on the scale (a date as days,
    /// say). By default each version is at its index.
    pub fn at(mut self, at: f64) -> Self {
        if let Some(last) = self.versions.last_mut() {
            last.at = Some(at);
        }
        self
    }

    /// List every change under the timeline (default on).
    pub fn details(mut self, details: bool) -> Self {
        self.details = details;
        self
    }

    /// Glyphs to draw the timeline with.
    pub fn charset(mut self, charset: Charset) -> Self {
        self.charset = charset;
        self
    }

    /// The version labels, in order.
    pub fn labels(&self) -> Vec<&str> {
        self.versions.iter().map(|v| v.label.as_str()).collect()
    }

    /// What changed into each version after the first: its label and the
    /// diff from the one before.
    pub fn changes(&self) -> Vec<(&str, SchemaDiff)> {
        self.versions
            .windows(2)
            .map(|pair| {
                let diff = match (&pair[0].source, &pair[1].source) {
                    (Source::Json(a), Source::Json(b)) => SchemaDiff::new(a, b),
                    _ => SchemaDiff::models(&pair[0].model(), &pair[1].model()),
                };
                let diff = diff.names(pair[0].label.clone(), pair[1].label.clone());
                (pair[1].label.as_str(), diff)
            })
            .collect()
    }

    fn position(&self, index: usize) -> f64 {
        self.versions[index]
            .at
            .filter(|at| at.is_finite())
            .unwrap_or(index as f64)
    }

    /// The [`Timeline`] drawn, its styles resolved through `console`'s
    /// theme: at most [`MAX_ROWS`] rows (rendering counts the rest on a
    /// line under it).
    pub fn timeline(&self, console: &Console) -> Timeline {
        let mut timeline = Timeline::new()
            .durations(false)
            .compress(false)
            .charset(self.charset);
        if self.versions.is_empty() {
            return timeline;
        }
        let spec = |key: &str| -> String {
            if console.theme().get(key).is_some() {
                key.to_string()
            } else {
                STYLES
                    .iter()
                    .find(|(name, _)| *name == key)
                    .map_or_else(String::new, |(_, spec)| spec.to_string())
            }
        };
        let models: Vec<Schema> = self.versions.iter().map(Version::model).collect();
        let changes = self.changes();
        let (rows, _) = rows(&models);
        let n = self.versions.len();
        let step = if n > 1 {
            ((self.position(n - 1) - self.position(0)) / (n - 1) as f64).max(f64::EPSILON)
        } else {
            1.0
        };
        let present: Vec<std::collections::HashSet<String>> = models
            .iter()
            .map(|model| row_names(model).into_iter().collect())
            .collect();
        for row in &rows {
            for (index, names) in present.iter().enumerate() {
                if !names.contains(row) {
                    continue;
                }
                let start = self.position(index);
                let end = if index + 1 < n {
                    self.position(index + 1)
                } else {
                    start + step
                };
                let mut span = Span::new(row.clone(), start, end);
                let key = index
                    .checked_sub(1)
                    .and_then(|i| row_change(row, &changes[i].1));
                if let Some(key) = key {
                    span = span.style(spec(key));
                } else {
                    span = span.style("dim");
                }
                timeline = timeline.push(span);
            }
        }
        for (index, version) in self.versions.iter().enumerate() {
            let label = match index.checked_sub(1) {
                None => version.label.clone(),
                Some(i) => format!("{}: {}", version.label, counts(&changes[i].1)),
            };
            timeline = timeline.milestone(label, self.position(index));
        }
        timeline
    }

    /// `… N more fields` under the timeline, when there are more rows than
    /// it draws.
    fn hidden_note(&self, console: &Console) -> Option<Text> {
        let models: Vec<Schema> = self.versions.iter().map(Version::model).collect();
        let (_, hidden) = rows(&models);
        (hidden > 0).then(|| {
            Text::styled(
                format!("… {hidden} more fields"),
                theme_style(console, "schema.description"),
            )
        })
    }

    /// The change list drawn under the timeline.
    fn listing(&self, console: &Console) -> Vec<Tree> {
        let mut trees = Vec::new();
        for (_, diff) in self.changes() {
            let mut tree = Tree::new(Text::styled(
                diff.summary(),
                theme_style(console, "schema.name"),
            ));
            for change in diff.changes() {
                let mut line = Text::new("");
                let style = theme_style(console, change.kind.style_key());
                line.append(change.kind.marker(), Some(style.clone().into()));
                line.append(" ", None);
                line.append(
                    &change.path,
                    Some(theme_style(console, "schema.name").into()),
                );
                line.append("  ", None);
                line.append(&change.detail, Some(style.into()));
                if change.breaking {
                    line.append("  ", None);
                    line.append(
                        "breaking",
                        Some(theme_style(console, "schema.breaking").into()),
                    );
                }
                tree.add(line);
            }
            trees.push(tree);
        }
        trees
    }
}

/// The rows drawn, in the order fields first appear, and how many more
/// there are past [`MAX_ROWS`].
fn rows(models: &[Schema]) -> (Vec<String>, usize) {
    let mut rows: Vec<String> = Vec::new();
    let mut listed: std::collections::HashSet<String> = std::collections::HashSet::new();
    for model in models {
        for row in row_names(model) {
            if listed.insert(row.clone()) {
                rows.push(row);
            }
        }
    }
    let hidden = rows.len().saturating_sub(MAX_ROWS);
    rows.truncate(MAX_ROWS);
    (rows, hidden)
}

/// The rows a schema has: its fields, then each table's as `table.column`.
fn row_names(schema: &Schema) -> Vec<String> {
    let mut rows: Vec<String> = schema
        .fields()
        .iter()
        .filter(|f| f.kind() == super::FieldKind::Field)
        .map(|f| f.name().to_string())
        .collect();
    for table in schema.tables() {
        let name = table.name().unwrap_or_default();
        rows.extend(
            table
                .fields()
                .iter()
                .map(|f| format!("{name}.{}", f.name())),
        );
    }
    rows
}

/// The style a row's span takes in a version: the strongest of its
/// changes there, if it changed.
fn row_change(row: &str, diff: &SchemaDiff) -> Option<&'static str> {
    let mut found: Option<&'static str> = None;
    for change in diff.changes() {
        let path = change.path.as_str();
        let under = path == row
            || path
                .strip_prefix(row)
                .is_some_and(|rest| rest.starts_with('.') || rest.starts_with('['));
        if !under {
            continue;
        }
        if change.breaking {
            return Some("schema.breaking");
        }
        let key = if path == row && change.kind == ChangeKind::Added {
            "schema.added"
        } else {
            "schema.changed"
        };
        if found != Some("schema.added") {
            found = Some(key);
        }
    }
    found
}

/// `+2 -1 ~3, 1 breaking`, or `no changes`.
fn counts(diff: &SchemaDiff) -> String {
    let count = |kind: ChangeKind| diff.changes().iter().filter(|c| c.kind == kind).count();
    let mut parts = Vec::new();
    for (kind, marker) in [
        (ChangeKind::Added, "+"),
        (ChangeKind::Removed, "-"),
        (ChangeKind::Changed, "~"),
    ] {
        let n = count(kind);
        if n > 0 {
            parts.push(format!("{marker}{n}"));
        }
    }
    if parts.is_empty() {
        return "no changes".into();
    }
    let mut text = parts.join(" ");
    if diff.breaking() > 0 {
        text.push_str(&format!(", {} breaking", diff.breaking()));
    }
    text
}

impl Renderable for SchemaTimeline {
    fn rich_render(&self, console: &Console, options: &ConsoleOptions) -> Vec<Segment> {
        let mut segments = self.timeline(console).rich_render(console, options);
        if let Some(note) = self.hidden_note(console) {
            if !segments.last().is_some_and(|s| s.text.ends_with('\n')) {
                segments.push(Segment::line());
            }
            segments.extend(note.rich_render(console, options));
        }
        if self.details {
            for tree in self.listing(console) {
                if !segments.last().is_some_and(|s| s.text.ends_with('\n')) {
                    segments.push(Segment::line());
                }
                segments.extend(tree.rich_render(console, options));
            }
        }
        segments
    }

    fn measure(&self, console: &Console, options: &ConsoleOptions) -> rich::measure::Measurement {
        let mut measurement = self.timeline(console).measure(console, options);
        if let Some(note) = self.hidden_note(console) {
            let m = note.measure(console, options);
            measurement = rich::measure::Measurement::new(
                measurement.minimum.max(m.minimum),
                measurement.maximum.max(m.maximum),
            );
        }
        if self.details {
            for tree in self.listing(console) {
                let m = tree.measure(console, options);
                measurement = rich::measure::Measurement::new(
                    measurement.minimum.max(m.minimum),
                    measurement.maximum.max(m.maximum),
                );
            }
        }
        measurement
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::schema::{DataType, Field};

    /// Each row was looked up among all those before it: 200,000 columns
    /// took minutes.
    #[test]
    fn many_rows_are_gathered_in_linear_time() {
        let schema = Schema::of_tables((0..50).map(|t| {
            Schema::new((0..4_000).map(|i| Field::new(format!("c{i}"), DataType::Integer)))
                .named(format!("t{t}"))
        }));
        let timeline = SchemaTimeline::new()
            .push("v1", schema.clone())
            .push("v2", schema)
            .details(false)
            .charset(Charset::Ascii);
        let console = Console::builder().width(60).color_system(None).build();
        let started = std::time::Instant::now();
        let out = console.render_to_string(&timeline);
        assert!(started.elapsed() < std::time::Duration::from_secs(20));
        assert!(out.starts_with("t0.c0 "), "{out}");
        assert!(
            out.ends_with(
                "
… 199800 more fields"
            ),
            "{out}"
        );
    }

    /// The rows past [`MAX_ROWS`] were counted in a milestone at the first
    /// version's place, which its label always took: never shown.
    #[test]
    fn rows_not_drawn_are_counted() {
        let schema =
            Schema::new((0..MAX_ROWS + 5).map(|i| Field::new(format!("f{i}"), DataType::Integer)));
        let timeline = SchemaTimeline::new()
            .push("v1", schema)
            .charset(Charset::Ascii);
        let console = Console::builder().width(40).color_system(None).build();
        let out = console.render_to_string(&timeline);
        let lines: Vec<&str> = out.lines().collect();
        assert_eq!(lines.len(), MAX_ROWS + 4, "{out}");
        assert!(lines[MAX_ROWS - 1].starts_with("f199 "));
        assert!(lines[MAX_ROWS].contains("* v1"));
        assert_eq!(lines[MAX_ROWS + 3], "… 5 more fields");
    }
}