Skip to content

Commit

Permalink
Pass hash_type by reference
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Feb 9, 2025
1 parent f48dc97 commit a77d2d0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
22 changes: 11 additions & 11 deletions src/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ pub fn local_path_and_canonical_url(
url: &str,
cargo_home: Option<&std::path::Path>,
) -> Result<(std::path::PathBuf, String), Error> {
local_path_and_canonical_url_with_hash_kind(url, cargo_home, HashKind::Legacy)
local_path_and_canonical_url_with_hash_kind(url, cargo_home, &DEFAULT_HASHER_KIND)
}

/// Like [`local_path_and_canonical_url`] but accepts [`HashKind`] for determining the crate index path.
pub fn local_path_and_canonical_url_with_hash_kind(
url: &str,
cargo_home: Option<&std::path::Path>,
hash_kind: HashKind,
hash_kind: &HashKind,
) -> Result<(std::path::PathBuf, String), Error> {
let (dir_name, canonical_url) = url_to_local_dir(url, hash_kind)?;

Expand Down Expand Up @@ -101,7 +101,7 @@ pub(crate) const DEFAULT_HASHER_KIND: HashKind = HashKind::Legacy;

/// Converts a full url, eg https://github.com/rust-lang/crates.io-index, into
/// the root directory name where cargo itself will fetch it on disk
fn url_to_local_dir(url: &str, hash_kind: HashKind) -> Result<(String, String), Error> {
fn url_to_local_dir(url: &str, hash_kind: &HashKind) -> Result<(String, String), Error> {
#[allow(deprecated)]
fn legacy_hash_u64(url: &str, registry_kind: u64) -> u64 {
use std::hash::{Hash, Hasher, SipHasher};
Expand Down Expand Up @@ -258,11 +258,11 @@ mod test {
fn http_index_url_matches_cargo() {
use crate::sparse::URL;
assert_eq!(
super::url_to_local_dir(URL, HashKind::Legacy).unwrap(),
super::url_to_local_dir(URL, &HashKind::Legacy).unwrap(),
("index.crates.io-6f17d22bba15001f".to_owned(), URL.to_owned(),)
);
assert_eq!(
super::url_to_local_dir(URL, HashKind::Stable).unwrap(),
super::url_to_local_dir(URL, &HashKind::Stable).unwrap(),
("index.crates.io-1949cf8c6b5b557f".to_owned(), URL.to_owned(),)
);

Expand All @@ -272,7 +272,7 @@ mod test {
assert_eq!(
super::url_to_local_dir(
"https://dl.cloudsmith.io/aBcW1234aBcW1234/embark/rust/cargo/index.git",
HashKind::Legacy
&HashKind::Legacy
)
.unwrap(),
(
Expand All @@ -283,7 +283,7 @@ mod test {
assert_eq!(
super::url_to_local_dir(
"https://dl.cloudsmith.io/aBcW1234aBcW1234/embark/rust/cargo/index.git",
HashKind::Stable
&HashKind::Stable
)
.unwrap(),
(
Expand All @@ -298,23 +298,23 @@ mod test {
fn git_url_matches_cargo() {
use crate::git::URL;
assert_eq!(
crate::dirs::url_to_local_dir(URL, HashKind::Legacy).unwrap(),
crate::dirs::url_to_local_dir(URL, &HashKind::Legacy).unwrap(),
("github.com-1ecc6299db9ec823".to_owned(), URL.to_owned())
);
assert_eq!(
crate::dirs::url_to_local_dir(URL, HashKind::Stable).unwrap(),
crate::dirs::url_to_local_dir(URL, &HashKind::Stable).unwrap(),
("github.com-25cdd57fae9f0462".to_owned(), URL.to_owned())
);

// Ensure we actually strip off the irrelevant parts of a url, note that
// the .git suffix is not part of the canonical url, but *is* used when hashing
assert_eq!(
crate::dirs::url_to_local_dir(&format!("registry+{}.git?one=1&two=2#fragment", URL), HashKind::Legacy)
crate::dirs::url_to_local_dir(&format!("registry+{}.git?one=1&two=2#fragment", URL), &HashKind::Legacy)
.unwrap(),
("github.com-c786010fb7ef2e6e".to_owned(), URL.to_owned())
);
assert_eq!(
crate::dirs::url_to_local_dir(&format!("registry+{}.git?one=1&two=2#fragment", URL), HashKind::Stable)
crate::dirs::url_to_local_dir(&format!("registry+{}.git?one=1&two=2#fragment", URL), &HashKind::Stable)
.unwrap(),
("github.com-e78ed0bbfe5f35d7".to_owned(), URL.to_owned())
);
Expand Down
8 changes: 4 additions & 4 deletions src/git/impl_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ impl GitIndex {
/// Concurrent invocations may fail if the index needs to be cloned. To prevent that,
/// use synchronization mechanisms like mutexes or file locks as needed by the application.
pub fn from_url(url: &str) -> Result<Self, Error> {
Self::from_url_with_hash_kind(url, DEFAULT_HASHER_KIND)
Self::from_url_with_hash_kind(url, &DEFAULT_HASHER_KIND)
}

/// Like [`Self::from_url`], but accepts an explicit [`HashKind`] for determining the crates index path.
pub fn from_url_with_hash_kind(url: &str, hash_kind: HashKind) -> Result<Self, Error> {
pub fn from_url_with_hash_kind(url: &str, hash_kind: &HashKind) -> Result<Self, Error> {
let (path, canonical_url) = local_path_and_canonical_url_with_hash_kind(url, None, hash_kind)?;
Ok(
Self::from_path_and_url(path, canonical_url, Mode::CloneUrlToPathIfRepoMissing)?
Expand All @@ -108,11 +108,11 @@ impl GitIndex {

/// Like [`Self::from_url()`], but read-only without auto-cloning the index at `url`.
pub fn try_from_url(url: &str) -> Result<Option<Self>, Error> {
Self::try_from_url_with_hash_kind(url, DEFAULT_HASHER_KIND)
Self::try_from_url_with_hash_kind(url, &DEFAULT_HASHER_KIND)
}

/// Like [`Self::try_from_url`], but accepts an explicit [`HashKind`] for determining the crates index path.
pub fn try_from_url_with_hash_kind(url: &str, hash_kind: HashKind) -> Result<Option<Self>, Error> {
pub fn try_from_url_with_hash_kind(url: &str, hash_kind: &HashKind) -> Result<Option<Self>, Error> {
let (path, canonical_url) = local_path_and_canonical_url_with_hash_kind(url, None, hash_kind)?;
Self::from_path_and_url(path, canonical_url, Mode::ReadOnly)
}
Expand Down
8 changes: 4 additions & 4 deletions src/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ impl SparseIndex {
/// Note this function takes the `CARGO_HOME` environment variable into account
#[inline]
pub fn from_url(url: &str) -> Result<Self, Error> {
Self::from_url_with_hash_kind(url, DEFAULT_HASHER_KIND)
Self::from_url_with_hash_kind(url, &DEFAULT_HASHER_KIND)
}

/// Like [`Self::from_url`] but accepts an explicit [`HashKind`] for determining the crates index path.
#[inline]
pub fn from_url_with_hash_kind(url: &str, hash_kind: HashKind) -> Result<Self, Error> {
pub fn from_url_with_hash_kind(url: &str, hash_kind: &HashKind) -> Result<Self, Error> {
Self::with_path_and_hash_kind(home::cargo_home()?, url, hash_kind)
}

Expand All @@ -41,15 +41,15 @@ impl SparseIndex {
/// at the specified location
#[inline]
pub fn with_path(cargo_home: impl AsRef<Path>, url: impl AsRef<str>) -> Result<Self, Error> {
Self::with_path_and_hash_kind(cargo_home, url, DEFAULT_HASHER_KIND)
Self::with_path_and_hash_kind(cargo_home, url, &DEFAULT_HASHER_KIND)
}

/// Like [`Self::with_path`] but accepts an explicit [`HashKind`] for determining the crates index path.
#[inline]
pub fn with_path_and_hash_kind(
cargo_home: impl AsRef<Path>,
url: impl AsRef<str>,
hash_kind: HashKind,
hash_kind: &HashKind,
) -> Result<Self, Error> {
let url = url.as_ref();
// It is required to have the sparse+ scheme modifier for sparse urls as
Expand Down

0 comments on commit a77d2d0

Please sign in to comment.