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/// [1]: https://docs.rs/cargo/latest/cargo/sources/registry/index.html#the-format-of-the-index
7/// [2]: https://github.com/rust-lang/crates.io-index
8pub fn make_dep_path(dep_name: &str, prefix_only: bool) -> String {
9    let (slash, name) = if prefix_only {
10        ("", "")
11    } else {
12        ("/", dep_name)
13    };
14    match dep_name.len() {
15        1 => format!("1{}{}", slash, name),
16        2 => format!("2{}{}", slash, name),
17        3 => format!("3/{}{}{}", &dep_name[..1], slash, name),
18        _ => format!("{}/{}{}{}", &dep_name[0..2], &dep_name[2..4], slash, name),
19    }
20}
21
22#[cfg(test)]
23mod tests {
24    use super::make_dep_path;
25
26    #[test]
27    fn prefix_only() {
28        assert_eq!(make_dep_path("a", true), "1");
29        assert_eq!(make_dep_path("ab", true), "2");
30        assert_eq!(make_dep_path("abc", true), "3/a");
31        assert_eq!(make_dep_path("Abc", true), "3/A");
32        assert_eq!(make_dep_path("AbCd", true), "Ab/Cd");
33        assert_eq!(make_dep_path("aBcDe", true), "aB/cD");
34    }
35
36    #[test]
37    fn full() {
38        assert_eq!(make_dep_path("a", false), "1/a");
39        assert_eq!(make_dep_path("ab", false), "2/ab");
40        assert_eq!(make_dep_path("abc", false), "3/a/abc");
41        assert_eq!(make_dep_path("Abc", false), "3/A/Abc");
42        assert_eq!(make_dep_path("AbCd", false), "Ab/Cd/AbCd");
43        assert_eq!(make_dep_path("aBcDe", false), "aB/cD/aBcDe");
44    }
45}