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
//! The short text a row carries beside its title, for a value a setup
//! asked `bdi` to show.
//!
//! Nothing here learns what a key means. Which keys are worth drawing, and
//! what to say for a value, are both config's to state; this renders what it
//! is given and knows no more about `blocked_on` than about any other key.
use std::collections::BTreeSet;
use serde::Serialize;
use crate::config::{Badge, Colour};
use crate::model::types::Bead;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Badged {
pub key: String,
pub text: String,
/// What it says instead on a row too narrow for `text`, for one whose
/// config names a `short`.
///
/// Both forms travel, because which of them a row can afford is the width
/// the row was given, and nothing here has one.
pub short: Option<String>,
/// Where the badge points, for one whose config names a `link`.
///
/// Beside the text rather than inside it, and beside it the whole way to
/// the row: a line is fitted by the visible width of what its spans say,
/// so a URL held in the text would be counted in the columns the row has
/// to spend.
pub link: Option<String>,
/// What to draw it in, for one whose config named a colour. A slot the
/// view resolves rather than a colour, so nothing here learns what the
/// row is drawn in any more than it learns what the key means.
pub colour: Option<Colour>,
}
/// A badge that drew less than its config asked for.
///
/// Every badge here read the value and then could not keep a promise its
/// config made about the badge it drew. A pattern that does not read the value
/// is a filter declining, which is what patterns are for, so it is nowhere
/// here however many of them decline in a row.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "undrawn", rename_all = "kebab-case")]
pub enum Undrawn {
/// A badge without its link: the template named a capture this value did
/// not supply, and a destination built round a part that was never there
/// points somewhere else.
Link { key: String },
/// A badge without its short form, for the same reason: a form built round
/// a part that was never there says something the value does not. The row
/// falls back to cutting the long form, and a narrow pane loses a badge the
/// config meant to keep.
///
/// Reported for any badge naming a `short`, `link` or no `link`: naming
/// one is a promise about a length, made by the same config that would
/// otherwise have declined.
Short { key: String },
}
/// What this bead's configured badges came to: the ones it draws, and the
/// ones that fell short of what their config promised.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Badges {
pub drawn: Vec<Badged>,
pub undrawn: Vec<Undrawn>,
}
/// Render the configured badges that apply to this bead.
pub fn badges_for(bead: &Bead, badges: &[Badge]) -> Badges {
let mut drawn = Vec::new();
let mut undrawn = Vec::new();
let mut read: BTreeSet<&str> = BTreeSet::new();
for badge in badges {
let Some(value) = bead.values.get(&badge.key).map(String::as_str) else {
continue;
};
// Badges on one key are a chain read in config order, so the entries
// below the one that read this value are what the reader wrote for the
// values it does not read. They are not tried at all, and neither is
// what their config promised about a value they were never given.
if read.contains(badge.key.as_str()) {
continue;
}
let Some(text) = badge.apply(value) else {
continue;
};
read.insert(badge.key.as_str());
let link = badge.link_for(value);
if badge.link.is_some() && link.is_none() {
undrawn.push(Undrawn::Link {
key: badge.key.clone(),
});
}
let short = badge.short_for(value);
if badge.short.is_some() && short.is_none() {
undrawn.push(Undrawn::Short {
key: badge.key.clone(),
});
}
drawn.push(Badged {
key: badge.key.clone(),
text,
short,
link,
colour: badge.colour,
});
}
Badges { drawn, undrawn }
}
#[cfg(test)]
mod tests {
use super::*;
use crate::collect::bd::parse_beads;
use pretty_assertions::assert_eq;
use crate::config::Pattern;
fn matching(pattern: &str) -> Pattern {
Pattern::new(pattern).expect("the pattern compiles")
}
fn bead_with(metadata: &str) -> Bead {
let json =
format!(r#"[{{"id":"p-1","title":"root","status":"open","metadata":{metadata}}}]"#);
parse_beads(&json).expect("the bead parses").remove(0)
}
#[test]
fn badges_render_only_where_the_key_and_match_agree() {
let bead = bead_with(r#"{"blocked_on":"human","delivery_pr":"owner/repo#7"}"#);
let cfg = vec![
Badge {
key: "metadata.blocked_on".into(),
match_value: Some(matching("human")),
render: "waiting".into(),
link: None,
short: None,
colour: None,
},
Badge {
key: "metadata.blocked_on".into(),
match_value: Some(matching("dependency")),
render: "dep".into(),
link: None,
short: None,
colour: None,
},
Badge {
key: "metadata.absent_key".into(),
match_value: None,
render: "never".into(),
link: None,
short: None,
colour: None,
},
];
let got = badges_for(&bead, &cfg);
assert_eq!(
got.drawn,
vec![Badged {
key: "metadata.blocked_on".to_string(),
text: "waiting".to_string(),
link: None,
short: None,
colour: None,
}]
);
}
/// Nothing in the model learns what a metadata key means: a key it has
/// never heard of renders exactly as well as a familiar one.
#[test]
fn badges_render_a_configured_key_without_interpreting_it() {
let bead = bead_with(r#"{"xyzzy":"plugh"}"#);
let cfg = vec![Badge {
key: "metadata.xyzzy".into(),
match_value: None,
render: "→ {}".into(),
link: None,
short: None,
colour: None,
}];
let got = badges_for(&bead, &cfg);
assert_eq!(
got.drawn,
vec![Badged {
key: "metadata.xyzzy".to_string(),
text: "→ plugh".to_string(),
link: None,
short: None,
colour: None,
}]
);
}
/// The colour a badge's config named travels beside its text, because
/// nothing downstream of here can go back and read the config: the row
/// carries badges and the view draws a row.
#[test]
fn a_badges_colour_travels_with_its_text() {
let bead = bead_with(r#"{"jira":"ARKHAM-19"}"#);
let cfg = vec![Badge {
key: "metadata.jira".into(),
match_value: None,
render: "{}".into(),
link: None,
short: None,
colour: Some(Colour::Status),
}];
let got = badges_for(&bead, &cfg);
assert_eq!(
got.drawn,
vec![Badged {
key: "metadata.jira".to_string(),
text: "ARKHAM-19".to_string(),
link: None,
short: None,
colour: Some(Colour::Status),
}]
);
}
#[test]
fn a_bead_with_no_configured_badges_renders_none() {
let bead = bead_with(r#"{"blocked_on":"human"}"#);
assert_eq!(badges_for(&bead, &[]), Badges::default());
}
// ---- the bead's own external reference -------------------------------
fn bead_referencing(external_ref: &str) -> Bead {
let json = format!(
r#"[{{"id":"p-1","title":"root","status":"open",
"external_ref":{external_ref},"metadata":{{"jira":"ARKHAM-19"}}}}]"#
);
parse_beads(&json).expect("the bead parses").remove(0)
}
/// A tracker running a sync adapter holds its reference in the field
/// rather than in metadata, and a badge reading the field draws it exactly
/// as a badge reading metadata draws that.
#[test]
fn a_badge_on_the_external_reference_draws_it_as_it_draws_metadata() {
let bead = bead_referencing(r#""https://jira.invalid/browse/HELIO-412""#);
let cfg = vec![
Badge {
key: "external_ref".into(),
match_value: Some(matching(r".*/(?<ticket>[A-Z]+-[0-9]+)")),
render: "{ticket}".into(),
link: Some("https://jira.invalid/browse/{ticket}".into()),
short: None,
colour: None,
},
Badge {
key: "metadata.jira".into(),
match_value: None,
render: "{}".into(),
link: None,
short: None,
colour: None,
},
];
let got = badges_for(&bead, &cfg);
assert_eq!(
got.drawn,
vec![
Badged {
key: "external_ref".to_string(),
text: "HELIO-412".to_string(),
link: Some("https://jira.invalid/browse/HELIO-412".to_string()),
short: None,
colour: None,
},
Badged {
key: "metadata.jira".to_string(),
text: "ARKHAM-19".to_string(),
link: None,
short: None,
colour: None,
},
]
);
assert_eq!(got.undrawn, Vec::new());
}
/// The field is empty on every bead of a tracker no sync adapter fills, so
/// this is the case the badge meets most often. It is not a badge that fell
/// short: it is a badge that was never about this bead.
///
/// A field the bead leaves unset and a metadata key it never wrote are two
/// paths to the same silence, and a badge promising a `link` and a `short`
/// is the one with most to report if either path takes it. Asserted
/// together because only one of them existed before a key could name a
/// field.
#[test]
fn a_value_a_bead_does_not_hold_draws_no_badge_and_reports_nothing() {
let promising = |key: &str| Badge {
key: key.to_string(),
match_value: None,
render: "{}".into(),
link: Some("https://jira.invalid/browse/{}".into()),
short: Some("{}".into()),
colour: None,
};
// Every way bd spells an unset field, and the field left out entirely.
for spelling in [r#""""#, "null"] {
let got = badges_for(&bead_referencing(spelling), &[promising("external_ref")]);
assert_eq!(got.drawn, Vec::new(), "drew on {spelling}");
assert_eq!(got.undrawn, Vec::new(), "reported on {spelling}");
}
let carrying_neither = bead_with(r#"{"jira":"ARKHAM-19"}"#);
for key in ["external_ref", "metadata.nobody_wrote_this"] {
let got = badges_for(&carrying_neither, &[promising(key)]);
assert_eq!(got.drawn, Vec::new(), "drew on {key}");
assert_eq!(got.undrawn, Vec::new(), "reported on {key}");
}
}
// ---- several badges on one key ---------------------------------------
/// Ordering is what a list of badges on one key says: each is tried until
/// one reads the value, so the qualified form above a permissive entry
/// draws the qualified form and the entry below it stands in for nothing.
///
/// The permissive entry names a `short` its own pattern cannot fill, which
/// is something running it would have had to report. Its silence is how
/// this asserts it never ran at all.
#[test]
fn two_badges_on_one_key_that_both_read_a_value_draw_the_first_alone() {
let bead = bead_with(r#"{"delivery_pr":"dunwich/arkham#30"}"#);
let permissive = Badge {
match_value: Some(matching(".*")),
render: "⇢ {}".into(),
short: Some("⇢ {repo}".into()),
link: None,
..qualified_only()
};
let got = badges_for(&bead, &[qualified_only(), permissive]);
assert_eq!(
got.drawn,
vec![Badged {
key: "metadata.delivery_pr".to_string(),
text: "⇢ #30".to_string(),
short: None,
link: Some("https://forge.invalid/dunwich/arkham/pull/30".to_string()),
colour: None,
}]
);
assert_eq!(got.undrawn, Vec::new());
}
/// A value every badge on its key declined is a value nothing was written
/// to read, and that is the reader's list saying what it wanted rather than
/// anything falling short. A `link` on the badges that declined is a
/// promise about where a badge points and not about which values reach one.
#[test]
fn a_value_no_badge_on_its_key_reads_draws_nothing_and_reports_nothing() {
let bead = bead_with(r#"{"delivery_pr":"30"}"#);
let url_only = Badge {
match_value: Some(matching(
r"https://forge\.invalid/[^/]+/(?<repo>[^/]+)/pull/(?<number>[0-9]+)",
)),
..qualified_only()
};
let got = badges_for(&bead, &[qualified_only(), url_only]);
assert_eq!(got.drawn, Vec::new());
assert_eq!(got.undrawn, Vec::new());
}
// ---- what a badge meant to draw could not draw -----------------------
/// The pattern a global list writes for a `delivery_pr` reads the
/// qualified form. A tracker holding a bare number as well has beads this
/// pattern cannot read at all, and a permissive entry below it is how a
/// reader asks to see them.
fn qualified_only() -> Badge {
Badge {
key: "metadata.delivery_pr".into(),
match_value: Some(matching(
r"(?<owner>[^/]+)/(?<repo>[^#]+)#(?<number>[0-9]+)",
)),
render: "⇢ #{number}".into(),
link: Some("https://forge.invalid/{owner}/{repo}/pull/{number}".into()),
short: None,
colour: None,
}
}
/// The same silence read off the badge the shipped waiting badge is written
/// as, where the one above is read off a pair that name a `link`: a pattern
/// is how a config says which values it wants, and declining is what every
/// pattern is there to do.
#[test]
fn a_badge_configured_to_decline_stays_silent() {
let bead = bead_with(r#"{"blocked_on":"dependency"}"#);
let filter = Badge {
key: "metadata.blocked_on".into(),
match_value: Some(matching("human")),
render: "⏸ waiting".into(),
link: None,
short: None,
colour: None,
};
let got = badges_for(&bead, &[filter]);
assert_eq!(got.drawn, Vec::new());
assert_eq!(got.undrawn, Vec::new());
}
/// A key the bead does not carry is not a badge that fell short — it is a
/// badge that was never about this bead.
#[test]
fn a_badge_whose_key_the_bead_does_not_carry_reports_nothing() {
let bead = bead_with(r#"{"blocked_on":"human"}"#);
let got = badges_for(&bead, &[qualified_only()]);
assert_eq!(got.drawn, Vec::new());
assert_eq!(got.undrawn, Vec::new());
}
/// The badge draws and the link does not, which is the case a reader
/// cannot see: an ordinary-looking badge that has quietly lost its
/// destination.
#[test]
fn a_badge_reports_a_link_its_value_could_not_fill() {
let bead = bead_with(r#"{"delivery_pr":"30"}"#);
let either_form = Badge {
match_value: Some(matching(
r"(?:(?<owner>[^/]+)/(?<repo>[^#]+))?#?(?<number>[0-9]+)",
)),
..qualified_only()
};
let got = badges_for(&bead, &[either_form]);
assert_eq!(
got.drawn,
vec![Badged {
key: "metadata.delivery_pr".to_string(),
text: "⇢ #30".to_string(),
link: None,
short: None,
colour: None,
}]
);
assert_eq!(
got.undrawn,
vec![Undrawn::Link {
key: "metadata.delivery_pr".to_string()
}]
);
}
#[test]
fn a_badge_that_draws_its_link_reports_nothing() {
let bead = bead_with(r#"{"delivery_pr":"dunwich/arkham#30"}"#);
let got = badges_for(&bead, &[qualified_only()]);
assert_eq!(
got.drawn,
vec![Badged {
key: "metadata.delivery_pr".to_string(),
text: "⇢ #30".to_string(),
link: Some("https://forge.invalid/dunwich/arkham/pull/30".to_string()),
short: None,
colour: None,
}]
);
assert_eq!(got.undrawn, Vec::new());
}
/// Both forms come off one reading of the value, and both travel to the
/// row: which of them a row can afford is the view's to decide and not
/// this module's.
#[test]
fn a_badge_carries_the_short_form_its_config_named() {
let bead = bead_with(r#"{"delivery_pr":"dunwich/arkham#30"}"#);
let both_forms = Badge {
render: "⇢ {repo} #{number}".into(),
short: Some("⇢ #{number}".into()),
..qualified_only()
};
let got = badges_for(&bead, &[both_forms]);
assert_eq!(
got.drawn,
vec![Badged {
key: "metadata.delivery_pr".to_string(),
text: "⇢ arkham #30".to_string(),
short: Some("⇢ #30".to_string()),
link: Some("https://forge.invalid/dunwich/arkham/pull/30".to_string()),
colour: None,
}]
);
assert_eq!(got.undrawn, Vec::new());
}
/// A badge naming no short form is a badge with one length, which is
/// every badge written before there was a second one to name.
#[test]
fn a_badge_whose_config_names_no_short_form_carries_none() {
let bead = bead_with(r#"{"delivery_pr":"dunwich/arkham#30"}"#);
let got = badges_for(&bead, &[qualified_only()]);
assert_eq!(got.drawn[0].short, None);
assert_eq!(got.undrawn, Vec::new());
}
/// The `link` rule read across to the other template: a short form built
/// round a part that was never there says something the value does not.
/// It is dropped and reported, so the row falls back to cutting the long
/// form and the reader is told which key to go and look at.
///
/// Reported whatever the badge's `link`, unlike the two above it. A
/// `link` decides whether a badge *promised* to point anywhere; naming a
/// short form is that same promise made about a length.
#[test]
fn a_badge_reports_a_short_form_its_value_could_not_fill() {
let bead = bead_with(r#"{"delivery_pr":"30"}"#);
let unlinked_either_form = Badge {
match_value: Some(matching(
r"(?:(?<owner>[^/]+)/(?<repo>[^#]+))?#?(?<number>[0-9]+)",
)),
render: "⇢ #{number}".into(),
short: Some("⇢ {repo}".into()),
link: None,
..qualified_only()
};
let got = badges_for(&bead, &[unlinked_either_form]);
assert_eq!(
got.drawn,
vec![Badged {
key: "metadata.delivery_pr".to_string(),
text: "⇢ #30".to_string(),
short: None,
link: None,
colour: None,
}]
);
assert_eq!(
got.undrawn,
vec![Undrawn::Short {
key: "metadata.delivery_pr".to_string()
}]
);
}
/// A badge that declines the value says nothing at either length, so the
/// short form it names is nothing to report.
#[test]
fn a_badge_configured_to_decline_reports_no_short_form() {
let bead = bead_with(r#"{"blocked_on":"dependency"}"#);
let filter = Badge {
key: "metadata.blocked_on".into(),
match_value: Some(matching("human")),
render: "⏸ waiting".into(),
short: Some("⏸".into()),
link: None,
colour: None,
};
let got = badges_for(&bead, &[filter]);
assert_eq!(got.drawn, Vec::new());
assert_eq!(got.undrawn, Vec::new());
}
}