Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
- replace boolean with an enum to make purpose obvious
- remove legacy hex implementation
  • Loading branch information
Byron committed Feb 8, 2025
1 parent 59cd40e commit 285913d
Showing 1 changed file with 22 additions and 47 deletions.
69 changes: 22 additions & 47 deletions src/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ pub fn local_path_and_canonical_url(
path.push("registry");
path.push("index");

let (dir_name, canonical) = url_to_local_dir(url, true)?;
let (dir_name, canonical) = url_to_local_dir(url, HashKind::Stable)?;
let canonical_url = if path.join(&dir_name).exists() {
path.push(dir_name);
canonical
} else {
let (dir_name, canonical) = url_to_local_dir(url, false)?;
let (dir_name, canonical) = url_to_local_dir(url, HashKind::Legacy)?;
path.push(dir_name);
canonical
};
Expand Down Expand Up @@ -77,37 +77,14 @@ pub(crate) fn crate_name_to_relative_path(crate_name: &str, separator: Option<ch
Some(rel_path)
}

enum HashKind {
Stable,
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, use_stable_hash: bool) -> Result<(String, String), Error> {
fn legacy_to_hex(num: u64) -> String {
const CHARS: &[u8] = b"0123456789abcdef";

let bytes = &[
num as u8,
(num >> 8) as u8,
(num >> 16) as u8,
(num >> 24) as u8,
(num >> 32) as u8,
(num >> 40) as u8,
(num >> 48) as u8,
(num >> 56) as u8,
];

let mut output = vec![0u8; 16];

let mut ind = 0;

for &byte in bytes {
output[ind] = CHARS[(byte >> 4) as usize];
output[ind + 1] = CHARS[(byte & 0xf) as usize];

ind += 2;
}

String::from_utf8(output).expect("valid utf-8 hex string")
}

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 All @@ -120,7 +97,7 @@ fn url_to_local_dir(url: &str, use_stable_hash: bool) -> Result<(String, String)
hasher.finish()
}

fn stable_to_hex(num: u64) -> String {
fn to_hex(num: u64) -> String {
hex::encode(num.to_le_bytes())
}

Expand All @@ -137,18 +114,13 @@ fn url_to_local_dir(url: &str, use_stable_hash: bool) -> Result<(String, String)
Hasher::finish(&hasher)
}

let hash_u64 = if use_stable_hash {
let is_stable_hash = matches!(hash_kind, HashKind::Stable);
let hash_u64 = if is_stable_hash {
stable_hash_u64
} else {
legacy_hash_u64
};

let to_hex = if use_stable_hash {
stable_to_hex
} else {
legacy_to_hex
};

// SourceKind::Registry
let mut registry_kind = 2;

Expand Down Expand Up @@ -221,16 +193,17 @@ fn url_to_local_dir(url: &str, use_stable_hash: bool) -> Result<(String, String)

#[cfg(test)]
mod test {
use crate::dirs::HashKind;

#[test]
fn http_index_url_matches_cargo() {
use crate::sparse::URL;
assert_eq!(
super::url_to_local_dir(URL, false).unwrap(),
super::url_to_local_dir(dbg!(URL), HashKind::Legacy).unwrap(),
("index.crates.io-6f17d22bba15001f".to_owned(), URL.to_owned(),)
);
assert_eq!(
super::url_to_local_dir(URL, true).unwrap(),
super::url_to_local_dir(URL, HashKind::Stable).unwrap(),
("index.crates.io-1949cf8c6b5b557f".to_owned(), URL.to_owned(),)
);

Expand All @@ -240,7 +213,7 @@ mod test {
assert_eq!(
super::url_to_local_dir(
"https://dl.cloudsmith.io/aBcW1234aBcW1234/embark/rust/cargo/index.git",
false
HashKind::Legacy
)
.unwrap(),
(
Expand All @@ -251,7 +224,7 @@ mod test {
assert_eq!(
super::url_to_local_dir(
"https://dl.cloudsmith.io/aBcW1234aBcW1234/embark/rust/cargo/index.git",
true
HashKind::Stable
)
.unwrap(),
(
Expand All @@ -266,22 +239,24 @@ mod test {
fn git_url_matches_cargo() {
use crate::git::URL;
assert_eq!(
crate::dirs::url_to_local_dir(URL, false).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, true).unwrap(),
crate::dirs::url_to_local_dir(URL, HashKind::Stable).unwrap(),
("github.com-1ecc6299db9ec823".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), false).unwrap(),
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), true).unwrap(),
crate::dirs::url_to_local_dir(&format!("registry+{}.git?one=1&two=2#fragment", URL), HashKind::Stable)
.unwrap(),
("github.com-c786010fb7ef2e6e".to_owned(), URL.to_owned())
);
}
Expand Down

0 comments on commit 285913d

Please sign in to comment.