dsc-rs 0.10.30

Discourse CLI tool for managing multiple Discourse forums: track installs, run upgrades over SSH, manage emojis, sync topics and categories as Markdown, and more.
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
use anyhow::{Context, Result};
use std::collections::HashMap;
use std::fs;
use std::io::IsTerminal;
use std::path::{Path, PathBuf};

/// Trim trailing slashes from a base URL.
pub fn normalize_baseurl(baseurl: &str) -> String {
    baseurl.trim_end_matches('/').to_string()
}

/// Create a URL-safe slug from arbitrary input.
///
/// Wraps the [`slug`] crate, which transliterates Unicode (so `"Café"`
/// becomes `"cafe"`, Cyrillic and CJK get sensible romanisations) and
/// emits the standard kebab-case shape used across most slug-generating
/// tooling. Returns `"untitled"` when the slug would otherwise be empty
/// (the `slug` crate itself returns an empty string for input that has
/// no transliterable characters).
pub fn slugify(input: &str) -> String {
    let s = slug::slugify(input);
    if s.is_empty() {
        "untitled".to_string()
    } else {
        s
    }
}

/// Ensure a directory exists.
pub fn ensure_dir(path: &Path) -> Result<()> {
    fs::create_dir_all(path).with_context(|| format!("creating {}", path.display()))?;
    Ok(())
}

/// Resolve a topic path from a user-provided path and a topic title.
pub fn resolve_topic_path(
    provided: Option<&Path>,
    title: &str,
    default_dir: &Path,
) -> Result<PathBuf> {
    let filename = format!("{}.md", slugify(title));
    match provided {
        Some(path) if path.exists() && path.is_dir() => Ok(path.join(filename)),
        Some(path) if path.extension().is_some() => Ok(path.to_path_buf()),
        Some(path) => Ok(path.join(filename)),
        None => Ok(default_dir.join(filename)),
    }
}

/// Read a Markdown file.
pub fn read_markdown(path: &Path) -> Result<String> {
    let raw = fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?;
    Ok(raw)
}

/// Write a Markdown file, creating parent directories if needed.
pub fn write_markdown(path: &Path, content: &str) -> Result<()> {
    if let Some(parent) = path.parent() {
        ensure_dir(parent)?;
    }
    fs::write(path, content).with_context(|| format!("writing {}", path.display()))?;
    Ok(())
}

/// Quote a YAML scalar if it contains characters that would confuse the
/// parser. Keeps simple values unquoted. Shared by every command that
/// writes YAML front matter (`topic pull --full`, `category pull`).
pub fn yaml_scalar(value: &str) -> String {
    let needs_quoting = value.is_empty()
        || value.contains(':')
        || value.contains('#')
        || value.contains('\n')
        || value.starts_with([
            '-', '?', '!', '&', '*', '|', '>', '@', '`', '%', '\'', '"', '[',
        ])
        || value.starts_with("  ");
    if needs_quoting {
        let escaped = value.replace('\\', "\\\\").replace('"', "\\\"");
        format!("\"{}\"", escaped)
    } else {
        value.to_string()
    }
}

/// Split a Markdown document into its leading YAML front matter (if any) and
/// the body that follows.
///
/// Front matter is recognised only when the file's very first line is exactly
/// `---` (an optional leading BOM is tolerated), terminated by a later line
/// that is exactly `---`. The fenced block is parsed shallowly into a flat
/// `key → value` map (one `key: value` pair per line; lines without a colon
/// are ignored) — `dsc` front matter is intentionally shallow (`title`,
/// `topic_id`, `url`, `pulled_at`), so a full YAML parse is unnecessary and a
/// flat scan keeps the body intact.
///
/// Returns `(map, body)`. When there is no recognisable front matter the map
/// is empty and the body is the original content unchanged, so callers can
/// treat "no front matter" and "empty front matter" identically. One blank
/// line separating the closing fence from the body is consumed (it mirrors
/// what the `pull` side writes), giving a stable pull → push round-trip.
///
/// Note the inherent ambiguity shared with Jekyll/Hugo: a file with no front
/// matter whose body genuinely opens with a `---` thematic break followed by
/// another `---` will be misread as front matter. This is accepted; real
/// snapshots written by `dsc` always carry proper front matter.
pub fn strip_frontmatter(raw: &str) -> (HashMap<String, String>, String) {
    let mut map = HashMap::new();
    let text = raw.strip_prefix('\u{feff}').unwrap_or(raw);

    let mut lines = text.lines();
    if lines.next().map(str::trim_end) != Some("---") {
        return (map, raw.to_string());
    }

    let mut body_lines: Vec<&str> = Vec::new();
    let mut closed = false;
    for line in &mut lines {
        if line.trim_end() == "---" {
            closed = true;
            break;
        }
        if let Some((key, value)) = line.split_once(':') {
            map.insert(key.trim().to_string(), unquote_yaml_scalar(value.trim()));
        }
    }

    if !closed {
        // Opening fence with no matching close: not front matter after all.
        return (HashMap::new(), raw.to_string());
    }

    body_lines.extend(lines);
    // Consume a single conventional blank line between fence and body.
    if body_lines.first() == Some(&"") {
        body_lines.remove(0);
    }
    let mut body = body_lines.join("\n");
    if raw.ends_with('\n') && !body.is_empty() {
        body.push('\n');
    }
    (map, body)
}

/// Inverse of [`yaml_scalar`]'s quoting: if `value` is wrapped in double
/// quotes, strip them and unescape `\"` and `\\`. Bare values pass through
/// unchanged, so a value Discourse never sees as quoted (an integer, a URL)
/// is untouched.
fn unquote_yaml_scalar(value: &str) -> String {
    let bytes = value.as_bytes();
    if bytes.len() < 2 || bytes[0] != b'"' || bytes[bytes.len() - 1] != b'"' {
        return value.to_string();
    }
    let inner = &value[1..value.len() - 1];
    let mut out = String::with_capacity(inner.len());
    let mut chars = inner.chars();
    while let Some(c) = chars.next() {
        if c == '\\' {
            match chars.next() {
                Some('"') => out.push('"'),
                Some('\\') => out.push('\\'),
                Some(other) => {
                    out.push('\\');
                    out.push(other);
                }
                None => out.push('\\'),
            }
        } else {
            out.push(c);
        }
    }
    out
}

/// Current time in `YYYY-MM-DDTHH:MM:SSZ` form, derived directly from
/// `SystemTime` to avoid a chrono dependency where one is not otherwise
/// needed. Used for the `pulled_at` front-matter stamp.
pub fn current_utc_iso8601() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);
    // Days-from-epoch arithmetic (proleptic Gregorian via the standard
    // 1970-01-01 epoch). Good for any year `dsc` will plausibly run in.
    let days = (secs / 86_400) as i64;
    let secs_of_day = secs % 86_400;
    let hh = secs_of_day / 3600;
    let mm = (secs_of_day % 3600) / 60;
    let ss = secs_of_day % 60;
    let (y, m, d) = civil_from_days(days);
    format!("{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z", y, m, d, hh, mm, ss)
}

/// Convert days-from-1970-01-01 to (year, month, day).
/// Reference: Howard Hinnant, "chrono-Compatible Low-Level Date Algorithms".
fn civil_from_days(z: i64) -> (i32, u32, u32) {
    let z = z + 719_468;
    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let doe = (z - era * 146_097) as u64; // [0, 146096]
    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365; // [0, 399]
    let y = yoe as i64 + era * 400;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
    let mp = (5 * doy + 2) / 153; // [0, 11]
    let d = doy - (153 * mp + 2) / 5 + 1; // [1, 31]
    let m = if mp < 10 { mp + 3 } else { mp - 9 }; // [1, 12]
    let y = y + if m <= 2 { 1 } else { 0 };
    (y as i32, m as u32, d as u32)
}

fn color_mode() -> &'static str {
    match std::env::var("DSC_COLOR") {
        Ok(value) => match value.trim().to_ascii_lowercase().as_str() {
            "always" => "always",
            "never" => "never",
            _ => "auto",
        },
        Err(_) => "auto",
    }
}

fn color_allowed_for_stdout() -> bool {
    if std::env::var_os("NO_COLOR").is_some() {
        return false;
    }
    match color_mode() {
        "always" => true,
        "never" => false,
        _ => std::io::stdout().is_terminal(),
    }
}

fn discourse_color_code(key: &str) -> u8 {
    const COLORS: [u8; 12] = [31, 32, 33, 34, 35, 36, 91, 92, 93, 94, 95, 96];
    let hash = key.bytes().fold(0usize, |acc, b| {
        acc.wrapping_mul(31).wrapping_add(b as usize)
    });
    COLORS[hash % COLORS.len()]
}

pub fn color_discourse_label(label: &str, key: &str) -> String {
    if !color_allowed_for_stdout() {
        return label.to_string();
    }
    let code = discourse_color_code(key);
    format!("\x1b[1;{}m{}\x1b[0m", code, label)
}

/// Parse a `--since`-style value. Accepts either a relative duration
/// (`7d`, `24h`, `30m`, `1w`, `90s`) or an ISO-8601 absolute timestamp
/// (`2026-04-01`, `2026-04-01T12:00:00Z`). Returns the resulting cutoff
/// instant (now - duration, or the ISO value itself).
pub fn parse_since_cutoff(input: &str) -> anyhow::Result<chrono::DateTime<chrono::Utc>> {
    use anyhow::anyhow;
    let trimmed = input.trim();
    if trimmed.is_empty() {
        return Err(anyhow!("empty --since value"));
    }

    if let Some(duration) = parse_relative_duration(trimmed) {
        return Ok(chrono::Utc::now() - duration);
    }

    // Try RFC3339 (full timestamp).
    if let Ok(dt) = chrono::DateTime::parse_from_rfc3339(trimmed) {
        return Ok(dt.with_timezone(&chrono::Utc));
    }
    // Try date-only — treat as midnight UTC.
    if let Ok(d) = chrono::NaiveDate::parse_from_str(trimmed, "%Y-%m-%d") {
        return Ok(chrono::NaiveDateTime::new(
            d,
            chrono::NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
        )
        .and_utc());
    }

    Err(anyhow!(
        "unrecognised --since value: {:?} (expected e.g. `7d`, `24h`, `30m`, `1w`, or an ISO-8601 timestamp)",
        input
    ))
}

/// Parse a relative duration like `7d`, `24h`, `1w`, `1m`, `90s`, `1y`.
///
/// Calendar units (`m`, `y`) are imprecise; for windows we use these
/// conventions:
///
/// - `s` — seconds
/// - `min` — minutes (use this rather than `m` to avoid the months-vs-minutes
///   ambiguity)
/// - `h` — hours
/// - `d` — days
/// - `w` — weeks (= 7 days)
/// - `m` — **months** (= 30 days; matches what most users mean by "1m" in
///   analytics windows)
/// - `y` — years (= 365 days)
///
/// For exact calendar math, pass an ISO-8601 timestamp instead.
pub fn parse_relative_duration(input: &str) -> Option<chrono::Duration> {
    let s = input.trim();
    if s.len() < 2 {
        return None;
    }
    // Order matters: `min` must be tried before `m` so we don't read
    // "10min" as "10mi" + "n".
    let multi_char_units = [("min", 60i64)];
    for (suffix, secs_per_unit) in multi_char_units {
        if let Some(digits) = s.strip_suffix(suffix) {
            let n: i64 = digits.parse().ok()?;
            return Some(chrono::Duration::seconds(n * secs_per_unit));
        }
    }
    let (digits, unit) = s.split_at(s.len() - 1);
    let n: i64 = digits.parse().ok()?;
    match unit {
        "s" => Some(chrono::Duration::seconds(n)),
        "h" => Some(chrono::Duration::hours(n)),
        "d" => Some(chrono::Duration::days(n)),
        "w" => Some(chrono::Duration::weeks(n)),
        "m" => Some(chrono::Duration::days(n * 30)),
        "y" => Some(chrono::Duration::days(n * 365)),
        _ => None,
    }
}

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

    #[test]
    fn slugify_simple_ascii() {
        assert_eq!(slugify("Hello World"), "hello-world");
    }

    #[test]
    fn slugify_collapses_runs_of_non_alnum() {
        assert_eq!(slugify("a   b___c!!!d"), "a-b-c-d");
    }

    #[test]
    fn slugify_trims_leading_and_trailing_dashes() {
        assert_eq!(slugify("   hello   "), "hello");
        assert_eq!(slugify("!!!foo!!!"), "foo");
    }

    #[test]
    fn slugify_empty_input_returns_untitled() {
        assert_eq!(slugify(""), "untitled");
        assert_eq!(slugify("   "), "untitled");
        assert_eq!(slugify("!!!"), "untitled");
    }

    #[test]
    fn slugify_preserves_numbers() {
        assert_eq!(slugify("Topic 42 - intro"), "topic-42-intro");
    }

    #[test]
    fn slugify_lowercases() {
        assert_eq!(slugify("ABCxyz"), "abcxyz");
    }

    #[test]
    fn slugify_transliterates_unicode() {
        // The whole reason for adopting the `slug` crate: pre-existing
        // ASCII behaviour preserved, plus accented Latin, Cyrillic, and
        // CJK now produce meaningful slugs instead of "untitled".
        assert_eq!(slugify("Café Tonight"), "cafe-tonight");
        assert_eq!(slugify("Привет мир"), "privet-mir");
        assert_eq!(slugify("日本語"), "ri-ben-yu");
    }

    #[test]
    fn slugify_trims_both_ends_of_dashes() {
        // Regression guard: catches a latent bug from a contributor PR
        // that only trimmed trailing dashes. The `slug` crate handles
        // both ends correctly.
        assert_eq!(slugify("-foo-"), "foo");
        assert_eq!(slugify("---foo---bar---"), "foo-bar");
    }

    #[test]
    fn normalize_baseurl_strips_trailing_slashes() {
        assert_eq!(
            normalize_baseurl("https://example.com/"),
            "https://example.com"
        );
        assert_eq!(
            normalize_baseurl("https://example.com///"),
            "https://example.com"
        );
        assert_eq!(
            normalize_baseurl("https://example.com"),
            "https://example.com"
        );
    }

    #[test]
    fn normalize_baseurl_preserves_no_trailing() {
        assert_eq!(normalize_baseurl(""), "");
    }

    #[test]
    fn resolve_topic_path_uses_title_when_no_path_given() {
        let default_dir = Path::new("/tmp/dsc-test");
        let out = resolve_topic_path(None, "Hello World", default_dir).unwrap();
        assert_eq!(out, default_dir.join("hello-world.md"));
    }

    #[test]
    fn resolve_topic_path_uses_given_path_with_extension() {
        let default_dir = Path::new("/tmp/dsc-test");
        let explicit = Path::new("/tmp/custom.md");
        let out = resolve_topic_path(Some(explicit), "Ignored", default_dir).unwrap();
        assert_eq!(out, explicit);
    }

    #[test]
    fn parse_relative_duration_common_units() {
        assert_eq!(
            parse_relative_duration("7d"),
            Some(chrono::Duration::days(7))
        );
        assert_eq!(
            parse_relative_duration("24h"),
            Some(chrono::Duration::hours(24))
        );
        assert_eq!(
            parse_relative_duration("30min"),
            Some(chrono::Duration::minutes(30))
        );
        assert_eq!(
            parse_relative_duration("1w"),
            Some(chrono::Duration::weeks(1))
        );
        assert_eq!(
            parse_relative_duration("90s"),
            Some(chrono::Duration::seconds(90))
        );
    }

    #[test]
    fn parse_relative_duration_rejects_nonsense() {
        assert!(parse_relative_duration("").is_none());
        assert!(parse_relative_duration("d").is_none());
        assert!(parse_relative_duration("7x").is_none());
        assert!(parse_relative_duration("abc").is_none());
        assert!(parse_relative_duration("3M").is_none()); // case-sensitive
    }

    #[test]
    fn parse_relative_duration_treats_m_as_months() {
        // `m` = months (= 30 days). Users naturally write `1m` for "one
        // month" in analytics windows; we match that. Use `min` for the
        // rare minutes case.
        assert_eq!(
            parse_relative_duration("1m"),
            Some(chrono::Duration::days(30))
        );
        assert_eq!(
            parse_relative_duration("3m"),
            Some(chrono::Duration::days(90))
        );
    }

    #[test]
    fn parse_relative_duration_minutes_via_min_suffix() {
        assert_eq!(
            parse_relative_duration("5min"),
            Some(chrono::Duration::minutes(5))
        );
        assert_eq!(
            parse_relative_duration("90min"),
            Some(chrono::Duration::minutes(90))
        );
    }

    #[test]
    fn parse_relative_duration_accepts_years_as_365d() {
        assert_eq!(
            parse_relative_duration("1y"),
            Some(chrono::Duration::days(365))
        );
        assert_eq!(
            parse_relative_duration("2y"),
            Some(chrono::Duration::days(730))
        );
    }

    #[test]
    fn parse_since_cutoff_iso_date() {
        let cutoff = parse_since_cutoff("2026-01-01").unwrap();
        assert_eq!(cutoff.to_rfc3339(), "2026-01-01T00:00:00+00:00");
    }

    #[test]
    fn parse_since_cutoff_iso_timestamp() {
        let cutoff = parse_since_cutoff("2026-04-15T12:30:00Z").unwrap();
        assert_eq!(cutoff.to_rfc3339(), "2026-04-15T12:30:00+00:00");
    }

    #[test]
    fn parse_since_cutoff_relative_is_in_the_past() {
        let now = chrono::Utc::now();
        let cutoff = parse_since_cutoff("7d").unwrap();
        let diff = now - cutoff;
        // Should be very close to 7 days (within a second).
        assert!(
            (diff - chrono::Duration::days(7)).num_seconds().abs() < 2,
            "expected ~7 day delta, got {}",
            diff
        );
    }

    #[test]
    fn parse_since_cutoff_rejects_garbage() {
        assert!(parse_since_cutoff("not a date").is_err());
        assert!(parse_since_cutoff("").is_err());
    }

    #[test]
    fn yaml_scalar_leaves_simple_values_bare() {
        assert_eq!(
            yaml_scalar("Dependency management"),
            "Dependency management"
        );
        assert_eq!(yaml_scalar("Topic 42"), "Topic 42");
    }

    #[test]
    fn yaml_scalar_quotes_when_needed() {
        assert_eq!(yaml_scalar("a: b"), "\"a: b\"");
        assert_eq!(yaml_scalar("# hash"), "\"# hash\"");
        assert_eq!(yaml_scalar("- leading dash"), "\"- leading dash\"");
        // Interior quotes alone do not trigger quoting; a quote that coincides
        // with another trigger (here the colon) is escaped inside the wrap.
        assert_eq!(yaml_scalar("she said \"hi\""), "she said \"hi\"");
        assert_eq!(yaml_scalar("a: \"b\""), "\"a: \\\"b\\\"\"");
    }

    #[test]
    fn strip_frontmatter_parses_block_and_body() {
        let raw = "---\ntitle: Dependency management\ntopic_id: 412\nurl: https://forum.rcpch.tech/t/dependency-management/412\npulled_at: 2026-06-22T09:19:00Z\n---\n\nBody line one.\nBody line two.\n";
        let (front, body) = strip_frontmatter(raw);
        assert_eq!(front.get("topic_id").map(String::as_str), Some("412"));
        assert_eq!(
            front.get("title").map(String::as_str),
            Some("Dependency management")
        );
        assert_eq!(
            front.get("url").map(String::as_str),
            Some("https://forum.rcpch.tech/t/dependency-management/412")
        );
        assert_eq!(body, "Body line one.\nBody line two.\n");
    }

    #[test]
    fn strip_frontmatter_absent_returns_empty_map_and_full_body() {
        let raw = "# Heading\n\nNo front matter here.\n";
        let (front, body) = strip_frontmatter(raw);
        assert!(front.is_empty());
        assert_eq!(body, raw);
    }

    #[test]
    fn strip_frontmatter_unclosed_fence_is_not_front_matter() {
        // Opening `---` but never closed: treat the whole thing as body.
        let raw = "---\ntitle: oops\nstill body, no closing fence\n";
        let (front, body) = strip_frontmatter(raw);
        assert!(front.is_empty());
        assert_eq!(body, raw);
    }

    #[test]
    fn strip_frontmatter_preserves_horizontal_rules_in_body() {
        // A `---` inside the body (after the real close) must survive intact.
        let raw = "---\ntopic_id: 7\n---\n\nIntro.\n\n---\n\nAfter the rule.\n";
        let (front, body) = strip_frontmatter(raw);
        assert_eq!(front.get("topic_id").map(String::as_str), Some("7"));
        assert_eq!(body, "Intro.\n\n---\n\nAfter the rule.\n");
    }

    #[test]
    fn strip_frontmatter_unquotes_yaml_scalar_values() {
        // yaml_scalar quotes a title containing a colon; strip must invert it.
        let title = "Intro: getting started";
        let raw = format!(
            "---\ntitle: {}\ntopic_id: 3\n---\n\nbody\n",
            yaml_scalar(title)
        );
        let (front, body) = strip_frontmatter(&raw);
        assert_eq!(front.get("title").map(String::as_str), Some(title));
        assert_eq!(front.get("topic_id").map(String::as_str), Some("3"));
        assert_eq!(body, "body\n");
    }

    #[test]
    fn strip_frontmatter_leaves_url_with_colons_intact() {
        // URLs are written bare (not via yaml_scalar) and only the first colon
        // separates key from value, so the scheme colon must survive.
        let raw = "---\nurl: https://forum.rcpch.tech/t/x/9\n---\n\nbody\n";
        let (front, _) = strip_frontmatter(raw);
        assert_eq!(
            front.get("url").map(String::as_str),
            Some("https://forum.rcpch.tech/t/x/9")
        );
    }

    #[test]
    fn strip_frontmatter_tolerates_leading_bom() {
        let raw = "\u{feff}---\ntopic_id: 99\n---\n\nbody\n";
        let (front, body) = strip_frontmatter(raw);
        assert_eq!(front.get("topic_id").map(String::as_str), Some("99"));
        assert_eq!(body, "body\n");
    }

    #[test]
    fn current_utc_iso8601_has_expected_shape() {
        let s = current_utc_iso8601();
        assert_eq!(s.len(), 20, "got {s:?}");
        assert!(s.ends_with('Z'));
        assert_eq!(&s[4..5], "-");
        assert_eq!(&s[10..11], "T");
    }

    #[test]
    fn civil_from_days_matches_known_dates() {
        // 1970-01-01 is day 0.
        assert_eq!(civil_from_days(0), (1970, 1, 1));
        // 2026-06-10 = 20614 days from epoch (well-known via cal / date).
        assert_eq!(civil_from_days(20614), (2026, 6, 10));
        // Leap-day check: 2024-02-29.
        assert_eq!(civil_from_days(19782), (2024, 2, 29));
    }
}