mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-28 11:20:36 +00:00
refactor: extract common make_dep_path
to cargo_util
This commit is contained in:
parent
b1684e2849
commit
df82a44f9e
@ -1,6 +1,6 @@
|
|||||||
use crate::git::repo;
|
use crate::git::repo;
|
||||||
use crate::paths;
|
use crate::paths;
|
||||||
use cargo_util::Sha256;
|
use cargo_util::{registry::make_dep_path, Sha256};
|
||||||
use flate2::write::GzEncoder;
|
use flate2::write::GzEncoder;
|
||||||
use flate2::Compression;
|
use flate2::Compression;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
@ -627,12 +627,7 @@ impl Package {
|
|||||||
}
|
}
|
||||||
let line = json.to_string();
|
let line = json.to_string();
|
||||||
|
|
||||||
let file = match self.name.len() {
|
let file = make_dep_path(&self.name, false);
|
||||||
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 registry_path = if self.alternative {
|
let registry_path = if self.alternative {
|
||||||
alt_registry_path()
|
alt_registry_path()
|
||||||
|
@ -9,6 +9,7 @@ pub mod paths;
|
|||||||
mod process_builder;
|
mod process_builder;
|
||||||
mod process_error;
|
mod process_error;
|
||||||
mod read2;
|
mod read2;
|
||||||
|
pub mod registry;
|
||||||
mod sha256;
|
mod sha256;
|
||||||
|
|
||||||
/// Whether or not this running in a Continuous Integration environment.
|
/// Whether or not this running in a Continuous Integration environment.
|
||||||
|
45
crates/cargo-util/src/registry.rs
Normal file
45
crates/cargo-util/src/registry.rs
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
@ -72,7 +72,7 @@ use crate::sources::registry::{RegistryData, RegistryPackage, INDEX_V_MAX};
|
|||||||
use crate::util::interning::InternedString;
|
use crate::util::interning::InternedString;
|
||||||
use crate::util::{internal, CargoResult, Config, Filesystem, OptVersionReq, ToSemver};
|
use crate::util::{internal, CargoResult, Config, Filesystem, OptVersionReq, ToSemver};
|
||||||
use anyhow::bail;
|
use anyhow::bail;
|
||||||
use cargo_util::paths;
|
use cargo_util::{paths, registry::make_dep_path};
|
||||||
use log::{debug, info};
|
use log::{debug, info};
|
||||||
use semver::Version;
|
use semver::Version;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
@ -373,12 +373,7 @@ impl<'cfg> RegistryIndex<'cfg> {
|
|||||||
.chars()
|
.chars()
|
||||||
.flat_map(|c| c.to_lowercase())
|
.flat_map(|c| c.to_lowercase())
|
||||||
.collect::<String>();
|
.collect::<String>();
|
||||||
let raw_path = match fs_name.len() {
|
let raw_path = make_dep_path(&fs_name, false);
|
||||||
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),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Attempt to handle misspellings by searching for a chain of related
|
// Attempt to handle misspellings by searching for a chain of related
|
||||||
// names to the original `raw_path` name. Only return summaries
|
// names to the original `raw_path` name. Only return summaries
|
||||||
|
@ -9,7 +9,7 @@ use crate::util::errors::CargoResult;
|
|||||||
use crate::util::interning::InternedString;
|
use crate::util::interning::InternedString;
|
||||||
use crate::util::{Config, Filesystem};
|
use crate::util::{Config, Filesystem};
|
||||||
use anyhow::Context as _;
|
use anyhow::Context as _;
|
||||||
use cargo_util::{paths, Sha256};
|
use cargo_util::{paths, registry::make_dep_path, Sha256};
|
||||||
use lazycell::LazyCell;
|
use lazycell::LazyCell;
|
||||||
use log::{debug, trace};
|
use log::{debug, trace};
|
||||||
use std::cell::{Cell, Ref, RefCell};
|
use std::cell::{Cell, Ref, RefCell};
|
||||||
@ -21,15 +21,6 @@ use std::mem;
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::str;
|
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
|
/// 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
|
/// crates.io). The git index is cloned locally, and `.crate` files are
|
||||||
/// downloaded as needed and cached locally.
|
/// downloaded as needed and cached locally.
|
||||||
@ -279,7 +270,7 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
|
|||||||
{
|
{
|
||||||
write!(url, "/{}/{}/download", CRATE_TEMPLATE, VERSION_TEMPLATE).unwrap();
|
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
|
let url = url
|
||||||
.replace(CRATE_TEMPLATE, &*pkg.name())
|
.replace(CRATE_TEMPLATE, &*pkg.name())
|
||||||
.replace(VERSION_TEMPLATE, &pkg.version().to_string())
|
.replace(VERSION_TEMPLATE, &pkg.version().to_string())
|
||||||
@ -343,18 +334,3 @@ impl<'cfg> Drop for RemoteRegistry<'cfg> {
|
|||||||
self.tree.borrow_mut().take();
|
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user