tame_index/index/
combo.rs

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
#[cfg(feature = "local")]
use crate::index::LocalRegistry;
use crate::{
    index::{FileLock, RemoteGitIndex, RemoteSparseIndex},
    Error, IndexKrate, KrateName,
};

/// A wrapper around either a [`RemoteGitIndex`] or [`RemoteSparseIndex`]
#[non_exhaustive]
pub enum ComboIndex {
    /// A standard git based registry index. No longer the default for crates.io
    /// as of 1.70.0
    Git(RemoteGitIndex),
    /// An HTTP sparse index
    Sparse(RemoteSparseIndex),
    /// A local registry
    #[cfg(feature = "local")]
    Local(LocalRegistry),
}

impl ComboIndex {
    /// Retrieves the index metadata for the specified crate name, optionally
    /// writing a cache entry for it if there was not already an up to date one
    ///
    /// Note no cache entry is written if this is a `Local` registry as they do
    /// not use .cache files
    #[inline]
    pub fn krate(
        &self,
        name: KrateName<'_>,
        write_cache_entry: bool,
        lock: &FileLock,
    ) -> Result<Option<IndexKrate>, Error> {
        match self {
            Self::Git(index) => index.krate(name, write_cache_entry, lock),
            Self::Sparse(index) => index.krate(name, write_cache_entry, lock),
            #[cfg(feature = "local")]
            Self::Local(lr) => lr.cached_krate(name, lock),
        }
    }

    /// Retrieves the cached crate metadata if it exists
    #[inline]
    pub fn cached_krate(
        &self,
        name: KrateName<'_>,
        lock: &FileLock,
    ) -> Result<Option<IndexKrate>, Error> {
        match self {
            Self::Git(index) => index.cached_krate(name, lock),
            Self::Sparse(index) => index.cached_krate(name, lock),
            #[cfg(feature = "local")]
            Self::Local(lr) => lr.cached_krate(name, lock),
        }
    }
}

impl From<RemoteGitIndex> for ComboIndex {
    #[inline]
    fn from(index: RemoteGitIndex) -> Self {
        Self::Git(index)
    }
}

impl From<RemoteSparseIndex> for ComboIndex {
    #[inline]
    fn from(index: RemoteSparseIndex) -> Self {
        Self::Sparse(index)
    }
}

#[cfg(feature = "local")]
impl From<LocalRegistry> for ComboIndex {
    #[inline]
    fn from(local: LocalRegistry) -> Self {
        Self::Local(local)
    }
}