Skip to content

Commit

Permalink
refactor: extract common make_dep_path to cargo_util
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Jun 1, 2021
1 parent b1684e2 commit df82a44
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 40 deletions.
9 changes: 2 additions & 7 deletions crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::git::repo;
use crate::paths;
use cargo_util::Sha256;
use cargo_util::{registry::make_dep_path, Sha256};
use flate2::write::GzEncoder;
use flate2::Compression;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -627,12 +627,7 @@ impl Package {
}
let line = json.to_string();

let file = match self.name.len() {
1 => format!("1/{}", self.name),
2 => format!("2/{}", self.name),
3 => format!("3/{}/{}", &self.name[..1], self.name),
_ => format!("{}/{}/{}", &self.name[0..2], &self.name[2..4], self.name),
};
let file = make_dep_path(&self.name, false);

let registry_path = if self.alternative {
alt_registry_path()
Expand Down
1 change: 1 addition & 0 deletions crates/cargo-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod paths;
mod process_builder;
mod process_error;
mod read2;
pub mod registry;
mod sha256;

/// Whether or not this running in a Continuous Integration environment.
Expand Down
45 changes: 45 additions & 0 deletions crates/cargo-util/src/registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/// Make a path to a dependency, which aligns to
///
/// - [index from of Cargo's index on filesystem][1], and
/// - [index from Crates.io][2].
///
/// [1]: https://docs.rs/cargo/latest/cargo/sources/registry/index.html#the-format-of-the-index
/// [2]: https://github.com/rust-lang/crates.io-index
pub fn make_dep_path(dep_name: &str, prefix_only: bool) -> String {
let (slash, name) = if prefix_only {
("", "")
} else {
("/", dep_name)
};
match dep_name.len() {
1 => format!("1{}{}", slash, name),
2 => format!("2{}{}", slash, name),
3 => format!("3/{}{}{}", &dep_name[..1], slash, name),
_ => format!("{}/{}{}{}", &dep_name[0..2], &dep_name[2..4], slash, name),
}
}

#[cfg(test)]
mod tests {
use super::make_dep_path;

#[test]
fn prefix_only() {
assert_eq!(make_dep_path("a", true), "1");
assert_eq!(make_dep_path("ab", true), "2");
assert_eq!(make_dep_path("abc", true), "3/a");
assert_eq!(make_dep_path("Abc", true), "3/A");
assert_eq!(make_dep_path("AbCd", true), "Ab/Cd");
assert_eq!(make_dep_path("aBcDe", true), "aB/cD");
}

#[test]
fn full() {
assert_eq!(make_dep_path("a", false), "1/a");
assert_eq!(make_dep_path("ab", false), "2/ab");
assert_eq!(make_dep_path("abc", false), "3/a/abc");
assert_eq!(make_dep_path("Abc", false), "3/A/Abc");
assert_eq!(make_dep_path("AbCd", false), "Ab/Cd/AbCd");
assert_eq!(make_dep_path("aBcDe", false), "aB/cD/aBcDe");
}
}
9 changes: 2 additions & 7 deletions src/cargo/sources/registry/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ use crate::sources::registry::{RegistryData, RegistryPackage, INDEX_V_MAX};
use crate::util::interning::InternedString;
use crate::util::{internal, CargoResult, Config, Filesystem, OptVersionReq, ToSemver};
use anyhow::bail;
use cargo_util::paths;
use cargo_util::{paths, registry::make_dep_path};
use log::{debug, info};
use semver::Version;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -373,12 +373,7 @@ impl<'cfg> RegistryIndex<'cfg> {
.chars()
.flat_map(|c| c.to_lowercase())
.collect::<String>();
let raw_path = match fs_name.len() {
1 => format!("1/{}", fs_name),
2 => format!("2/{}", fs_name),
3 => format!("3/{}/{}", &fs_name[..1], fs_name),
_ => format!("{}/{}/{}", &fs_name[0..2], &fs_name[2..4], fs_name),
};
let raw_path = make_dep_path(&fs_name, false);

// Attempt to handle misspellings by searching for a chain of related
// names to the original `raw_path` name. Only return summaries
Expand Down
28 changes: 2 additions & 26 deletions src/cargo/sources/registry/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::util::errors::CargoResult;
use crate::util::interning::InternedString;
use crate::util::{Config, Filesystem};
use anyhow::Context as _;
use cargo_util::{paths, Sha256};
use cargo_util::{paths, registry::make_dep_path, Sha256};
use lazycell::LazyCell;
use log::{debug, trace};
use std::cell::{Cell, Ref, RefCell};
Expand All @@ -21,15 +21,6 @@ use std::mem;
use std::path::Path;
use std::str;

fn make_dep_prefix(name: &str) -> String {
match name.len() {
1 => String::from("1"),
2 => String::from("2"),
3 => format!("3/{}", &name[..1]),
_ => format!("{}/{}", &name[0..2], &name[2..4]),
}
}

/// A remote registry is a registry that lives at a remote URL (such as
/// crates.io). The git index is cloned locally, and `.crate` files are
/// downloaded as needed and cached locally.
Expand Down Expand Up @@ -279,7 +270,7 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
{
write!(url, "/{}/{}/download", CRATE_TEMPLATE, VERSION_TEMPLATE).unwrap();
}
let prefix = make_dep_prefix(&*pkg.name());
let prefix = make_dep_path(&*pkg.name(), true);
let url = url
.replace(CRATE_TEMPLATE, &*pkg.name())
.replace(VERSION_TEMPLATE, &pkg.version().to_string())
Expand Down Expand Up @@ -343,18 +334,3 @@ impl<'cfg> Drop for RemoteRegistry<'cfg> {
self.tree.borrow_mut().take();
}
}

#[cfg(test)]
mod tests {
use super::make_dep_prefix;

#[test]
fn dep_prefix() {
assert_eq!(make_dep_prefix("a"), "1");
assert_eq!(make_dep_prefix("ab"), "2");
assert_eq!(make_dep_prefix("abc"), "3/a");
assert_eq!(make_dep_prefix("Abc"), "3/A");
assert_eq!(make_dep_prefix("AbCd"), "Ab/Cd");
assert_eq!(make_dep_prefix("aBcDe"), "aB/cD");
}
}

0 comments on commit df82a44

Please sign in to comment.