Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ComboIndexCache::cache_path #52

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- next-header -->
## [Unreleased] - ReleaseDate
### Added
- [PR#52](https://github.com/EmbarkStudios/tame-index/pull/52) added `ComboIndexCache::cache_path` to retrieve the path of a particular crate's index entry.

## [0.9.6] - 2024-03-12
### Changed
- [PR#51](https://github.com/EmbarkStudios/tame-index/pull/51) updated dependencies.
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub enum Error {
#[error(transparent)]
Io(#[from] std::io::Error),
/// An I/O error occurred trying to access a specific path
#[error("I/O operation failed for path '{}'", .1)]
#[error("I/O operation failed for path '{}': {}", .1, .0)]
IoPath(#[source] std::io::Error, crate::PathBuf),
/// A user provided URL was invalid
#[error(transparent)]
Expand Down
13 changes: 11 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ pub enum ComboIndexCache {
}

impl ComboIndexCache {
/// 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
/// Retrieves the index metadata for the specified crate name
#[inline]
pub fn cached_krate(
&self,
Expand All @@ -131,6 +130,16 @@ impl ComboIndexCache {
}
}

/// Gets the path to the cache entry for the specified crate
pub fn cache_path(&self, name: crate::KrateName<'_>) -> crate::PathBuf {
match self {
Self::Git(index) => index.cache.cache_path(name),
Self::Sparse(index) => index.cache().cache_path(name),
#[cfg(feature = "local")]
Self::Local(lr) => lr.krate_path(name),
}
}

/// Constructs a [`Self`] for the specified index.
///
/// See [`Self::crates_io`] if you want to create a crates.io index based
Expand Down
11 changes: 10 additions & 1 deletion src/index/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl LocalRegistry {
name: KrateName<'_>,
_lock: &FileLock,
) -> Result<Option<IndexKrate>, Error> {
let index_path = make_path(&self.path, name);
let index_path = self.krate_path(name);

let buf = match std::fs::read(&index_path) {
Ok(buf) => buf,
Expand All @@ -146,6 +146,15 @@ impl LocalRegistry {

Ok(Some(IndexKrate::from_slice(&buf)?))
}

/// Gets the path to the index entry for the krate.
///
/// Note that unlike .cache entries for git and sparse indices, these are not
/// binary files, they are just the JSON line format
#[inline]
pub fn krate_path(&self, name: KrateName<'_>) -> PathBuf {
make_path(&self.path, name)
}
}

/// Allows the building of a local registry from a [`RemoteGitIndex`] or [`RemoteSparseIndex`]
Expand Down