evidencebook 0.0.23

Evidence book: records, epistemic types, typed links, retention, disclosure, index classes, request answers and reconcile/close over an embedded Checkpointed Local Log -- a Rust implementation of the Evidence Layer semantics.
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
//! The `standard_history/1` deal entry (agent-action-capsule
//! `spec/standard-history-v0.md` §5.2, SH-2) as the profile fills it from a
//! deal's sealed records: a port of Go's `dealprofile/standard_history.go`,
//! as the Python package's `evidencebook.deal.standard_history`. The mapping
//! is here, with the profile, so every producer that counts x-deal-v0 deals
//! counts them the same way. A member with no sealed record behind it is
//! never guessed:
//!
//! * A deal is a deal's own log holding an `action` (or typed
//!   `action-record/v0`) `commit` or `pay`. A sale's log is not a deal; a deal
//!   that never reached such a step was never agreed.
//! * `agreed_at` is that first step's `committed_at`, to the minute.
//! * `role` is the baseline intent's `party_role`: seller is `provided`,
//!   buyer or absent `bought`.
//! * `amount` is the `amount_minor` and currency of the check (typed
//!   `proposed-action/v0`) the step rests on, scaled by the currency's ISO
//!   4217 minor units (BTC: 11, msat). A deal without one is [`Error::Unpriced`]: the host
//!   refuses the card, it never drops the deal.
//! * `counterparty_class` is the member of that name a pay check carries at
//!   the top of its record (SH §7), when it is one of SH's three values; a
//!   deal whose records carry it on two or more records, on a record that is
//!   no pay check, or with another value, lacks it.
//! * `outcome` is the newest verified `ec:deal-reconcile@1` Result's claims.
//!   TEMPORARY exception (evidencebook#462, until evidencebook#461's computed
//!   row status): a deal log holding the owner's own `outcome_note`
//!   (`did_not_hold`) that confirms a close on that log is `did_not_hold`,
//!   over any Result.
//! * `happened` is `stamped_by_both` only when the counterparty's half is
//!   received and verifies; else `receipt_only` when the holder's own final
//!   close observed a `received` outcome; else `no_record`.
//! * `disputes` is empty: the profile seals no IP §11.2 disputes statement.
//!
//! The entry is the JSON object `derive_card` takes in `deals`. Records are
//! read with serde_json: a member name is matched exactly, where Go's
//! `encoding/json` also matches it in another case.

use std::collections::HashMap;

use serde_json::{json, Map, Value};

use super::check::{digest, Error};
use super::names::*;

/// One sealed record of a deal's own log.
#[derive(Clone, Debug)]
pub struct HistoryRecord {
    /// The record's JSON text, the Capsule's payload.
    pub payload: Vec<u8>,
    /// When the holder's book committed it, RFC 3339.
    pub committed_at: String,
}

/// One Result the deal's results book sealed.
#[derive(Clone, Debug)]
pub struct HistoryResult {
    /// The `evidence-result-v0` document.
    pub result: Vec<u8>,
    /// The host's check that the Result's record signature and seal verify.
    /// A Result that does not is no Result.
    pub verified: bool,
}

/// What the host holds for one deal under the card's anchor.
#[derive(Clone, Debug, Default)]
pub struct HistoryInput {
    /// The deal log's records the anchor commits, in log order.
    pub records: Vec<HistoryRecord>,
    /// The deal's sealed Results the anchor commits, oldest first.
    pub results: Vec<HistoryResult>,
    /// Whether the counterparty's half of the deal is received and its
    /// signature verifies.
    pub counterparty_half_verified: bool,
}

const UNPRICED: &str = "dealprofile: a deal has no recorded amount";

struct Rec {
    committed_at: String,
    kind: String,
    deal_id: String,
    refs: HashMap<String, Vec<String>>,
    body: Map<String, Value>,
    has_class: bool,
    class: String,
}

impl Rec {
    fn str(&self, member: &str) -> &str {
        self.body.get(member).and_then(Value::as_str).unwrap_or("")
    }

    /// The one digest the record names under `rel`, or "".
    fn reference(&self, rel: &str) -> &str {
        match self.refs.get(rel).map(Vec::as_slice) {
            Some([d]) => d,
            _ => "",
        }
    }
}

fn not_json(why: &str) -> Error {
    Error::Invalid(format!("dealprofile: a deal record is not JSON: {why}"))
}

fn string_member(obj: &Map<String, Value>, name: &str) -> Result<String, Error> {
    match obj.get(name) {
        None | Some(Value::Null) => Ok(String::new()),
        Some(Value::String(s)) => Ok(s.clone()),
        Some(_) => Err(not_json(&format!("member {name} is not a string"))),
    }
}

fn refs_of(obj: &Map<String, Value>) -> Result<Vec<(String, String)>, Error> {
    let items = match obj.get("refs") {
        None | Some(Value::Null) => return Ok(Vec::new()),
        Some(Value::Array(a)) => a,
        Some(_) => return Err(not_json("refs is not an array")),
    };
    let mut out = Vec::new();
    for r in items {
        match r {
            Value::Null => out.push((String::new(), String::new())),
            Value::Object(o) => {
                // Go reads each into RelRef, whose members are all strings.
                string_member(o, "type")?;
                string_member(o, "digest_alg")?;
                out.push((string_member(o, "rel")?, string_member(o, "digest")?));
            }
            _ => return Err(not_json("a ref is not an object")),
        }
    }
    Ok(out)
}

/// A step record of either kind (with its digest); `None` for a record that
/// is no step, such as a sealed report.
fn read_record(r: &HistoryRecord) -> Result<Option<(String, Rec)>, Error> {
    if r.payload.starts_with(&[0xEF, 0xBB, 0xBF]) {
        return Err(not_json("a byte order mark"));
    }
    // Go replaces invalid UTF-8 with U+FFFD where serde_json refuses it; a lone
    // surrogate escape is still refused (the one acknowledged difference).
    let text = String::from_utf8_lossy(&r.payload);
    let top: Value = serde_json::from_str(&text).map_err(|e| not_json(&e.to_string()))?;
    // Go's Unmarshal of `null` into a struct is a no-op: the record is no step.
    if top.is_null() {
        return Ok(None);
    }
    let Value::Object(top) = top else {
        return Err(not_json("not an object"));
    };
    let block = match top.get("x-deal-v0") {
        None | Some(Value::Null) => None,
        Some(Value::Object(b)) => Some(b),
        Some(_) => return Err(not_json("the x-deal-v0 block is not an object")),
    };
    let typ = string_member(&top, "type")?;
    let chain_id = string_member(&top, "chain_id")?;
    let body = match top.get("body") {
        None | Some(Value::Null) => Map::new(),
        Some(Value::Object(b)) => b.clone(),
        Some(_) => return Err(not_json("body is not an object")),
    };
    let top_refs = refs_of(&top)?;
    let (kind, deal_id, refs) = if let Some(b) = block {
        (
            string_member(b, "record_type")?,
            string_member(b, "deal_id")?,
            refs_of(b)?,
        )
    } else if typ.ends_with("/v0") {
        (typ, chain_id, top_refs)
    } else {
        return Ok(None);
    };
    let mut by_rel: HashMap<String, Vec<String>> = HashMap::new();
    for (rel, d) in refs {
        by_rel.entry(rel).or_default().push(d);
    }
    let class = top.get("counterparty_class");
    let d = digest(&r.payload)?;
    Ok(Some((
        d,
        Rec {
            committed_at: r.committed_at.clone(),
            kind,
            deal_id,
            refs: by_rel,
            body,
            has_class: class.is_some(),
            class: class.and_then(Value::as_str).unwrap_or("").to_string(),
        },
    )))
}

const CLASSES: [&str; 3] = ["person_agent", "business_agent", "shop_preauth"];

fn counterparty_class(records: &[Rec]) -> Option<&str> {
    let mut carriers = records.iter().filter(|r| r.has_class);
    let (c, None) = (carriers.next()?, carriers.next()) else {
        return None;
    };
    if c.kind != TYPE_CHECK || c.str("action") != "pay" || !CLASSES.contains(&c.class.as_str()) {
        return None;
    }
    Some(&c.class)
}

/// One deal's SH §5.2 entry, or `None` for a log that is not an agreed deal
/// (it is not on the card). [`Error::Unpriced`], or any other error, means
/// the card cannot be built. The entry's members are in Go's struct order.
pub fn standard_history_deal(input: &HistoryInput) -> Result<Option<Value>, Error> {
    let mut records: Vec<Rec> = Vec::new();
    let mut by_digest: HashMap<String, usize> = HashMap::new();
    for r in &input.records {
        if let Some((d, rec)) = read_record(r)? {
            by_digest.insert(d, records.len());
            records.push(rec);
        }
    }
    let Some(first) = records.first() else {
        return Ok(None);
    };
    if first.deal_id.starts_with("sale-") || first.kind == TYPE_SALE {
        return Ok(None);
    }
    let Some(agreed) = records.iter().find(|r| {
        (r.kind == TYPE_ACTION || r.kind == TYPE_ACTION_RECORD)
            && matches!(r.str("action"), "commit" | "pay")
    }) else {
        return Ok(None);
    };
    let deal_id = first.deal_id.clone();
    let amount = agreed_amount(agreed, &records, &by_digest).map_err(|e| match e {
        Error::Unpriced(m) => Error::Unpriced(format!("deal {deal_id}: {m}")),
        e => e,
    })?;
    let mut entry = Map::new();
    entry.insert("deal".into(), deal_id.into());
    entry.insert("agreed_at".into(), minute(&agreed.committed_at)?.into());
    if let Some(class) = counterparty_class(&records) {
        entry.insert("counterparty_class".into(), class.into());
    }
    entry.insert("role".into(), deal_role(&records).into());
    entry.insert("amount".into(), amount);
    entry.insert(
        "outcome".into(),
        deal_outcome(&records, &by_digest, &input.results).into(),
    );
    entry.insert(
        "happened".into(),
        happened(&records, &by_digest, input.counterparty_half_verified).into(),
    );
    entry.insert("disputes".into(), json!([]));
    Ok(Some(Value::Object(entry)))
}

/// `at` (RFC 3339) in UTC, floored to the minute.
fn minute(at: &str) -> Result<String, Error> {
    let bad = || Error::Invalid(format!("dealprofile: committed_at {at:?} is not RFC 3339"));
    let b = at.as_bytes();
    let num = |from: usize, to: usize| -> Result<i64, Error> {
        at.get(from..to)
            .filter(|s| s.bytes().all(|c| c.is_ascii_digit()))
            .and_then(|s| s.parse().ok())
            .ok_or_else(bad)
    };
    if b.len() < 20
        || b[4] != b'-'
        || b[7] != b'-'
        || !matches!(b[10], b'T' | b't')
        || b[13] != b':'
        || b[16] != b':'
    {
        return Err(bad());
    }
    let (y, mo, d, h, mi, sec) = (
        num(0, 4)?,
        num(5, 7)?,
        num(8, 10)?,
        num(11, 13)?,
        num(14, 16)?,
        num(17, 19)?,
    );
    let leap = y % 4 == 0 && (y % 100 != 0 || y % 400 == 0);
    let month_days = [
        31,
        if leap { 29 } else { 28 },
        31,
        30,
        31,
        30,
        31,
        31,
        30,
        31,
        30,
        31,
    ];
    if !(1..=12).contains(&mo)
        || d < 1
        || d > month_days[mo as usize - 1]
        || h > 23
        || mi > 59
        || sec > 59
    {
        return Err(bad());
    }
    let mut i = 19;
    if b[i] == b'.' {
        let start = i + 1;
        i = start;
        while i < b.len() && b[i].is_ascii_digit() {
            i += 1;
        }
        if i == start {
            return Err(bad());
        }
    }
    let offset = match b.get(i) {
        Some(b'Z' | b'z') if i + 1 == b.len() => 0,
        Some(sign @ (b'+' | b'-')) if i + 6 == b.len() && b[i + 3] == b':' => {
            let (oh, om) = (num(i + 1, i + 3)?, num(i + 4, i + 6)?);
            if oh > 23 || om > 59 {
                return Err(bad());
            }
            let m = oh * 60 + om;
            if *sign == b'+' {
                m
            } else {
                -m
            }
        }
        _ => return Err(bad()),
    };
    // Days from civil (Howard Hinnant), to subtract the offset exactly.
    let (yy, mm) = if mo <= 2 {
        (y - 1, mo + 9)
    } else {
        (y, mo - 3)
    };
    let era = yy.div_euclid(400);
    let yoe = yy - era * 400;
    let doy = (153 * mm + 2) / 5 + d - 1;
    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
    let days = era * 146097 + doe - 719468;
    let minutes = days * 1440 + h * 60 + mi - offset;
    let (days, rem) = (minutes.div_euclid(1440), minutes.rem_euclid(1440));
    let z = days + 719468;
    let era = z.div_euclid(146097);
    let doe = z - era * 146097;
    let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
    let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
    let mp = (5 * doy + 2) / 153;
    let day = doy - (153 * mp + 2) / 5 + 1;
    let month = if mp < 10 { mp + 3 } else { mp - 9 };
    let year = yoe + era * 400 + i64::from(month <= 2);
    Ok(format!(
        "{year:04}-{month:02}-{day:02}T{:02}:{:02}:00Z",
        rem / 60,
        rem % 60
    ))
}

fn agreed_amount(
    step: &Rec,
    records: &[Rec],
    by_digest: &HashMap<String, usize>,
) -> Result<Value, Error> {
    let get = |d: &str| by_digest.get(d).map(|&i| &records[i]);
    let unpriced = |why: String| Error::Unpriced(format!("{UNPRICED}: {why}"));
    let mut check = None;
    if step.kind == TYPE_ACTION_RECORD {
        let d = step
            .body
            .get("evaluation_ref")
            .and_then(|r| r.get("digest"))
            .and_then(Value::as_str)
            .unwrap_or("");
        if let Some(ev) = get(d).filter(|e| e.kind == TYPE_ACTION_EVALUATION) {
            check =
                get(ev.str("proposed_action_digest")).filter(|c| c.kind == TYPE_PROPOSED_ACTION);
        }
    } else if let Some(ap) =
        get(step.reference("authorized_by")).filter(|a| a.kind == TYPE_APPROVAL)
    {
        if let Some(v) = get(ap.reference("approves")).filter(|v| v.kind == TYPE_VERDICT) {
            check = get(v.reference("checks")).filter(|c| c.kind == TYPE_CHECK);
        }
    }
    let Some(check) = check else {
        return Err(unpriced(format!(
            "the {} step's check is not on its log",
            step.str("action")
        )));
    };
    // Go reads *int64: a whole number only, and within int64.
    let minor = check
        .body
        .get("amount_minor")
        .and_then(Value::as_number)
        .and_then(|n| n.as_i64())
        .filter(|m| *m >= 0);
    let currency = check.str("currency");
    let Some(minor) = minor.filter(|_| !currency.is_empty()) else {
        return Err(unpriced("its check states no amount and currency".into()));
    };
    let Some(scale) = history_asset_scale(currency) else {
        return Err(unpriced(format!(
            "currency {currency:?} has no ISO 4217 minor units"
        )));
    };
    Ok(json!({"value": minor.to_string(), "assetCode": currency, "assetScale": scale}))
}

fn deal_role(records: &[Rec]) -> &'static str {
    for r in records {
        if r.kind == TYPE_BASELINE {
            let seller = r
                .body
                .get("intent")
                .and_then(|i| i.get("party_role"))
                .and_then(Value::as_str)
                == Some("seller");
            return if seller { "provided" } else { "bought" };
        }
    }
    "bought"
}

/// `did_not_hold` when the deal's own log holds the owner's `outcome_note`
/// confirming one of its closes (TEMPORARY, evidencebook#462), else the
/// Result's reading.
fn deal_outcome(
    records: &[Rec],
    by_digest: &HashMap<String, usize>,
    results: &[HistoryResult],
) -> &'static str {
    for r in records {
        if r.kind == TYPE_OUTCOME_NOTE && r.str("outcome") == "did_not_hold" {
            if let Some(&i) = by_digest.get(r.reference("confirms")) {
                if records[i].kind == TYPE_CLOSE {
                    return "did_not_hold";
                }
            }
        }
    }
    reconcile_outcome(results)
}

/// The newest verified Result's claims.
fn reconcile_outcome(results: &[HistoryResult]) -> &'static str {
    let Some(res) = results.iter().rev().find(|r| r.verified) else {
        return "undecided";
    };
    let Ok(Value::Object(doc)) =
        serde_json::from_str::<Value>(&String::from_utf8_lossy(&res.result))
    else {
        return "undecided";
    };
    let Some(Value::Array(claims)) = doc.get("claims") else {
        return "undecided";
    };
    let mut verdicts = Vec::new();
    for c in claims {
        match c {
            Value::Null => verdicts.push(""),
            Value::Object(o) => match o.get("verdict") {
                None | Some(Value::Null) => verdicts.push(""),
                Some(Value::String(s)) => verdicts.push(s.as_str()),
                Some(_) => return "undecided",
            },
            _ => return "undecided",
        }
    }
    if verdicts.is_empty() {
        return "undecided";
    }
    if verdicts.iter().all(|v| *v == "met") {
        "held"
    } else {
        "undecided"
    }
}

/// SH §5.5 over the holder's records: the counterparty's half, else the
/// holder's own final close of a received outcome.
fn happened(
    records: &[Rec],
    by_digest: &HashMap<String, usize>,
    counterparty_half: bool,
) -> &'static str {
    if counterparty_half {
        return "stamped_by_both";
    }
    let last_close = records
        .iter()
        .rfind(|r| r.kind == TYPE_CLOSE && r.str("outcome") != "open");
    if let Some(fin) = last_close {
        if let Some(&i) = by_digest.get(fin.reference("outcome")) {
            let o = &records[i];
            if (o.kind == TYPE_OUTCOME || o.kind == TYPE_ACTION_OUTCOME)
                && o.str("status") == "received"
            {
                return "receipt_only";
            }
        }
    }
    "no_record"
}

/// ISO 4217 codes with minor units other than two.
const MINOR_UNIT_EXCEPTIONS: [(&str, u8); 26] = [
    ("BIF", 0),
    ("CLP", 0),
    ("DJF", 0),
    ("GNF", 0),
    ("ISK", 0),
    ("JPY", 0),
    ("KMF", 0),
    ("KRW", 0),
    ("PYG", 0),
    ("RWF", 0),
    ("UGX", 0),
    ("UYI", 0),
    ("VND", 0),
    ("VUV", 0),
    ("XAF", 0),
    ("XOF", 0),
    ("XPF", 0),
    ("BHD", 3),
    ("IQD", 3),
    ("JOD", 3),
    ("KWD", 3),
    ("LYD", 3),
    ("OMR", 3),
    ("TND", 3),
    ("CLF", 4),
    ("UYW", 4),
];

/// ISO 4217 codes with two minor units, including ANG and BGN, withdrawn for
/// XCG and EUR but still on older deals.
const TWO_DECIMAL_CURRENCIES: &str = " AED AFN ALL AMD ANG AOA ARS AUD AWG AZN BAM BBD BDT BGN BMD BND BOB BOV BRL BSD BTN BWP BYN BZD \
 CAD CDF CHE CHF CHW CNY COP COU CRC CUP CVE CZK DKK DOP DZD EGP ERN ETB EUR FJD FKP GBP GEL GHS GIP GMD GTQ GYD \
 HKD HNL HTG HUF IDR ILS INR IRR JMD KES KGS KHR KPW KYD KZT LAK LBP LKR LRD LSL MAD MDL MGA MKD MMK MNT MOP MRU \
 MUR MVR MWK MXN MXV MYR MZN NAD NGN NIO NOK NPR NZD PAB PEN PGK PHP PKR PLN QAR RON RSD RUB SAR SBD SCR SDG SEK \
 SGD SHP SLE SOS SRD SSP STN SVC SYP SZL THB TJS TMT TOP TRY TTD TWD TZS UAH USD USN UYU UZS VED VES WST XCD XCG \
 YER ZAR ZMW ZWG ";

/// The `assetScale` of a non-ISO 4217 asset whose `amount_minor` the profile
/// defines (PROFILE.md section 3): BTC counts millisatoshi, 10^-11 BTC. A card
/// keeps it as its own currency row.
const HISTORY_ASSET_SCALES: &[(&str, u8)] = &[("BTC", 11)];

/// The `assetScale` of a deal's `amount_minor` in `currency`: the explicit
/// table above, else the ISO 4217 minor units.
pub(crate) fn history_asset_scale(currency: &str) -> Option<u8> {
    HISTORY_ASSET_SCALES
        .iter()
        .find(|(c, _)| *c == currency)
        .map(|(_, s)| *s)
        .or_else(|| minor_units(currency))
}

/// A currency's ISO 4217 minor units: the `assetScale` of an amount in minor
/// units. `None` for a code with none (a metal, a fund code without them) or
/// an unknown one.
pub fn minor_units(currency: &str) -> Option<u8> {
    if let Some((_, s)) = MINOR_UNIT_EXCEPTIONS.iter().find(|(c, _)| *c == currency) {
        return Some(*s);
    }
    (currency.len() == 3 && TWO_DECIMAL_CURRENCIES.contains(&format!(" {currency} "))).then_some(2)
}

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

    #[test]
    fn minute_floors_to_utc() {
        for (at, want) in [
            ("2026-10-06T09:09:59Z", "2026-10-06T09:09:00Z"),
            ("2026-10-06T09:09:59.123456Z", "2026-10-06T09:09:00Z"),
            ("2026-10-06T01:30:00+05:30", "2026-10-05T20:00:00Z"),
            ("2026-10-06T23:30:00-02:00", "2026-10-07T01:30:00Z"),
            ("2024-02-29T00:00:00Z", "2024-02-29T00:00:00Z"),
            ("2026-01-01T00:00:00+01:00", "2025-12-31T23:00:00Z"),
        ] {
            assert_eq!(minute(at).unwrap(), want, "{at}");
        }
    }

    #[test]
    fn minute_refuses_what_is_not_rfc_3339() {
        for at in [
            "",
            "2026-10-06",
            "2026-10-06T09:09Z",
            "2026-10-06T09:09:xxZ",
            "2026-13-01T00:00:00Z",
            "2026-02-30T00:00:00Z",
            "2025-02-29T00:00:00Z",
            "2026-10-06T24:00:00Z",
            "2026-10-06T09:60:00Z",
            "2026-10-06T09:09:60Z",
            "2026-10-06T09:09:00.Z",
            "2026-10-06T09:09:00",
            "2026-10-06T09:09:00+24:00",
            "2026-10-06T09:09:00+01",
            "2026-10-06T09:09:00Zjunk",
        ] {
            assert!(minute(at).is_err(), "{at}");
        }
    }
}