Skip to main content

cargo_util/
registry.rs

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/// <div class="warning">
7///
8/// Note: For index files, `dep_name` must have had `to_lowercase` called on it.
9///
10/// </div>
11///
12/// [1]: https://docs.rs/cargo/latest/cargo/sources/registry/index.html#the-format-of-the-index
13/// [2]: https://github.com/rust-lang/crates.io-index
14pub fn make_dep_path(dep_name: &str, prefix_only: bool) -> String {
15    let (slash, name) = if prefix_only {
16        ("", "")
17    } else {
18        ("/", dep_name)
19    };
20    match dep_name.len() {
21        1 => format!("1{}{}", slash, name),
22        2 => format!("2{}{}", slash, name),
23        3 => format!("3/{}{}{}", &dep_name[..1], slash, name),
24        _ => format!("{}/{}{}{}", &dep_name[0..2], &dep_name[2..4], slash, name),
25    }
26}
27
28pub fn crate_url(dl_template: &str, krate: &str, version: &str, sha256_checksum: &str) -> String {
29    let url = if !dl_template.contains(CRATE_TEMPLATE)
30        && !dl_template.contains(VERSION_TEMPLATE)
31        && !dl_template.contains(PREFIX_TEMPLATE)
32        && !dl_template.contains(LOWER_PREFIX_TEMPLATE)
33        && !dl_template.contains(CHECKSUM_TEMPLATE)
34    {
35        // Original format before customizing the download URL was supported.
36        format!("{dl_template}/{krate}/{version}/download")
37    } else {
38        let prefix = make_dep_path(krate, true);
39        let lowerprefix = prefix.to_lowercase();
40        dl_template
41            .replace(CRATE_TEMPLATE, krate)
42            .replace(VERSION_TEMPLATE, version)
43            .replace(PREFIX_TEMPLATE, &prefix)
44            .replace(LOWER_PREFIX_TEMPLATE, &lowerprefix)
45            .replace(CHECKSUM_TEMPLATE, sha256_checksum)
46    };
47
48    url
49}
50
51const CRATE_TEMPLATE: &str = "{crate}";
52const VERSION_TEMPLATE: &str = "{version}";
53const PREFIX_TEMPLATE: &str = "{prefix}";
54const LOWER_PREFIX_TEMPLATE: &str = "{lowerprefix}";
55const CHECKSUM_TEMPLATE: &str = "{sha256-checksum}";
56
57#[cfg(test)]
58mod tests {
59    use super::make_dep_path;
60
61    #[test]
62    fn prefix_only() {
63        assert_eq!(make_dep_path("a", true), "1");
64        assert_eq!(make_dep_path("ab", true), "2");
65        assert_eq!(make_dep_path("abc", true), "3/a");
66        assert_eq!(make_dep_path("Abc", true), "3/A");
67        assert_eq!(make_dep_path("AbCd", true), "Ab/Cd");
68        assert_eq!(make_dep_path("aBcDe", true), "aB/cD");
69    }
70
71    #[test]
72    fn full() {
73        assert_eq!(make_dep_path("a", false), "1/a");
74        assert_eq!(make_dep_path("ab", false), "2/ab");
75        assert_eq!(make_dep_path("abc", false), "3/a/abc");
76        assert_eq!(make_dep_path("Abc", false), "3/A/Abc");
77        assert_eq!(make_dep_path("AbCd", false), "Ab/Cd/AbCd");
78        assert_eq!(make_dep_path("aBcDe", false), "aB/cD/aBcDe");
79    }
80}