doiget-core 0.8.13

Core library: Source/Store traits, CapabilityProfile, safekey, provenance log
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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
//! Bibliography input adapters per ADR-0030.
//!
//! Parses three input shapes into an iterator of `Ref`s with optional
//! `entry_key` provenance back to the source bibliography:
//!
//! - **Plain refs**: one `doi:…` / `arxiv:…` / bare-DOI / bare-arXiv id
//!   per line, with `#`-prefixed comments and blank lines tolerated.
//!   The existing `doiget batch <refs.txt>` shape.
//! - **CSL-JSON**: a JSON array of entries with `id` (citation key),
//!   `DOI`, and optionally `archivePrefix = "arXiv"` + `eprint`
//!   fields. Parsed via the workspace's existing `serde_json` — no
//!   new dependency.
//! - **BibTeX / BibLaTeX (.bib)**: parsed via the `biblatex` crate
//!   (ADR-0030 D2). One `@entrytype{KEY, …}` per entry; the `doi`
//!   field is preferred, falling back to an arXiv `eprint`.
//!
//! Identifier-pick priority per ADR-0030 D3: `doi` > `arxiv` > `pmid`
//! (PMID adapter parking until the `Ref::Pmid` variant lands in a
//! later slice; current code carries the rule through without
//! producing a `Pmid` ref).
//!
//! Parse-error policy per ADR-0030 D5: a single entry's failure is
//! captured per-entry and does NOT abort the whole batch. The caller
//! decides whether to skip-and-warn (default) or fail-closed
//! (`--strict`).

use biblatex::{Bibliography, ChunksExt};
use camino::Utf8Path;
use thiserror::Error;

use crate::{Ref, RefParseError};

/// One successfully-parsed bibliography entry.
///
/// `entry_key` echoes the source bibliography's citation key
/// (BibTeX `@article{KEY,…}` / CSL-JSON `"id"`) so downstream
/// automation can bridge the fetch outcome back to the originating
/// reference — the load-bearing field for the Zotero / Mendeley
/// "attach fetched PDF to this reference" workflow per ADR-0030 §6.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct ParsedEntry {
    /// The identifier the adapter chose for this entry (`Ref::Doi` /
    /// `Ref::Arxiv`).
    pub ref_: Ref,
    /// The source bibliography's citation key, when one is available.
    /// `None` for plain-refs input (no key concept) and for any
    /// future input shape that lacks per-entry keys.
    pub entry_key: Option<String>,
}

/// Why a single bibliography entry failed to produce a `Ref`.
///
/// Closed-enum so the failure-class can be exposed at the
/// `docs/ERRORS.md` §3 INVALID_REF surface without leaking parser
/// internals.
#[derive(Debug, Clone, Error, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParseError {
    /// The line did not contain a `doi:` / `arxiv:` / bare-DOI /
    /// bare-arXiv id — empty (after trimming) or just a comment.
    /// Plain-refs path filters these out silently; CSL-JSON path
    /// emits this when an entry has no resolvable identifier.
    #[error("entry has no DOI / arXiv id (entry_key={entry_key:?})")]
    NoIdentifier {
        /// The source bibliography's citation key, when known.
        entry_key: Option<String>,
    },
    /// The entry DOES carry an identifier, and it is one doiget recognises
    /// and cannot resolve yet (#500).
    ///
    /// Distinct from [`Self::NoIdentifier`] because the two send a reader in
    /// opposite directions. "entry has no DOI / arXiv id" is accurate about
    /// what the parser did and wrong about the entry: a PubMed-exported
    /// `.bib` record carrying `pmid = {9659853}` is not deficient, and a user
    /// who believes it is will go and edit a bibliography that was fine. The
    /// missing piece is on doiget's side.
    ///
    /// Surfaces as `NOT_IMPLEMENTED` rather than `INVALID_REF`: the input is
    /// valid and the support is absent, and the two carry different advice --
    /// "wait for a release" versus "correct your input" (ADR-0055).
    #[error("entry {entry_key:?} is identified only by {kind} {value:?}, which doiget cannot resolve yet (issue #500) -- it is NOT missing an identifier")]
    UnsupportedIdentifier {
        /// Human-facing name of the identifier class, e.g. `"PMID"`.
        kind: &'static str,
        /// The identifier as written in the entry.
        value: String,
        /// The source bibliography's citation key, when known.
        entry_key: Option<String>,
    },
    /// The identifier was present but `Ref::parse` rejected it
    /// (malformed DOI suffix, invalid arXiv id shape, etc.).
    #[error(
        "entry identifier {raw:?} did not parse as a Ref \
         (entry_key={entry_key:?}): {source}"
    )]
    InvalidRef {
        /// The raw identifier string the parser saw.
        raw: String,
        /// The source bibliography's citation key, when known.
        entry_key: Option<String>,
        /// The structured `Ref::parse` failure.
        #[source]
        source: RefParseError,
    },
    /// The whole input did not deserialise — CSL-JSON that is not a
    /// JSON array, top-level malformed JSON, etc. This is a
    /// whole-input failure, not a per-entry failure; callers receive
    /// it as the sole `Err` element of the result iterator.
    #[error("input did not deserialise as {format}: {message}")]
    Decode {
        /// Which parser branch produced the failure (`"csl-json"` /
        /// `"bibtex"`).
        format: &'static str,
        /// `serde_json::Error::to_string()`.
        message: String,
    },
    /// Format requested or detected, but no parser for it is shipped.
    /// Retained as a forward-compatible variant for input shapes not
    /// yet implemented (e.g. a future RIS adapter); the `bibtex` and
    /// `csl-json` paths are both live today.
    #[error("{format} parsing is not yet implemented")]
    UnsupportedFormat {
        /// The format token naming the unsupported shape.
        format: &'static str,
    },
}

/// The claim [`ParseError::UnsupportedIdentifier`] makes, without the
/// `entry {entry_key:?}` prefix its `Display` carries.
///
/// Callers that put `entry_key` in a field of its own -- the CLI `verify`
/// row and the MCP `batch_from_bibliography` envelope both do -- would
/// otherwise say it twice. One definition rather than a copy at each site,
/// because the copies drifted: two of the three carried a run of joined-line
/// whitespace into user-facing output before anything asserted the text.
#[must_use]
pub fn unsupported_identifier_claim(kind: &str, value: &str) -> String {
    format!("entry is identified only by {kind} {value:?}, which doiget cannot resolve yet (issue #500); it is NOT missing an identifier")
}

/// Input-shape discriminator per ADR-0030 D4.
///
/// `Auto` means "detect from path extension and/or content
/// fingerprint"; the explicit variants name a parser directly and
/// skip detection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Format {
    /// Detect from file extension if a path was supplied, else from
    /// content fingerprint; fall through to [`Format::Refs`].
    Auto,
    /// Plain refs — one identifier per line, `#` comments, blanks.
    Refs,
    /// CSL-JSON array per <https://citationstyles.org/>.
    CslJson,
    /// BibTeX / BibLaTeX, parsed via the `biblatex` crate (ADR-0030 D2).
    Bibtex,
}

impl Format {
    /// Wire token used by the CLI `--format` flag and the MCP tool
    /// input schema's `format` field per ADR-0030 §6.
    pub fn as_wire(&self) -> &'static str {
        match self {
            Format::Auto => "auto",
            Format::Refs => "refs",
            Format::CslJson => "csl-json",
            Format::Bibtex => "bibtex",
        }
    }
}

/// Detect the input format per ADR-0030 D4.
///
/// Precedence: file extension first (when `path` is `Some`), then
/// content fingerprint, then fallback to [`Format::Refs`]. The
/// caller's explicit `--format` flag should short-circuit this
/// function — it is the slowest of the three precedence rules in the
/// ADR.
pub fn detect_format(path: Option<&Utf8Path>, content: &str) -> Format {
    if let Some(p) = path {
        let ext = p.extension().unwrap_or_default().to_ascii_lowercase();
        match ext.as_str() {
            "bib" | "biblatex" => return Format::Bibtex,
            "json" | "csl" => return Format::CslJson,
            _ => {}
        }
    }
    // Content fingerprint: peek the first non-blank, non-comment line.
    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.is_empty() || trimmed.starts_with('#') {
            continue;
        }
        if trimmed.starts_with('@') {
            return Format::Bibtex;
        }
        if trimmed.starts_with('[') || trimmed.starts_with('{') {
            return Format::CslJson;
        }
        break;
    }
    Format::Refs
}

/// Parse `text` per `format`, dispatching to the matching shape
/// parser. `path` is consulted only when `format == Format::Auto` to
/// drive [`detect_format`].
///
/// Returns one element per discovered entry — `Ok` for entries that
/// produced a `Ref`, `Err` for per-entry failures the caller should
/// surface as a JSONL `INVALID_REF` line. A whole-input decode
/// failure ([`ParseError::Decode`]) is returned as a single-element
/// `Err` so the caller's exit-code path treats it as one parse error
/// rather than zero.
pub fn parse_input(
    text: &str,
    format: Format,
    path: Option<&Utf8Path>,
) -> Vec<Result<ParsedEntry, ParseError>> {
    let resolved = match format {
        Format::Auto => detect_format(path, text),
        other => other,
    };
    match resolved {
        Format::Refs | Format::Auto => parse_plain_refs(text),
        Format::CslJson => parse_csl_json(text),
        Format::Bibtex => parse_bibtex(text),
    }
}

/// Parse plain refs — the existing batch input format. One ref per
/// non-blank, non-comment line. `entry_key` is always `None` for this
/// shape; plain refs have no citation-key concept.
pub fn parse_plain_refs(text: &str) -> Vec<Result<ParsedEntry, ParseError>> {
    let mut out = Vec::new();
    for raw_line in text.lines() {
        let line = raw_line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        out.push(match Ref::parse(line) {
            Ok(ref_) => Ok(ParsedEntry {
                ref_,
                entry_key: None,
            }),
            Err(e) => Err(ParseError::InvalidRef {
                raw: line.to_string(),
                entry_key: None,
                source: e,
            }),
        });
    }
    out
}

/// Parse a CSL-JSON document — a JSON array of objects, each with at
/// least an `id` (citation key) and one of `DOI`, or `archivePrefix`
/// + `eprint` (arXiv).
///
/// Identifier-pick priority per ADR-0030 D3:
///
/// 1. `DOI` field (case-sensitive per the CSL-JSON spec but Zotero
///    sometimes emits `doi` lowercase — we accept both).
/// 2. `archivePrefix == "arXiv"` (case-insensitive) + `eprint`
///    (or `note: "arXiv:..."` shape Zotero emits).
/// 3. (PMID parking — `Ref::Pmid` not yet defined; PMIDs in CSL-JSON
///    are recorded as parse failures with `NoIdentifier` until the
///    variant lands.)
///
/// `entry_key` is the `id` field verbatim.
pub fn parse_csl_json(text: &str) -> Vec<Result<ParsedEntry, ParseError>> {
    let parsed: serde_json::Result<Vec<serde_json::Value>> = serde_json::from_str(text);
    let entries = match parsed {
        Ok(arr) => arr,
        Err(e) => {
            return vec![Err(ParseError::Decode {
                format: "csl-json",
                message: e.to_string(),
            })]
        }
    };
    let mut out = Vec::with_capacity(entries.len());
    for entry in entries {
        // `id` is usually a string in real-world Zotero exports but
        // the spec allows numeric ids too — stringify either form so
        // the operator can find the entry in their library.
        let entry_key = entry.get("id").and_then(|v| {
            if let Some(s) = v.as_str() {
                Some(s.to_string())
            } else if v.is_number() {
                Some(v.to_string())
            } else {
                None
            }
        });
        out.push(parse_csl_entry(&entry, entry_key));
    }
    out
}

/// Pick the highest-priority identifier on a single CSL-JSON entry
/// and parse it. Honors ADR-0030 D3 priority.
fn parse_csl_entry(
    entry: &serde_json::Value,
    entry_key: Option<String>,
) -> Result<ParsedEntry, ParseError> {
    // Priority 1: DOI (both `DOI` per spec and `doi` lowercase per
    // real-world exports). Zotero emits uppercase; Mendeley sometimes
    // lowercase.
    if let Some(doi) = entry
        .get("DOI")
        .or_else(|| entry.get("doi"))
        .and_then(|v| v.as_str())
    {
        let raw = doi.trim();
        if !raw.is_empty() {
            return match Ref::parse(raw) {
                Ok(ref_) => Ok(ParsedEntry { ref_, entry_key }),
                Err(e) => Err(ParseError::InvalidRef {
                    raw: raw.to_string(),
                    entry_key,
                    source: e,
                }),
            };
        }
    }
    // Priority 2: arXiv — `archivePrefix == "arXiv"` (CSL extension)
    // OR the Zotero-specific `note: "arXiv:..."` shape.
    let is_arxiv = entry
        .get("archivePrefix")
        .or_else(|| entry.get("archive_prefix"))
        .and_then(|v| v.as_str())
        .map(|s| s.eq_ignore_ascii_case("arxiv"))
        .unwrap_or(false);
    if is_arxiv {
        if let Some(eprint) = entry.get("eprint").and_then(|v| v.as_str()) {
            let raw = eprint.trim();
            if !raw.is_empty() {
                let with_scheme = if raw.to_ascii_lowercase().starts_with("arxiv:") {
                    raw.to_string()
                } else {
                    format!("arxiv:{raw}")
                };
                return match Ref::parse(&with_scheme) {
                    Ok(ref_) => Ok(ParsedEntry { ref_, entry_key }),
                    Err(e) => Err(ParseError::InvalidRef {
                        raw: with_scheme,
                        entry_key,
                        source: e,
                    }),
                };
            }
        }
    }
    // Fallback: scan `note` for an embedded `arXiv:NNNN.NNNNN` —
    // Zotero often stores the arXiv id there instead of a typed
    // field. The pattern is intentionally narrow (must follow the
    // canonical "arXiv:" prefix); free-text DOIs in notes are NOT
    // mined here.
    if let Some(note) = entry.get("note").and_then(|v| v.as_str()) {
        if let Some(idx) = note.to_ascii_lowercase().find("arxiv:") {
            let tail = &note[idx + "arxiv:".len()..];
            // Take chars matching the arXiv id alphabet (digits / dot /
            // slash / letters / hyphen) — stop at the first separator
            // so the rest of the note is ignored.
            let id: String = tail
                .chars()
                .take_while(|c| matches!(c, '0'..='9' | '.' | '/' | 'a'..='z' | 'A'..='Z' | '-'))
                .collect();
            if !id.is_empty() {
                let with_scheme = format!("arxiv:{id}");
                return match Ref::parse(&with_scheme) {
                    Ok(ref_) => Ok(ParsedEntry { ref_, entry_key }),
                    Err(e) => Err(ParseError::InvalidRef {
                        raw: with_scheme,
                        entry_key,
                        source: e,
                    }),
                };
            }
        }
    }
    // #500, CSL-JSON half. The BibTeX parser learned to say "this entry HAS an
    // identifier I cannot use" and this one did not, so the same PubMed record
    // exported as CSL-JSON still got "entry has no DOI / arXiv id" -- the claim
    // #500 exists to stop doiget making, surviving in the format a Zotero user
    // is most likely to hand it.
    if let Some((kind, value)) = csl_unsupported_identifier(entry) {
        return Err(ParseError::UnsupportedIdentifier {
            kind,
            value,
            entry_key,
        });
    }
    Err(ParseError::NoIdentifier { entry_key })
}

/// The CSL-JSON counterpart of [`unsupported_identifier`]: an identifier
/// doiget recognises and cannot resolve yet (#500).
///
/// CSL-JSON has no standard PMID field, so exporters improvise. Zotero writes
/// `PMID: 9659853` into `note`; some tools emit a top-level `PMID` key. Both
/// are checked, and the note scan mirrors the arXiv one directly above it.
fn csl_unsupported_identifier(entry: &serde_json::Value) -> Option<(&'static str, String)> {
    let direct = |name: &str| -> Option<String> {
        let v = entry.get(name)?;
        let s = match v {
            serde_json::Value::String(s) => s.trim().to_string(),
            serde_json::Value::Number(n) => n.to_string(),
            _ => return None,
        };
        (!s.is_empty()).then_some(s)
    };
    for (field, kind) in [
        ("PMID", "PMID"),
        ("pmid", "PMID"),
        ("PMCID", "PMCID"),
        ("pmcid", "PMCID"),
    ] {
        if let Some(v) = direct(field) {
            return Some((kind, v));
        }
    }

    // Zotero's `note` carries `PMID: 9659853` / `PMCID: PMC1234567`.
    let note = entry.get("note").and_then(|v| v.as_str())?;
    for (needle, kind) in [("pmcid:", "PMCID"), ("pmid:", "PMID")] {
        let lower = note.to_ascii_lowercase();
        if let Some(at) = lower.find(needle) {
            let value: String = note[at + needle.len()..]
                .trim_start()
                .chars()
                .take_while(|c| c.is_ascii_alphanumeric())
                .collect();
            if !value.is_empty() {
                return Some((kind, value));
            }
        }
    }
    None
}

/// Parse a BibTeX / BibLaTeX document via the `biblatex` crate
/// (ADR-0030 D2). One `@entrytype{KEY, …}` produces one entry;
/// `entry_key` is the citation key verbatim.
///
/// A whole-input parse failure (malformed BibTeX the `biblatex` crate
/// rejects) is returned as a single-element [`ParseError::Decode`] so
/// the caller's exit-code path counts it as one error rather than
/// zero — matching the CSL-JSON behaviour.
///
/// Identifier-pick priority per ADR-0030 D3: `doi` field, then
/// `eprint` (arXiv). See `parse_bibtex_entry`.
pub fn parse_bibtex(text: &str) -> Vec<Result<ParsedEntry, ParseError>> {
    let bib = match Bibliography::parse(text) {
        Ok(b) => b,
        Err(e) => {
            return vec![Err(ParseError::Decode {
                format: "bibtex",
                message: e.to_string(),
            })]
        }
    };
    bib.iter()
        .map(|entry| parse_bibtex_entry(entry, Some(entry.key.clone())))
        .collect()
}

/// Pick the highest-priority identifier on a single BibTeX entry and
/// parse it. Honors ADR-0030 D3 priority (`doi` > arXiv `eprint`).
fn parse_bibtex_entry(
    entry: &biblatex::Entry,
    entry_key: Option<String>,
) -> Result<ParsedEntry, ParseError> {
    // Priority 1: `doi` field. The typed accessor formats the chunk
    // value to a `String`; `Err` means the field is absent or not a
    // plain string, which we treat as "no DOI here" and fall through.
    if let Ok(doi) = entry.doi() {
        let raw = doi.trim();
        if !raw.is_empty() {
            return match Ref::parse(raw) {
                Ok(ref_) => Ok(ParsedEntry { ref_, entry_key }),
                Err(e) => Err(ParseError::InvalidRef {
                    raw: raw.to_string(),
                    entry_key,
                    source: e,
                }),
            };
        }
    }
    // Priority 2: arXiv via the `eprint` field. The BibTeX convention
    // is `eprint = {2204.12345}` with `archivePrefix = {arXiv}` (or the
    // BibLaTeX `eprinttype = {arxiv}`). We accept the eprint as an arXiv
    // id when the prefix names arXiv OR is absent (the dominant
    // single-preprint-server convention); a prefix that names something
    // else (e.g. `eprinttype = {pubmed}`) is skipped rather than
    // parsed incorrectly.
    if let Ok(eprint) = entry.eprint() {
        let raw = eprint.trim();
        if !raw.is_empty() && arxiv_eligible(entry) {
            let with_scheme = if raw.to_ascii_lowercase().starts_with("arxiv:") {
                raw.to_string()
            } else {
                format!("arxiv:{raw}")
            };
            return match Ref::parse(&with_scheme) {
                Ok(ref_) => Ok(ParsedEntry { ref_, entry_key }),
                Err(e) => Err(ParseError::InvalidRef {
                    raw: with_scheme,
                    entry_key,
                    source: e,
                }),
            };
        }
    }
    // #500: before reporting "no identifier", check for one doiget simply
    // does not support. Saying "no DOI / arXiv id" about an entry that
    // carries a PMID is accurate about the parser and wrong about the entry.
    if let Some((kind, value)) = unsupported_identifier(entry) {
        return Err(ParseError::UnsupportedIdentifier {
            kind,
            value,
            entry_key,
        });
    }
    Err(ParseError::NoIdentifier { entry_key })
}

/// An identifier doiget recognises but cannot resolve yet (#500).
///
/// Only classes doiget can *name*. An entry carrying some field this does not
/// know about still reports [`ParseError::NoIdentifier`], which stays correct
/// for it: the point is not to guess, it is to stop saying "no identifier"
/// about the cases where there demonstrably is one.
///
/// `pmid = {...}` is what PubMed's own BibTeX export writes. The BibLaTeX
/// shape is `eprint = {...}` with `eprinttype = {pubmed}`, which
/// [`arxiv_eligible`] already refuses -- correctly, and until now silently.
fn unsupported_identifier(entry: &biblatex::Entry) -> Option<(&'static str, String)> {
    let field = |name: &str| -> Option<String> {
        let v = entry.get(name)?.format_verbatim().trim().to_string();
        (!v.is_empty()).then_some(v)
    };

    if let Some(v) = field("pmid") {
        return Some(("PMID", v));
    }
    if let Some(v) = field("pmcid") {
        return Some(("PMCID", v));
    }
    let names_pubmed = entry
        .get("archiveprefix")
        .or_else(|| entry.get("eprinttype"))
        .is_some_and(|c| c.format_verbatim().to_ascii_lowercase().contains("pubmed"));
    if names_pubmed {
        if let Some(v) = field("eprint") {
            return Some(("PMID", v));
        }
    }
    None
}

/// Whether an `eprint` field should be interpreted as an arXiv id.
/// True when `archivePrefix` / `eprinttype` names arXiv (case-
/// insensitive) or is absent; false when it explicitly names a
/// different preprint server.
fn arxiv_eligible(entry: &biblatex::Entry) -> bool {
    match entry
        .get("archiveprefix")
        .or_else(|| entry.get("eprinttype"))
    {
        Some(chunks) => chunks
            .format_verbatim()
            .to_ascii_lowercase()
            .contains("arxiv"),
        None => true,
    }
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
mod tests {
    /// #500, CSL-JSON. The BibTeX parser learned to distinguish "has no
    /// identifier" from "has one I cannot use"; this format did not, so the
    /// same PubMed record exported from Zotero still got the claim #500 exists
    /// to prevent -- in the format a Zotero user is most likely to hand over.
    #[test]
    fn a_csl_entry_identified_only_by_a_pmid_is_not_missing_an_identifier() {
        let doc = r#"[{"id":"Smith2020","type":"article-journal","PMID":"9659853"}]"#;
        let out = parse_csl_json(doc);
        assert_eq!(out.len(), 1);
        match &out[0] {
            Err(ParseError::UnsupportedIdentifier { kind, value, .. }) => {
                assert_eq!(*kind, "PMID");
                assert_eq!(value, "9659853");
            }
            other => panic!("expected UnsupportedIdentifier, got {other:?}"),
        }
    }

    /// Zotero does not emit a top-level `PMID`; it writes it into `note`.
    /// A checker that only looked at the field would have missed the exporter
    /// that produces most of these files.
    #[test]
    fn a_csl_note_carrying_a_pmid_is_read_the_same_way() {
        let doc = r#"[{"id":"S","type":"article-journal","note":"PMID: 9659853; see PubMed"}]"#;
        match &parse_csl_json(doc)[0] {
            Err(ParseError::UnsupportedIdentifier { kind, value, .. }) => {
                assert_eq!(*kind, "PMID");
                assert_eq!(value, "9659853");
            }
            other => panic!("expected UnsupportedIdentifier, got {other:?}"),
        }
    }

    /// PMCID is the other half of the check and was written without a test.
    /// Both the direct field and the Zotero `note` form.
    #[test]
    fn a_csl_entry_identified_only_by_a_pmcid_is_read_as_pmcid() {
        for doc in [
            r#"[{"id":"S","type":"article-journal","PMCID":"PMC1234567"}]"#,
            r#"[{"id":"S","type":"article-journal","pmcid":"PMC1234567"}]"#,
            r#"[{"id":"S","type":"article-journal","note":"PMCID: PMC1234567"}]"#,
        ] {
            match &parse_csl_json(doc)[0] {
                Err(ParseError::UnsupportedIdentifier { kind, value, .. }) => {
                    assert_eq!(*kind, "PMCID", "for {doc}");
                    assert_eq!(value, "PMC1234567", "for {doc}");
                }
                other => panic!("expected UnsupportedIdentifier for {doc}, got {other:?}"),
            }
        }
    }

    /// A numeric PMID must not be coerced into anything, and a DOI alongside
    /// one still wins: the check runs only after every supported identifier
    /// has been tried.
    #[test]
    fn a_csl_doi_still_wins_over_a_pmid() {
        let doc = r#"[{"id":"S","type":"article-journal","DOI":"10.1234/x","PMID":9659853}]"#;
        let parsed = parse_csl_json(doc);
        let entry = parsed[0].as_ref().expect("the DOI must still win");
        assert_eq!(entry.ref_.as_input_str(), "10.1234/x");
    }

    /// An entry with genuinely nothing must keep saying so -- the new check
    /// must not turn every unidentifiable entry into "unsupported".
    #[test]
    fn a_csl_entry_with_no_identifier_at_all_still_says_so() {
        let doc = r#"[{"id":"S","type":"article-journal","title":"No ids here"}]"#;
        assert!(matches!(
            &parse_csl_json(doc)[0],
            Err(ParseError::NoIdentifier { .. })
        ));
    }

    use super::*;

    // ---- detect_format ---------------------------------------------

    /// #500: the entry from PubMed's own BibTeX export. It carries `pmid`,
    /// and reporting "entry has no DOI / arXiv id" about it is accurate about
    /// the parser and wrong about the entry -- a user who believes it goes and
    /// edits a bibliography that was fine.
    ///
    /// The PMID is real: `9659853` is Coryell 1998, whose DOI
    /// `10.1176/ajp.155.7.895` NCBI's own esummary returns for it.
    #[test]
    fn a_pubmed_only_entry_says_it_has_a_pmid_not_that_it_has_nothing() {
        let bib = r#"@article{coryell1998,
  title = {Lithium discontinuation and subsequent effectiveness},
  author = {Coryell, William},
  year = {1998},
  pmid = {9659853},
}"#;
        let out = parse_bibtex(bib);
        assert_eq!(out.len(), 1);
        match &out[0] {
            Err(ParseError::UnsupportedIdentifier {
                kind,
                value,
                entry_key,
            }) => {
                assert_eq!(kind, &"PMID");
                assert_eq!(value, "9659853");
                assert_eq!(entry_key.as_deref(), Some("coryell1998"));
                let msg = out[0].as_ref().unwrap_err().to_string();
                assert!(
                    msg.contains("NOT missing an identifier"),
                    "the message has to contradict the wrong conclusion explicitly, or the reader draws it anyway: {msg}"
                );
            }
            other => panic!("expected UnsupportedIdentifier, got {other:?}"),
        }
    }

    /// The BibLaTeX shape. `arxiv_eligible` already refused this -- correctly,
    /// and until now silently, which is the whole complaint.
    #[test]
    fn the_biblatex_eprinttype_pubmed_shape_is_recognised_too() {
        let bib = r#"@article{e,
  title = {T},
  eprint = {9659853},
  eprinttype = {pubmed},
}"#;
        let out = parse_bibtex(bib);
        assert!(
            matches!(
                &out[0],
                Err(ParseError::UnsupportedIdentifier { kind: "PMID", .. })
            ),
            "got {:?}",
            out[0]
        );
    }

    /// An entry with genuinely nothing still reports `NoIdentifier`. The point
    /// is not to relabel every failure -- it is to stop saying "no identifier"
    /// about the cases where there demonstrably is one.
    #[test]
    fn an_entry_with_no_identifier_at_all_is_unchanged() {
        let bib = "@article{x,
  title = {T},
  year = {2020},
}";
        let out = parse_bibtex(bib);
        assert!(
            matches!(&out[0], Err(ParseError::NoIdentifier { .. })),
            "got {:?}",
            out[0]
        );
    }

    /// A DOI still wins. The new check runs only after every supported
    /// identifier has been tried, so adding it cannot divert an entry doiget
    /// could actually have resolved.
    #[test]
    fn a_doi_alongside_a_pmid_still_resolves() {
        let bib = r#"@article{both,
  title = {T},
  doi = {10.1176/ajp.155.7.895},
  pmid = {9659853},
}"#;
        let out = parse_bibtex(bib);
        let parsed = out[0].as_ref().expect("the DOI must still win");
        assert_eq!(parsed.ref_.as_input_str(), "10.1176/ajp.155.7.895");
    }

    #[test]
    fn detect_by_bib_extension() {
        let p = Utf8Path::new("/tmp/library.bib");
        assert_eq!(detect_format(Some(p), ""), Format::Bibtex);
    }

    #[test]
    fn detect_by_json_extension() {
        let p = Utf8Path::new("/tmp/library.json");
        assert_eq!(detect_format(Some(p), ""), Format::CslJson);
    }

    #[test]
    fn detect_by_csl_extension() {
        let p = Utf8Path::new("/tmp/library.csl");
        assert_eq!(detect_format(Some(p), ""), Format::CslJson);
    }

    #[test]
    fn detect_by_fingerprint_bibtex_at_sign() {
        let body = "# comment\n\n@article{foo,\n  doi = {10.1/x}\n}\n";
        assert_eq!(detect_format(None, body), Format::Bibtex);
    }

    #[test]
    fn detect_by_fingerprint_csl_json_array() {
        let body = "[{\"id\":\"foo\",\"DOI\":\"10.1/x\"}]";
        assert_eq!(detect_format(None, body), Format::CslJson);
    }

    #[test]
    fn detect_by_fingerprint_falls_through_to_refs() {
        let body = "doi:10.1234/foo\narxiv:2401.12345\n";
        assert_eq!(detect_format(None, body), Format::Refs);
    }

    // ---- plain refs ------------------------------------------------

    #[test]
    fn plain_refs_parses_mix_with_comments_and_blanks() {
        let body = "\
# header comment
doi:10.1234/foo

   arxiv:2401.12345
# trailing comment
";
        let parsed = parse_plain_refs(body);
        assert_eq!(parsed.len(), 2);
        let okays: Vec<_> = parsed.into_iter().filter_map(Result::ok).collect();
        assert!(matches!(okays[0].ref_, Ref::Doi(_)));
        assert!(matches!(okays[1].ref_, Ref::Arxiv(_)));
        assert!(okays.iter().all(|e| e.entry_key.is_none()));
    }

    #[test]
    fn plain_refs_surface_per_line_invalid_refs() {
        let body = "doi:10.1234/foo\nnot-a-ref\narxiv:2401.12345\n";
        let parsed = parse_plain_refs(body);
        assert_eq!(parsed.len(), 3);
        assert!(parsed[0].is_ok());
        assert!(matches!(parsed[1], Err(ParseError::InvalidRef { .. })));
        assert!(parsed[2].is_ok());
    }

    // ---- CSL-JSON --------------------------------------------------

    #[test]
    fn csl_json_picks_doi_when_present() {
        let body = r#"[{"id":"foo2024","DOI":"10.1234/foo"}]"#;
        let parsed = parse_csl_json(body);
        assert_eq!(parsed.len(), 1);
        let entry = parsed.into_iter().next().unwrap().expect("entry parses");
        assert!(matches!(entry.ref_, Ref::Doi(_)));
        assert_eq!(entry.entry_key.as_deref(), Some("foo2024"));
    }

    #[test]
    fn csl_json_accepts_lowercase_doi_field() {
        // Mendeley exports sometimes lowercase the field name.
        let body = r#"[{"id":"x","doi":"10.5555/bar"}]"#;
        let parsed = parse_csl_json(body);
        let entry = parsed.into_iter().next().unwrap().expect("entry parses");
        assert!(matches!(entry.ref_, Ref::Doi(_)));
    }

    #[test]
    fn csl_json_picks_arxiv_via_archive_prefix_and_eprint() {
        let body = r#"[{"id":"arx","archivePrefix":"arXiv","eprint":"2401.12345"}]"#;
        let parsed = parse_csl_json(body);
        let entry = parsed.into_iter().next().unwrap().expect("entry parses");
        assert!(matches!(entry.ref_, Ref::Arxiv(_)));
    }

    #[test]
    fn csl_json_arxiv_archive_prefix_is_case_insensitive() {
        let body = r#"[{"id":"arx","archivePrefix":"ARXIV","eprint":"2401.12345"}]"#;
        let parsed = parse_csl_json(body);
        let entry = parsed.into_iter().next().unwrap().expect("entry parses");
        assert!(matches!(entry.ref_, Ref::Arxiv(_)));
    }

    #[test]
    fn csl_json_doi_beats_arxiv_when_both_present() {
        // ADR-0030 D3: priority is DOI > arXiv > PMID.
        let body = r#"[{
            "id":"both",
            "DOI":"10.1234/foo",
            "archivePrefix":"arXiv",
            "eprint":"2401.12345"
        }]"#;
        let parsed = parse_csl_json(body);
        let entry = parsed.into_iter().next().unwrap().expect("entry parses");
        assert!(matches!(entry.ref_, Ref::Doi(_)));
    }

    #[test]
    fn csl_json_arxiv_from_note_field() {
        // Zotero often dumps "arXiv:NNNN.NNNNN" into the note field
        // instead of a typed field.
        let body = r#"[{"id":"znote","note":"Comment: 12 pages. arXiv:2401.12345"}]"#;
        let parsed = parse_csl_json(body);
        let entry = parsed.into_iter().next().unwrap().expect("entry parses");
        assert!(matches!(entry.ref_, Ref::Arxiv(_)));
    }

    #[test]
    fn csl_json_entry_without_any_identifier_yields_no_identifier_error() {
        let body = r#"[{"id":"empty","title":"no ids here"}]"#;
        let parsed = parse_csl_json(body);
        assert!(matches!(
            parsed.into_iter().next().unwrap(),
            Err(ParseError::NoIdentifier { .. })
        ));
    }

    #[test]
    fn csl_json_invalid_doi_surface_as_invalid_ref_per_entry() {
        let body = r#"[{"id":"bad","DOI":"not-a-doi"}]"#;
        let parsed = parse_csl_json(body);
        match &parsed[0] {
            Err(ParseError::InvalidRef { raw, entry_key, .. }) => {
                assert_eq!(raw, "not-a-doi");
                assert_eq!(entry_key.as_deref(), Some("bad"));
            }
            other => panic!("expected InvalidRef, got {other:?}"),
        }
    }

    #[test]
    fn csl_json_top_level_malformed_yields_single_decode_error() {
        let body = "{this is not JSON}";
        let parsed = parse_csl_json(body);
        assert_eq!(parsed.len(), 1);
        assert!(matches!(
            parsed[0],
            Err(ParseError::Decode {
                format: "csl-json",
                ..
            })
        ));
    }

    #[test]
    fn csl_json_non_array_top_level_yields_decode_error() {
        // A single-entry object (not an array) is not a valid CSL-JSON
        // document by the spec — the top level MUST be an array even
        // for a single entry.
        let body = r#"{"id":"x","DOI":"10.1/x"}"#;
        let parsed = parse_csl_json(body);
        assert!(matches!(
            parsed[0],
            Err(ParseError::Decode {
                format: "csl-json",
                ..
            })
        ));
    }

    // ---- parse_input dispatch -------------------------------------

    #[test]
    fn parse_input_auto_dispatches_csl_json_by_content() {
        let body = r#"[{"id":"foo","DOI":"10.1234/foo"}]"#;
        let parsed = parse_input(body, Format::Auto, None);
        assert_eq!(parsed.len(), 1);
        assert!(matches!(
            parsed[0],
            Ok(ParsedEntry {
                ref_: Ref::Doi(_),
                ..
            })
        ));
    }

    #[test]
    fn parse_input_auto_dispatches_refs_by_content() {
        let body = "doi:10.1234/foo\n";
        let parsed = parse_input(body, Format::Auto, None);
        assert_eq!(parsed.len(), 1);
        assert!(matches!(
            parsed[0],
            Ok(ParsedEntry {
                ref_: Ref::Doi(_),
                ..
            })
        ));
    }

    // ---- BibTeX parsing (ADR-0030 D2) -----------------------------

    #[test]
    fn bibtex_picks_doi_and_preserves_key() {
        let body = r#"@article{Onsager1944,
            author = {Onsager, Lars},
            title  = {Crystal Statistics},
            doi    = {10.1103/PhysRev.65.117}
        }"#;
        let parsed = parse_bibtex(body);
        assert_eq!(parsed.len(), 1);
        let entry = parsed.into_iter().next().unwrap().expect("entry parses");
        assert!(matches!(entry.ref_, Ref::Doi(_)));
        assert_eq!(entry.entry_key.as_deref(), Some("Onsager1944"));
    }

    #[test]
    fn bibtex_picks_arxiv_via_eprint() {
        let body = r#"@article{Pollmann2012,
            title         = {Detection of SPT order},
            eprint        = {1010.3732},
            archivePrefix = {arXiv}
        }"#;
        let entry = parse_bibtex(body)
            .into_iter()
            .next()
            .unwrap()
            .expect("entry parses");
        assert!(matches!(entry.ref_, Ref::Arxiv(_)));
    }

    #[test]
    fn bibtex_bare_eprint_without_prefix_is_arxiv() {
        // The dominant convention: a lone `eprint` field is an arXiv id.
        let body = "@misc{x, eprint = {2204.12345}}";
        let entry = parse_bibtex(body)
            .into_iter()
            .next()
            .unwrap()
            .expect("entry parses");
        assert!(matches!(entry.ref_, Ref::Arxiv(_)));
    }

    #[test]
    fn bibtex_non_arxiv_eprinttype_reports_the_identifier_it_found() {
        // This test used to assert `NoIdentifier`, with the comment "the entry
        // has no resolvable identifier". The entry has a PMID. It is not
        // resolvable BY DOIGET, which is a different statement, and the one
        // #500 is about -- so the test was pinning the wrong claim.
        let body = "@article{x, eprint = {12345678}, eprinttype = {pubmed}}";
        let res = parse_bibtex(body).into_iter().next().unwrap();
        match res {
            Err(ParseError::UnsupportedIdentifier { kind, value, .. }) => {
                assert_eq!(kind, "PMID");
                assert_eq!(value, "12345678");
            }
            other => panic!("expected UnsupportedIdentifier, got {other:?}"),
        }
    }

    #[test]
    fn bibtex_doi_beats_arxiv_when_both_present() {
        // ADR-0030 D3: priority is DOI > arXiv > PMID.
        let body = r#"@article{both,
            doi           = {10.1234/foo},
            eprint        = {2401.12345},
            archivePrefix = {arXiv}
        }"#;
        let entry = parse_bibtex(body)
            .into_iter()
            .next()
            .unwrap()
            .expect("entry parses");
        assert!(matches!(entry.ref_, Ref::Doi(_)));
    }

    #[test]
    fn bibtex_multiple_entries_each_yield_a_result() {
        let body = r#"
            @article{a, doi = {10.1103/PhysRev.65.117}}
            @article{b, eprint = {1010.3732}, archivePrefix = {arXiv}}
            @article{c, title = {no identifier here}}
        "#;
        let parsed = parse_bibtex(body);
        assert_eq!(parsed.len(), 3);
        assert!(matches!(
            parsed[0],
            Ok(ParsedEntry {
                ref_: Ref::Doi(_),
                ..
            })
        ));
        assert!(matches!(
            parsed[1],
            Ok(ParsedEntry {
                ref_: Ref::Arxiv(_),
                ..
            })
        ));
        assert!(matches!(parsed[2], Err(ParseError::NoIdentifier { .. })));
    }

    #[test]
    fn bibtex_entry_without_identifier_yields_no_identifier_error() {
        let body = "@book{nodoi, title = {A Book}, author = {Author, A.}}";
        let res = parse_bibtex(body).into_iter().next().unwrap();
        assert!(matches!(res, Err(ParseError::NoIdentifier { .. })));
    }

    #[test]
    fn bibtex_invalid_doi_surfaces_as_invalid_ref_per_entry() {
        let body = "@article{bad, doi = {not-a-doi}}";
        let res = parse_bibtex(body).into_iter().next().unwrap();
        assert!(matches!(res, Err(ParseError::InvalidRef { .. })));
    }

    #[test]
    fn bibtex_malformed_input_yields_single_decode_error() {
        // A truncated entry the biblatex parser rejects outright.
        let body = "@article{unterminated, doi = {10.1234/x}";
        let parsed = parse_bibtex(body);
        assert_eq!(parsed.len(), 1);
        assert!(matches!(
            parsed[0],
            Err(ParseError::Decode {
                format: "bibtex",
                ..
            })
        ));
    }

    #[test]
    fn parse_input_bibtex_dispatches_and_parses() {
        let body = "@article{foo, doi = {10.1234/foo}}";
        let parsed = parse_input(body, Format::Bibtex, None);
        assert_eq!(parsed.len(), 1);
        let entry = parsed.into_iter().next().unwrap().expect("entry parses");
        assert!(matches!(entry.ref_, Ref::Doi(_)));
        assert_eq!(entry.entry_key.as_deref(), Some("foo"));
    }

    #[test]
    fn parse_input_auto_dispatches_bibtex_by_content() {
        // Leading `@article{` fingerprint routes to the BibTeX parser.
        let body = "@article{auto, doi = {10.1234/auto}}";
        let parsed = parse_input(body, Format::Auto, None);
        let entry = parsed.into_iter().next().unwrap().expect("entry parses");
        assert!(matches!(entry.ref_, Ref::Doi(_)));
    }

    #[test]
    fn parse_input_auto_with_path_uses_extension() {
        let body = "[]";
        let parsed = parse_input(body, Format::Auto, Some(Utf8Path::new("foo.csl")));
        assert_eq!(
            parsed.len(),
            0,
            "empty array yields zero entries: {parsed:?}"
        );
    }

    // ---- Format::as_wire ------------------------------------------

    #[test]
    fn format_wire_strings_are_stable() {
        // Pinned because the strings appear in the CLI --format flag,
        // the MCP tool input schema, and the JSON-Lines parse-error
        // records (ADR-0030 §6). A drift would be a wire-format break.
        assert_eq!(Format::Auto.as_wire(), "auto");
        assert_eq!(Format::Refs.as_wire(), "refs");
        assert_eq!(Format::CslJson.as_wire(), "csl-json");
        assert_eq!(Format::Bibtex.as_wire(), "bibtex");
    }

    #[test]
    fn the_unsupported_identifier_claim_denies_the_wrong_reading() {
        // #500's whole point: the sentence must put the gap on doiget's
        // side. A reader who takes "no identifier" at face value goes and
        // edits a `.bib` that was fine.
        let msg = unsupported_identifier_claim("PMID", "9659853");
        assert!(msg.contains("PMID"), "names the identifier kind: {msg}");
        assert!(msg.contains("9659853"), "quotes the value: {msg}");
        assert!(
            msg.contains("NOT missing an identifier"),
            "denies the wrong reading: {msg}"
        );
        assert!(msg.contains("#500"), "points at the issue: {msg}");
        // The `entry_key` prefix belongs to `Display`, not here -- callers
        // carry it in a field of its own and would say it twice.
        assert!(!msg.starts_with("entry {"), "no entry_key prefix: {msg}");
    }
}