Skip to content

Commit 19aafa0

Browse files
authored
Merge pull request #34 from Eijebong/fix-3-letter-crates
2 parents a694c19 + 1e2fe7d commit 19aafa0

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

src/util.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ pub fn format_fingerprint(fingerprint: &str) -> String {
1212
format!("SHA256:{}", fingerprint)
1313
}
1414

15-
/// Crates with a total of 1, 2 or 3 characters in the same are written out to directories named
16-
/// 1, 2 or 3 respectively as per the cargo spec. Anything else we'll build out a normal tree for
17-
/// using the first four characters of the crate name, 2 for the first directory and the other 2
18-
/// for the second.
15+
/// Crates with a total of 1, 2 characters in the same are written out to directories named
16+
/// 1, 2 respectively as per the cargo spec. With a total of 3 characters they're stored in a
17+
/// directory named 3 and then a subdirectory named after the first letter of the crate's name.
18+
/// Anything else we'll build out a normal tree for using the first four characters of the crate
19+
/// name, 2 for the first directory and the other 2 for the second.
1920
#[must_use]
2021
pub fn get_crate_folder(crate_name: &str) -> ArrayVec<&'static str, 2> {
2122
let mut folders = ArrayVec::new();
@@ -24,7 +25,10 @@ pub fn get_crate_folder(crate_name: &str) -> ArrayVec<&'static str, 2> {
2425
0 => {}
2526
1 => folders.push("1"),
2627
2 => folders.push("2"),
27-
3 => folders.push("3"),
28+
3 => {
29+
folders.push("3");
30+
folders.push(ustr(&crate_name[..1]).as_str());
31+
}
2832
_ => {
2933
folders.push(ustr(&crate_name[..2]).as_str());
3034
folders.push(ustr(&crate_name[2..4]).as_str());
@@ -86,3 +90,28 @@ impl Display for ArcOrCowStr {
8690
std::fmt::Display::fmt(&**self, f)
8791
}
8892
}
93+
94+
#[cfg(test)]
95+
mod tests {
96+
use super::*;
97+
98+
#[test]
99+
fn test_crate_paths() {
100+
let result = get_crate_folder("dfu");
101+
let expected = ArrayVec::from(["3", "d"]);
102+
assert_eq!(result, expected);
103+
let result = get_crate_folder("df");
104+
let mut expected = ArrayVec::new();
105+
expected.push("2");
106+
assert_eq!(result, expected);
107+
108+
let result = get_crate_folder("d");
109+
let mut expected = ArrayVec::new();
110+
expected.push("1");
111+
assert_eq!(result, expected);
112+
113+
let result = get_crate_folder("longname");
114+
let expected = ArrayVec::from(["lo", "ng"]);
115+
assert_eq!(result, expected);
116+
}
117+
}

0 commit comments

Comments
 (0)