Skip to content

Commit cdd6dc7

Browse files
committed
Auto merge of #9529 - weihanglo:refactor/make_dep_path, r=alexcrichton
Extract common `make_dep_path` to cargo_util Just a teeny tiny refactor extracting the same logic.
2 parents 51eac9c + df82a44 commit cdd6dc7

File tree

5 files changed

+52
-40
lines changed

5 files changed

+52
-40
lines changed

crates/cargo-test-support/src/registry.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::git::repo;
22
use crate::paths;
3-
use cargo_util::Sha256;
3+
use cargo_util::{registry::make_dep_path, Sha256};
44
use flate2::write::GzEncoder;
55
use flate2::Compression;
66
use std::collections::BTreeMap;
@@ -627,12 +627,7 @@ impl Package {
627627
}
628628
let line = json.to_string();
629629

630-
let file = match self.name.len() {
631-
1 => format!("1/{}", self.name),
632-
2 => format!("2/{}", self.name),
633-
3 => format!("3/{}/{}", &self.name[..1], self.name),
634-
_ => format!("{}/{}/{}", &self.name[0..2], &self.name[2..4], self.name),
635-
};
630+
let file = make_dep_path(&self.name, false);
636631

637632
let registry_path = if self.alternative {
638633
alt_registry_path()

crates/cargo-util/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod paths;
99
mod process_builder;
1010
mod process_error;
1111
mod read2;
12+
pub mod registry;
1213
mod sha256;
1314

1415
/// Whether or not this running in a Continuous Integration environment.

crates/cargo-util/src/registry.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/// Make a path to a dependency, which aligns to
2+
///
3+
/// - [index from of Cargo's index on filesystem][1], and
4+
/// - [index from Crates.io][2].
5+
///
6+
/// [1]: https://docs.rs/cargo/latest/cargo/sources/registry/index.html#the-format-of-the-index
7+
/// [2]: https://github.com/rust-lang/crates.io-index
8+
pub fn make_dep_path(dep_name: &str, prefix_only: bool) -> String {
9+
let (slash, name) = if prefix_only {
10+
("", "")
11+
} else {
12+
("/", dep_name)
13+
};
14+
match dep_name.len() {
15+
1 => format!("1{}{}", slash, name),
16+
2 => format!("2{}{}", slash, name),
17+
3 => format!("3/{}{}{}", &dep_name[..1], slash, name),
18+
_ => format!("{}/{}{}{}", &dep_name[0..2], &dep_name[2..4], slash, name),
19+
}
20+
}
21+
22+
#[cfg(test)]
23+
mod tests {
24+
use super::make_dep_path;
25+
26+
#[test]
27+
fn prefix_only() {
28+
assert_eq!(make_dep_path("a", true), "1");
29+
assert_eq!(make_dep_path("ab", true), "2");
30+
assert_eq!(make_dep_path("abc", true), "3/a");
31+
assert_eq!(make_dep_path("Abc", true), "3/A");
32+
assert_eq!(make_dep_path("AbCd", true), "Ab/Cd");
33+
assert_eq!(make_dep_path("aBcDe", true), "aB/cD");
34+
}
35+
36+
#[test]
37+
fn full() {
38+
assert_eq!(make_dep_path("a", false), "1/a");
39+
assert_eq!(make_dep_path("ab", false), "2/ab");
40+
assert_eq!(make_dep_path("abc", false), "3/a/abc");
41+
assert_eq!(make_dep_path("Abc", false), "3/A/Abc");
42+
assert_eq!(make_dep_path("AbCd", false), "Ab/Cd/AbCd");
43+
assert_eq!(make_dep_path("aBcDe", false), "aB/cD/aBcDe");
44+
}
45+
}

src/cargo/sources/registry/index.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use crate::sources::registry::{RegistryData, RegistryPackage, INDEX_V_MAX};
7272
use crate::util::interning::InternedString;
7373
use crate::util::{internal, CargoResult, Config, Filesystem, OptVersionReq, ToSemver};
7474
use anyhow::bail;
75-
use cargo_util::paths;
75+
use cargo_util::{paths, registry::make_dep_path};
7676
use log::{debug, info};
7777
use semver::Version;
7878
use std::collections::{HashMap, HashSet};
@@ -373,12 +373,7 @@ impl<'cfg> RegistryIndex<'cfg> {
373373
.chars()
374374
.flat_map(|c| c.to_lowercase())
375375
.collect::<String>();
376-
let raw_path = match fs_name.len() {
377-
1 => format!("1/{}", fs_name),
378-
2 => format!("2/{}", fs_name),
379-
3 => format!("3/{}/{}", &fs_name[..1], fs_name),
380-
_ => format!("{}/{}/{}", &fs_name[0..2], &fs_name[2..4], fs_name),
381-
};
376+
let raw_path = make_dep_path(&fs_name, false);
382377

383378
// Attempt to handle misspellings by searching for a chain of related
384379
// names to the original `raw_path` name. Only return summaries

src/cargo/sources/registry/remote.rs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::util::errors::CargoResult;
99
use crate::util::interning::InternedString;
1010
use crate::util::{Config, Filesystem};
1111
use anyhow::Context as _;
12-
use cargo_util::{paths, Sha256};
12+
use cargo_util::{paths, registry::make_dep_path, Sha256};
1313
use lazycell::LazyCell;
1414
use log::{debug, trace};
1515
use std::cell::{Cell, Ref, RefCell};
@@ -21,15 +21,6 @@ use std::mem;
2121
use std::path::Path;
2222
use std::str;
2323

24-
fn make_dep_prefix(name: &str) -> String {
25-
match name.len() {
26-
1 => String::from("1"),
27-
2 => String::from("2"),
28-
3 => format!("3/{}", &name[..1]),
29-
_ => format!("{}/{}", &name[0..2], &name[2..4]),
30-
}
31-
}
32-
3324
/// A remote registry is a registry that lives at a remote URL (such as
3425
/// crates.io). The git index is cloned locally, and `.crate` files are
3526
/// downloaded as needed and cached locally.
@@ -279,7 +270,7 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
279270
{
280271
write!(url, "/{}/{}/download", CRATE_TEMPLATE, VERSION_TEMPLATE).unwrap();
281272
}
282-
let prefix = make_dep_prefix(&*pkg.name());
273+
let prefix = make_dep_path(&*pkg.name(), true);
283274
let url = url
284275
.replace(CRATE_TEMPLATE, &*pkg.name())
285276
.replace(VERSION_TEMPLATE, &pkg.version().to_string())
@@ -343,18 +334,3 @@ impl<'cfg> Drop for RemoteRegistry<'cfg> {
343334
self.tree.borrow_mut().take();
344335
}
345336
}
346-
347-
#[cfg(test)]
348-
mod tests {
349-
use super::make_dep_prefix;
350-
351-
#[test]
352-
fn dep_prefix() {
353-
assert_eq!(make_dep_prefix("a"), "1");
354-
assert_eq!(make_dep_prefix("ab"), "2");
355-
assert_eq!(make_dep_prefix("abc"), "3/a");
356-
assert_eq!(make_dep_prefix("Abc"), "3/A");
357-
assert_eq!(make_dep_prefix("AbCd"), "Ab/Cd");
358-
assert_eq!(make_dep_prefix("aBcDe"), "aB/cD");
359-
}
360-
}

0 commit comments

Comments
 (0)