refactor: extract common make_dep_path to cargo_util

This commit is contained in:
Weihang Lo 2021-06-01 10:28:36 +08:00
parent b1684e2849
commit df82a44f9e
No known key found for this signature in database
GPG Key ID: D7DBF189825E82E7
5 changed files with 52 additions and 40 deletions

View File

@ -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;
@ -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()

View File

@ -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.

View File

@ -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");
}
}

View File

@ -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};
@ -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

View File

@ -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};
@ -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.
@ -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())
@ -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");
}
}