deps-core 4.0.0

Core abstractions for deps-lsp: caching, errors, and traits
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
//! [`VersionData`]: the read-only bundle of per-document registry, lock-file and scan state
//! every LSP response builder consumes.

use std::collections::HashMap;
use std::sync::Arc;

#[cfg(all(doc, feature = "lsp-responses"))]
use super::generate_hover;
#[cfg(doc)]
use super::{
    DependencyOutcome, REGISTRY_FETCH_BUDGET, await_interactive_fetch, candidate_verdict,
    generate_diagnostics_from_cache, latest_verdict, local_cooldown_precedence,
};
use super::{
    DependencyOutcomes, EcosystemFormatter, GossipFindings, InteractiveFetchGate, LookupMiss,
};
use crate::ecosystem::Dependency;
use crate::error::FetchFailure;
use crate::licenses::LicensePolicy;
use crate::osv::{CandidateStatusMap, LatestStatusMap, VulnerabilityMap};
use crate::parser::DependencySource;
use crate::{
    ConcreteVersion, DepsDevClient, EcosystemId, LicenseSource, NormalizedName, PackageKey,
    PackageVersions, TyposquatSignal,
};

/// Registry data per package, keyed by the ecosystem-normalized name.
///
/// A declared name cannot probe it:
///
/// ```compile_fail
/// use deps_core::{CachedVersionsMap, PackageName};
///
/// let cached = CachedVersionsMap::new();
/// let _ = cached.get(&PackageName::new("serde"));
/// ```
pub type CachedVersionsMap = HashMap<NormalizedName, PackageVersions>;

/// Lock-file-resolved version per package, keyed by the ecosystem-normalized name.
pub type ResolvedVersionsMap = HashMap<NormalizedName, ConcreteVersion>;

/// Every retained lock-file version per package, keyed by the ecosystem-normalized name.
pub type ResolvedCandidatesMap = HashMap<NormalizedName, Vec<ConcreteVersion>>;

/// Bundles the per-package version maps (`cached`, `resolved`, the optional lock-file
/// candidates) that LSP handlers pass together everywhere.
///
/// Grouping them prevents accidentally swapping the map arguments at a call site, since the
/// compiler can no longer typecheck them positionally. Every per-package map is keyed by
/// [`NormalizedName`], so a read takes a [`PackageKey`] ([`Self::cached_for`],
/// [`Self::resolved_for`], [`Self::resolved_candidates_for`], [`Self::is_resolved`]) and a
/// declared [`crate::PackageName`] cannot probe one by accident.
///
/// # Examples
///
/// ```
/// use deps_core::lsp_helpers::PackageNaming;
/// use deps_core::{ConcreteVersion, PackageKey, PackageName, PackageVersions, VersionData};
/// use std::collections::HashMap;
///
/// struct Identity;
/// impl PackageNaming for Identity {}
///
/// let key = PackageKey::new(&PackageName::new("serde"), &Identity);
///
/// let mut cached = HashMap::new();
/// cached.insert(key.normalized().clone(), PackageVersions::latest_only("1.0.214"));
///
/// let mut resolved = HashMap::new();
/// resolved.insert(key.normalized().clone(), ConcreteVersion::new("1.0.200"));
///
/// let versions = VersionData::new(&cached, &resolved);
///
/// assert_eq!(versions.cached_for(&key).map(|v| v.latest.as_str()), Some("1.0.214"));
/// assert_eq!(versions.resolved_for(&key).map(ConcreteVersion::as_str), Some("1.0.200"));
/// ```
///
/// The maps themselves are not reachable from outside the crate:
///
/// ```compile_fail
/// use deps_core::VersionData;
/// use std::collections::HashMap;
///
/// let cached = HashMap::new();
/// let resolved = HashMap::new();
/// let versions = VersionData::new(&cached, &resolved);
/// let _ = versions.cached.len();
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, Copy)]
pub struct VersionData<'a> {
    /// Latest known versions and full version lists from the registry, keyed by normalized package name.
    cached: &'a CachedVersionsMap,
    /// Versions actually resolved in the lock file, keyed by normalized package name.
    resolved: &'a ResolvedVersionsMap,
    /// Every lock-file-resolved version for a normalized package name, when more than one is retained
    /// (issue #649) — additive alongside [`Self::resolved_for`], which stays the single
    /// collapsed value for the common case. `None` by default (most call sites have no such
    /// map); [`crate::lsp_helpers::resolve_in_use_version`] and [`crate::osv::vulnerability_keys`]
    /// consult it to disambiguate two manifest occurrences of one resolved name (e.g. a
    /// Cargo `package = "..."` rename pinning an older major) by each occurrence's own
    /// `version_requirement()`, falling back to [`Self::resolved_for`] when a name has at most
    /// one candidate.
    resolved_version_candidates: Option<&'a ResolvedCandidatesMap>,
    /// OSV scan results, keyed by normalized package name. `None` when no
    /// scan has run yet (e.g. the feature is disabled) — distinct from an
    /// empty map, which would mean "scanned, nothing found".
    pub vulnerabilities: Option<&'a VulnerabilityMap>,
    /// Yanked, deprecation, and fetch-failure findings from the most recent lifecycle fetch,
    /// keyed by normalized package name — see [`DependencyOutcome`] for what each channel
    /// means and why they must stay readable together off one lookup (D5 in
    /// [`generate_diagnostics_from_cache`], #233/#263/#205/#267). `None` when no fetch has run
    /// yet — distinct from an empty map, which would mean "checked, nothing found".
    pub outcomes: Option<&'a DependencyOutcomes>,
    /// This document's ecosystem, when the caller has one to give. `None` in
    /// most test fixtures and a handful of ecosystem-crate self-tests that
    /// predate this field.
    ///
    /// Enables two occurrence-aware refinements added for #394 (duplicate
    /// dependency names no longer collapsing into one shared finding):
    /// [`generate_diagnostics_from_cache`] only emits a yanked-version
    /// diagnostic on the occurrence whose own in-use version actually
    /// matches the recorded finding (S1), and the vulnerability lookups in
    /// `generate_diagnostics_from_cache`, [`generate_hover`], and
    /// `generate_code_actions` prefer a version-qualified
    /// [`crate::osv::VulnerabilityMap`] key over the plain name when more
    /// than one occurrence of a name has a distinct in-use version (S2).
    /// When `None`, both fall back to their pre-#394 name-only behavior.
    pub ecosystem: Option<EcosystemId>,
    /// The current [`NetworkMode`](crate::NetworkMode) (issue #483). When
    /// [`NetworkMode::Offline`](crate::NetworkMode::Offline), [`generate_hover`] appends a
    /// footer stating that version *and vulnerability* data were not checked — deliberately
    /// more specific than a bare "showing cached data" notice, since `hover.rs`'s
    /// `Some(ScanOutcome::Skipped(_)) | None` arm renders nothing for an offline OSV skip,
    /// which would otherwise look identical to a scanned-and-clean dependency.
    pub network: crate::NetworkMode,
    /// The deps.dev client to fetch a supply-chain trust signal through
    /// (spec 037), when the caller wants hover to attempt one. `None` by
    /// default and left `None` by every surface but `handlers/hover.rs`
    /// (deps-lsp) — diagnostics, code actions, inlay hints, and code lenses
    /// never set this, which is what makes FR-010's hover-only scope
    /// structural rather than convention: those surfaces cannot reach
    /// deps.dev because they are never handed a client. `&'a Arc<..>`, not
    /// `&'a DepsDevClient`, so [`generate_hover`] can clone the `Arc` into a
    /// detached background task.
    pub trust: Option<&'a Arc<DepsDevClient>>,
    /// Background-pre-fetched license data for ecosystems whose hot-path registry
    /// response carries no license field — Dart, Swift, Gradle, Deno (issue #660,
    /// spec 010 plan §1 tier 3). Gradle is also deps.dev-covered (spec 080), so on hover
    /// its deps.dev SPDX license takes precedence and this POM data is the fallback.
    /// Keyed by normalized
    /// package name. `None` by default. Populated from `DocumentState`'s
    /// per-document pre-fetch cache (`document::osv_scan::run_license_prefetch` on
    /// document open/change — mirrors [`Self::vulnerabilities`]'s "populated by a
    /// background task, read synchronously here" shape) by `handlers/hover.rs` (deps-lsp)
    /// for hover, and by `handlers/diagnostics.rs`'s `textDocument/diagnostic` pull path
    /// for [`generate_diagnostics_from_cache`]'s license-policy rule (issue #661) — see
    /// [`Self::license_policy`].
    ///
    /// **What version this actually reflects is per-ecosystem, not uniformly the
    /// resolved version** (critic S1 — corrects a previous blanket claim here that it
    /// "only ever covers the resolved version"): Gradle's Maven Central POM fetch and
    /// Deno's JSR per-version API are genuinely fetched at the dependency's resolved/
    /// in-use version. Dart's pub.dev `/score` endpoint is per-*package*, not
    /// per-version — it reflects pana's detection on whatever pub.dev most recently
    /// scored, unrelated to which version is resolved. Swift's GitHub
    /// `GET /repos/{owner}/{repo}` reflects the repository's default branch, not the
    /// resolved version's tag. Every source here is still a background pre-fetch gated
    /// on a version having been resolved at all (no in-use version means nothing to
    /// look up), but only Gradle/Deno are actually version-*specific* in what they
    /// return.
    pub license_prefetch: Option<&'a HashMap<NormalizedName, Vec<String>>>,
    /// How [`Self::license_prefetch`]'s (and, for a resolved-version match, the
    /// registry-declared) license strings are sourced (issue #688), consulted by
    /// [`generate_hover`] for the "(detected)" qualifier and by
    /// [`generate_diagnostics_from_cache`]'s license-policy rule to decide whether
    /// [`crate::licenses::resolve_license_entries`] must normalize free text before
    /// evaluation. `None` in most test fixtures and any handler that doesn't go through
    /// [`crate::Ecosystem::generate_hover`]/[`crate::Ecosystem::generate_diagnostics`]'s
    /// default implementation, which is what actually attaches this field via
    /// `self.license_source()` — falls back to
    /// [`LicenseSource::RegistryDeclaredSpdx`] (`Default`) wherever consumed.
    pub license_source: Option<LicenseSource>,
    /// SPDX allow-list/deny-list license policy (issue #661, spec 010 Phase 2), when the
    /// caller wants diagnostics evaluated against one. `None` by default.
    ///
    /// Unlike [`Self::trust`], this is **not** hover-only-scoped: `deps-lsp`'s
    /// `handlers/diagnostics.rs::generate_diagnostics_internal` attaches the currently
    /// configured policy (cached on `ServerState`, kept live-updated by
    /// `Backend::initialize`/`did_change_configuration`) unconditionally, so every
    /// diagnostics-generation call site — the `textDocument/diagnostic` pull path *and*
    /// every push-path background refresh (fetch-completion, watched-config reparse,
    /// lock-file change) — evaluates the same policy. An earlier revision scoped this
    /// field to the pull path only, following `Self::trust`'s hover-only precedent; that
    /// was a false analogy (issue #660/#661 critic C1) — `trust` is safe hover-only
    /// because hover has exactly one producer per request, but diagnostics has multiple
    /// producers all replacing the same client-visible `publish_diagnostics` set, so a
    /// caller-scoped policy meant the license diagnostic flickered in and out on every
    /// edit and was invisible to push-only clients.
    /// [`generate_diagnostics_from_cache`]'s license-policy rule reads this alongside
    /// [`Self::license_prefetch`] — so today it only actually fires for tier-3 ecosystems
    /// (Dart/Swift/Gradle/Deno; Gradle's POM free text is normalized first, see that rule's
    /// doc comment), the only ones `license_prefetch` covers; this field and the rule are otherwise
    /// ecosystem-agnostic and need no change as `license_prefetch`'s coverage widens.
    pub license_policy: Option<&'a LicensePolicy>,
    /// Background-pre-fetched typosquat-suspect signal per declared dependency (issue
    /// #1437, spec 071), keyed by normalized package name — mirrors
    /// [`Self::license_prefetch`]'s exact shape and rationale (NFR-002: this must never be
    /// an inline `.await` on the diagnostics-generation path, so it is resolved ahead of
    /// time by a document-lifecycle background task,
    /// `deps-lsp::document::typosquat::run_typosquat_prefetch`, and merely read
    /// synchronously here — see that function's doc). Consumed directly inside
    /// [`generate_diagnostics_from_cache`] itself (not gated behind
    /// `Ecosystem::generate_diagnostics`'s default impl), the same way
    /// [`Self::license_prefetch`] is, so every ecosystem's `generate_diagnostics`
    /// override — not just the shared default — picks up the diagnostic automatically
    /// (issue #1437 security-review finding: an override that calls
    /// [`generate_diagnostics_from_cache`] directly, as `deps-npm`'s catalog-diagnostics
    /// override does, must never be able to silently bypass this). `None` when the feature
    /// is disabled, the ecosystem isn't deps.dev-covered, offline, or no dependency in the
    /// document cleared the ratio gate.
    pub typosquat_prefetch: Option<&'a HashMap<NormalizedName, TyposquatSignal>>,
    /// Background-pre-fetched deps.dev GOSSIP cooldown/low-usage findings per declared
    /// dependency (issue #1456, spec 072), keyed by normalized package name —
    /// mirrors [`Self::typosquat_prefetch`]'s exact shape and rationale: populated by
    /// `deps-lsp::document::gossip_prefetch`, via `DocumentState::signals.gossip_findings`, and
    /// merely read synchronously here — never fetched inline on the hover/diagnostics-
    /// generation path itself.
    ///
    /// A lookup hit is only trustworthy for the version it was resolved against
    /// (spec 072 FR-008) — the entry's own [`crate::GossipFindings::version`] field must
    /// be compared against the version actually being displayed at each call site (hover's
    /// `latest_line`, diagnostics' `package_versions.latest`) before use; a mismatch is
    /// treated as a cache miss and falls back to [`local_cooldown_precedence`]. `None`
    /// when the feature is disabled, offline, the ecosystem isn't deps.dev-covered, or no
    /// dependency in the document has a prefetch result yet.
    pub gossip_prefetch: Option<&'a HashMap<NormalizedName, GossipFindings>>,
    /// The deps.dev client to fetch hover's live GOSSIP low-usage signal through (issue
    /// #1456, spec 072 FR-005/FR-009), when the caller wants hover to attempt one. `None`
    /// by default, mirroring [`Self::trust`]'s exact "presence is the gate" shape (FR-010's
    /// structural pattern) — set only by `handlers/hover.rs` (deps-lsp) when
    /// `GossipConfig.enabled` is set and not offline. Deliberately a **separate** field
    /// from [`Self::trust`], not reused: `SupplyChainConfig.enabled` (opt-out, default
    /// `true`) and `GossipConfig.enabled` (opt-in, default `false`) are independently
    /// switchable — conflating the two fields would let disabling supply-chain trust
    /// signals silently disable GOSSIP's low-usage fetch too, or vice versa. `&'a Arc<..>`,
    /// not `&'a DepsDevClient`, for the same "clone into a detached background task"
    /// reason [`Self::trust`]'s doc gives.
    pub gossip_client: Option<&'a Arc<DepsDevClient>>,
    /// Phase B's per-key "latest" check result (issue #1517), keyed the same way as
    /// [`Self::vulnerabilities`] — see [`crate::osv::LatestStatusMap`]. `None` means OSV
    /// checking is disabled or offline for this scan entirely, distinct from `Some(map)` with a
    /// missing/`Unverified` entry for one dependency (which [`latest_verdict`] treats as
    /// fail-closed, not "not applicable"). Every renderer that surfaces a dependency's `latest`
    /// as an upgrade recommendation (hover, diagnostics, code actions, code lens, inlay hints,
    /// completion) must consult [`latest_verdict`] with this field before treating that `latest`
    /// as safe.
    pub latest_status: Option<&'a LatestStatusMap>,
    /// Phase B's per-(dependency, candidate-version) OSV check result (#1524) — see
    /// [`crate::osv::CandidateStatusMap`]. `None` means OSV checking is disabled or offline for
    /// this scan entirely, mirroring [`Self::latest_status`]'s identical distinction. A
    /// candidate-offering surface (code actions' "update to X" list, completion's version
    /// items) must consult [`candidate_verdict`] with this field for every version it is about
    /// to offer, not only the one [`Self::latest_status`] already covers.
    pub candidate_status: Option<&'a CandidateStatusMap>,
    /// Failure memo and concurrency cap for hover's and code actions' interactive registry
    /// await (#1830). `None` by default — and for every surface but `handlers/hover.rs` and
    /// `handlers/code_actions.rs` (deps-lsp) — which keeps the plain
    /// [`REGISTRY_FETCH_BUDGET`]-bounded await; see [`await_interactive_fetch`].
    pub interactive_fetch: Option<&'a InteractiveFetchGate>,
}

impl<'a> VersionData<'a> {
    /// Creates a new `VersionData` from the cached and resolved version maps.
    ///
    /// `vulnerabilities` starts `None`; chain [`Self::with_vulnerabilities`]
    /// to attach a scan result.
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::VersionData;
    /// use std::collections::HashMap;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let versions = VersionData::new(&cached, &resolved);
    /// assert!(versions.vulnerabilities.is_none());
    /// ```
    pub fn new(cached: &'a CachedVersionsMap, resolved: &'a ResolvedVersionsMap) -> Self {
        Self {
            cached,
            resolved,
            resolved_version_candidates: None,
            vulnerabilities: None,
            outcomes: None,
            ecosystem: None,
            network: crate::NetworkMode::Online,
            trust: None,
            license_prefetch: None,
            license_source: None,
            license_policy: None,
            typosquat_prefetch: None,
            gossip_prefetch: None,
            gossip_client: None,
            latest_status: None,
            candidate_status: None,
            interactive_fetch: None,
        }
    }

    /// The registry entry for `key`.
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::lsp_helpers::PackageNaming;
    /// use deps_core::{PackageKey, PackageName, PackageVersions, VersionData};
    /// use std::collections::HashMap;
    ///
    /// struct Identity;
    /// impl PackageNaming for Identity {}
    ///
    /// let hit = PackageKey::new(&PackageName::new("serde"), &Identity);
    /// let mut cached = HashMap::new();
    /// cached.insert(hit.normalized().clone(), PackageVersions::latest_only("1.0.214"));
    /// let resolved = HashMap::new();
    /// let versions = VersionData::new(&cached, &resolved);
    ///
    /// let miss = PackageKey::new(&PackageName::new("tokio"), &Identity);
    /// assert!(versions.cached_for(&hit).is_some());
    /// assert!(versions.cached_for(&miss).is_none());
    /// ```
    #[must_use]
    pub fn cached_for(&self, key: &PackageKey) -> Option<&'a PackageVersions> {
        self.cached.get(key.normalized())
    }

    /// The lock-file-resolved version for `key`.
    #[must_use]
    pub fn resolved_for(&self, key: &PackageKey) -> Option<&'a ConcreteVersion> {
        self.resolved.get(key.normalized())
    }

    /// The recorded registry fetch failure for `key`, if any.
    #[must_use]
    pub fn fetch_failure_for(&self, key: &PackageKey) -> Option<&'a FetchFailure> {
        self.outcomes
            .and_then(|outcomes| outcomes.fetch_failure(key.normalized()))
    }

    /// The recorded [`LookupMiss`] for `key`, regardless of whether it is reportable.
    #[must_use]
    pub fn lookup_miss_for(&self, key: &PackageKey) -> Option<LookupMiss> {
        self.outcomes
            .and_then(|outcomes| outcomes.lookup_miss(key.normalized()))
    }

    /// The [`LookupMiss`] for `dep` when it is safe to surface to the user, else `None`.
    ///
    /// The single gate shared by diagnostics and inlay hints. It suppresses the miss when
    /// the session is offline (a stale miss says nothing about the registry), when the
    /// source cannot be resolved against a registry, and for
    /// [`DependencySource::AlternateRegistry`] sources, which are opt-in routes that are
    /// expected to answer 404 for packages they do not mirror.
    #[must_use]
    pub fn reportable_lookup_miss(
        &self,
        dep: &dyn Dependency,
        key: &PackageKey,
        formatter: &dyn EcosystemFormatter,
    ) -> Option<LookupMiss> {
        let miss = self.lookup_miss_for(key)?;
        if miss == LookupMiss::NotLookedUp || self.network.is_offline() {
            return None;
        }
        let source = dep.source();
        if !formatter.can_resolve_source(&source)
            || matches!(source, DependencySource::AlternateRegistry { .. })
        {
            return None;
        }
        Some(miss)
    }

    /// Whether the lock file resolved `key`.
    #[must_use]
    pub fn is_resolved(&self, key: &PackageKey) -> bool {
        self.resolved_for(key).is_some()
    }

    /// Every lock-file-resolved version retained for `key` (see
    /// [`Self::with_resolved_version_candidates`]), or `None` when no candidates map is attached
    /// or it has no entry for the package.
    #[must_use]
    pub fn resolved_candidates_for(&self, key: &PackageKey) -> Option<&'a [ConcreteVersion]> {
        self.resolved_version_candidates
            .and_then(|candidates| candidates.get(key.normalized()))
            .map(Vec::as_slice)
    }

    /// The whole lock-file map, for deps-core helpers that take the raw map.
    pub(crate) const fn resolved_map(&self) -> &'a ResolvedVersionsMap {
        self.resolved
    }

    /// The whole candidates map, for deps-core helpers that take the raw map.
    pub(crate) const fn candidates_map(&self) -> Option<&'a ResolvedCandidatesMap> {
        self.resolved_version_candidates
    }

    /// Attaches the interactive-fetch gate that lets hover and code actions skip a registry
    /// await already known to fail (#1830).
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::VersionData;
    /// use deps_core::lsp_helpers::InteractiveFetchGate;
    /// use std::collections::HashMap;
    /// use std::num::NonZeroUsize;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let gate = InteractiveFetchGate::new(NonZeroUsize::new(4).unwrap());
    /// let versions = VersionData::new(&cached, &resolved).with_interactive_fetch_gate(&gate);
    /// assert!(versions.interactive_fetch.is_some());
    /// ```
    #[must_use]
    pub const fn with_interactive_fetch_gate(mut self, gate: &'a InteractiveFetchGate) -> Self {
        self.interactive_fetch = Some(gate);
        self
    }

    /// Attaches the per-name lock-file candidates map, enabling the per-occurrence
    /// disambiguation described on [`Self::resolved_candidates_for`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::lsp_helpers::PackageNaming;
    /// use deps_core::{ConcreteVersion, PackageKey, PackageName, VersionData};
    /// use std::collections::HashMap;
    ///
    /// struct Identity;
    /// impl PackageNaming for Identity {}
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let key = PackageKey::new(&PackageName::new("serde"), &Identity);
    /// let mut candidates = HashMap::new();
    /// candidates.insert(key.normalized().clone(), vec![ConcreteVersion::new("1.0.200")]);
    /// let versions =
    ///     VersionData::new(&cached, &resolved).with_resolved_version_candidates(&candidates);
    /// assert_eq!(versions.resolved_candidates_for(&key).map(<[_]>::len), Some(1));
    /// ```
    #[must_use]
    pub const fn with_resolved_version_candidates(
        mut self,
        candidates: &'a ResolvedCandidatesMap,
    ) -> Self {
        self.resolved_version_candidates = Some(candidates);
        self
    }

    /// Attaches an OSV scan result to this `VersionData`.
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::VersionData;
    /// use deps_core::osv::VulnerabilityMap;
    /// use std::collections::HashMap;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let vulns = VulnerabilityMap::new();
    /// let versions = VersionData::new(&cached, &resolved).with_vulnerabilities(&vulns);
    /// assert!(versions.vulnerabilities.is_some());
    /// ```
    #[must_use]
    pub fn with_vulnerabilities(mut self, vulnerabilities: &'a VulnerabilityMap) -> Self {
        self.vulnerabilities = Some(vulnerabilities);
        self
    }

    /// Attaches phase B's per-key "latest" check result to this `VersionData`. See
    /// [`Self::latest_status`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::VersionData;
    /// use deps_core::osv::LatestStatusMap;
    /// use std::collections::HashMap;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let latest_status = LatestStatusMap::new();
    /// let versions = VersionData::new(&cached, &resolved).with_latest_status(&latest_status);
    /// assert!(versions.latest_status.is_some());
    /// ```
    #[must_use]
    pub fn with_latest_status(mut self, latest_status: &'a LatestStatusMap) -> Self {
        self.latest_status = Some(latest_status);
        self
    }

    /// Attaches phase B's per-candidate-version check result to this `VersionData`. See
    /// [`Self::candidate_status`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::VersionData;
    /// use deps_core::osv::CandidateStatusMap;
    /// use std::collections::HashMap;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let candidate_status = CandidateStatusMap::new();
    /// let versions =
    ///     VersionData::new(&cached, &resolved).with_candidate_status(&candidate_status);
    /// assert!(versions.candidate_status.is_some());
    /// ```
    #[must_use]
    pub fn with_candidate_status(mut self, candidate_status: &'a CandidateStatusMap) -> Self {
        self.candidate_status = Some(candidate_status);
        self
    }

    /// Attaches yanked, deprecation, and fetch-failure findings to this `VersionData`. See
    /// [`Self::outcomes`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::VersionData;
    /// use deps_core::lsp_helpers::DependencyOutcomes;
    /// use std::collections::HashMap;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let outcomes = DependencyOutcomes::new();
    /// let versions = VersionData::new(&cached, &resolved).with_outcomes(&outcomes);
    /// assert!(versions.outcomes.is_some());
    /// ```
    #[must_use]
    pub fn with_outcomes(mut self, outcomes: &'a DependencyOutcomes) -> Self {
        self.outcomes = Some(outcomes);
        self
    }

    /// Attaches this document's ecosystem, enabling the occurrence-aware
    /// refinements described on [`Self::ecosystem`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::{EcosystemId, VersionData};
    /// use std::collections::HashMap;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let versions = VersionData::new(&cached, &resolved).with_ecosystem(EcosystemId::Cargo);
    /// assert_eq!(versions.ecosystem, Some(EcosystemId::Cargo));
    /// ```
    #[must_use]
    pub const fn with_ecosystem(mut self, ecosystem: EcosystemId) -> Self {
        self.ecosystem = Some(ecosystem);
        self
    }

    /// Marks this `VersionData` as built while `network.offline` was set, so
    /// [`generate_hover`] appends its offline footer.
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::VersionData;
    /// use std::collections::HashMap;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let versions = VersionData::new(&cached, &resolved).with_network(deps_core::NetworkMode::Offline);
    /// assert_eq!(versions.network, deps_core::NetworkMode::Offline);
    /// ```
    #[must_use]
    pub const fn with_network(mut self, network: crate::NetworkMode) -> Self {
        self.network = network;
        self
    }

    /// Attaches a deps.dev client, enabling [`generate_hover`] to attempt a
    /// supply-chain trust signal for the hovered dependency. See
    /// [`Self::trust`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::{DepsDevClient, HttpCache, VersionData};
    /// use std::collections::HashMap;
    /// use std::sync::Arc;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let client = Arc::new(DepsDevClient::new(Arc::new(HttpCache::new())));
    /// let versions = VersionData::new(&cached, &resolved).with_trust(&client);
    /// assert!(versions.trust.is_some());
    /// ```
    #[must_use]
    pub const fn with_trust(mut self, client: &'a Arc<DepsDevClient>) -> Self {
        self.trust = Some(client);
        self
    }

    /// Attaches tier-3 pre-fetched license data. See [`Self::license_prefetch`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::VersionData;
    /// use std::collections::HashMap;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let licenses = HashMap::new();
    /// let versions = VersionData::new(&cached, &resolved).with_license_prefetch(&licenses);
    /// assert!(versions.license_prefetch.is_some());
    /// ```
    #[must_use]
    pub const fn with_license_prefetch(
        mut self,
        licenses: &'a HashMap<NormalizedName, Vec<String>>,
    ) -> Self {
        self.license_prefetch = Some(licenses);
        self
    }

    /// Attaches this ecosystem's license source. See [`Self::license_source`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::{LicenseSource, VersionData};
    /// use std::collections::HashMap;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let versions =
    ///     VersionData::new(&cached, &resolved).with_license_source(LicenseSource::PomFreeText);
    /// assert_eq!(versions.license_source, Some(LicenseSource::PomFreeText));
    /// ```
    #[must_use]
    pub const fn with_license_source(mut self, source: LicenseSource) -> Self {
        self.license_source = Some(source);
        self
    }

    /// Attaches a license policy for diagnostics evaluation. See [`Self::license_policy`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::{LicensePolicy, VersionData};
    /// use std::collections::HashMap;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let policy = LicensePolicy::new(vec!["MIT".to_string()], vec![]);
    /// let versions = VersionData::new(&cached, &resolved).with_license_policy(&policy);
    /// assert!(versions.license_policy.is_some());
    /// ```
    #[must_use]
    pub const fn with_license_policy(mut self, policy: &'a LicensePolicy) -> Self {
        self.license_policy = Some(policy);
        self
    }

    /// Attaches background-pre-fetched typosquat signals. See [`Self::typosquat_prefetch`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::VersionData;
    /// use std::collections::HashMap;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let typosquat = HashMap::new();
    /// let versions = VersionData::new(&cached, &resolved).with_typosquat_prefetch(&typosquat);
    /// assert!(versions.typosquat_prefetch.is_some());
    /// ```
    #[must_use]
    pub const fn with_typosquat_prefetch(
        mut self,
        typosquat_prefetch: &'a HashMap<NormalizedName, TyposquatSignal>,
    ) -> Self {
        self.typosquat_prefetch = Some(typosquat_prefetch);
        self
    }

    /// Attaches background-pre-fetched GOSSIP findings. See [`Self::gossip_prefetch`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::VersionData;
    /// use std::collections::HashMap;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let gossip = HashMap::new();
    /// let versions = VersionData::new(&cached, &resolved).with_gossip_prefetch(&gossip);
    /// assert!(versions.gossip_prefetch.is_some());
    /// ```
    #[must_use]
    pub const fn with_gossip_prefetch(
        mut self,
        gossip_prefetch: &'a HashMap<NormalizedName, GossipFindings>,
    ) -> Self {
        self.gossip_prefetch = Some(gossip_prefetch);
        self
    }

    /// Attaches a deps.dev client, enabling hover to attempt a live GOSSIP low-usage fetch.
    /// See [`Self::gossip_client`].
    ///
    /// # Examples
    ///
    /// ```
    /// use deps_core::{DepsDevClient, HttpCache, VersionData};
    /// use std::collections::HashMap;
    /// use std::sync::Arc;
    ///
    /// let cached = HashMap::new();
    /// let resolved = HashMap::new();
    /// let client = Arc::new(DepsDevClient::new(Arc::new(HttpCache::new())));
    /// let versions = VersionData::new(&cached, &resolved).with_gossip_client(&client);
    /// assert!(versions.gossip_client.is_some());
    /// ```
    #[must_use]
    pub const fn with_gossip_client(mut self, client: &'a Arc<DepsDevClient>) -> Self {
        self.gossip_client = Some(client);
        self
    }
}