tame_index/index/
combo.rs

1#[cfg(feature = "local")]
2use crate::index::LocalRegistry;
3use crate::{
4    index::{FileLock, RemoteGitIndex, RemoteSparseIndex},
5    Error, IndexKrate, KrateName,
6};
7
8/// A wrapper around either a [`RemoteGitIndex`] or [`RemoteSparseIndex`]
9#[non_exhaustive]
10pub enum ComboIndex {
11    /// A standard git based registry index. No longer the default for crates.io
12    /// as of 1.70.0
13    Git(RemoteGitIndex),
14    /// An HTTP sparse index
15    Sparse(RemoteSparseIndex),
16    /// A local registry
17    #[cfg(feature = "local")]
18    Local(LocalRegistry),
19}
20
21impl ComboIndex {
22    /// Retrieves the index metadata for the specified crate name, optionally
23    /// writing a cache entry for it if there was not already an up to date one
24    ///
25    /// Note no cache entry is written if this is a `Local` registry as they do
26    /// not use .cache files
27    #[inline]
28    pub fn krate(
29        &self,
30        name: KrateName<'_>,
31        write_cache_entry: bool,
32        lock: &FileLock,
33    ) -> Result<Option<IndexKrate>, Error> {
34        match self {
35            Self::Git(index) => index.krate(name, write_cache_entry, lock),
36            Self::Sparse(index) => index.krate(name, write_cache_entry, lock),
37            #[cfg(feature = "local")]
38            Self::Local(lr) => lr.cached_krate(name, lock),
39        }
40    }
41
42    /// Retrieves the cached crate metadata if it exists
43    #[inline]
44    pub fn cached_krate(
45        &self,
46        name: KrateName<'_>,
47        lock: &FileLock,
48    ) -> Result<Option<IndexKrate>, Error> {
49        match self {
50            Self::Git(index) => index.cached_krate(name, lock),
51            Self::Sparse(index) => index.cached_krate(name, lock),
52            #[cfg(feature = "local")]
53            Self::Local(lr) => lr.cached_krate(name, lock),
54        }
55    }
56}
57
58impl From<RemoteGitIndex> for ComboIndex {
59    #[inline]
60    fn from(index: RemoteGitIndex) -> Self {
61        Self::Git(index)
62    }
63}
64
65impl From<RemoteSparseIndex> for ComboIndex {
66    #[inline]
67    fn from(index: RemoteSparseIndex) -> Self {
68        Self::Sparse(index)
69    }
70}
71
72#[cfg(feature = "local")]
73impl From<LocalRegistry> for ComboIndex {
74    #[inline]
75    fn from(local: LocalRegistry) -> Self {
76        Self::Local(local)
77    }
78}