#
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
    28#[cfg(test)]
    29mod tests {
    30    use super::make_dep_path;
    31
    32    #[test]
    33    fn prefix_only() {
    34        assert_eq!(make_dep_path("a", true), "1");
    35        assert_eq!(make_dep_path("ab", true), "2");
    36        assert_eq!(make_dep_path("abc", true), "3/a");
    37        assert_eq!(make_dep_path("Abc", true), "3/A");
    38        assert_eq!(make_dep_path("AbCd", true), "Ab/Cd");
    39        assert_eq!(make_dep_path("aBcDe", true), "aB/cD");
    40    }
    41
    42    #[test]
    43    fn full() {
    44        assert_eq!(make_dep_path("a", false), "1/a");
    45        assert_eq!(make_dep_path("ab", false), "2/ab");
    46        assert_eq!(make_dep_path("abc", false), "3/a/abc");
    47        assert_eq!(make_dep_path("Abc", false), "3/A/Abc");
    48        assert_eq!(make_dep_path("AbCd", false), "Ab/Cd/AbCd");
    49        assert_eq!(make_dep_path("aBcDe", false), "aB/cD/aBcDe");
    50    }
    51}