🏡 index : ~doyle/gitlab-cargo-shim.git

author jordan <jordan@doyle.la> 2022-09-13 16:03:18.0 +00:00:00
committer GitHub <noreply@github.com> 2022-09-13 16:03:18.0 +00:00:00
commit
19aafa0cf39306c063f9d1f7bc5638f9d7514d35 [patch]
tree
309cbe3146ef0ec7b9eab9aa8ecb63e4e6991c3e
parent
a694c19042746d2fde425e927ec9866e3c69ee83
parent
1e2fe7d8e56419886de1af76614180df2ff688d6
download
19aafa0cf39306c063f9d1f7bc5638f9d7514d35.tar.gz

Merge pull request #34 from Eijebong/fix-3-letter-crates



Diff

 src/util.rs | 39 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 34 insertions(+), 5 deletions(-)

diff --git a/src/util.rs b/src/util.rs
index 1ec0df9..aeec7ef 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -12,10 +12,11 @@ pub fn format_fingerprint(fingerprint: &str) -> String {
    format!("SHA256:{}", fingerprint)
}

/// Crates with a total of 1, 2 or 3 characters in the same are written out to directories named
/// 1, 2 or 3 respectively as per the cargo spec. Anything else we'll build out a normal tree for
/// using the first four characters of the crate name, 2 for the first directory and the other 2
/// for the second.
/// Crates with a total of 1, 2 characters in the same are written out to directories named
/// 1, 2 respectively as per the cargo spec. With a total of 3 characters they're stored in a
/// directory named 3 and then a subdirectory named after the first letter of the crate's name.
/// Anything else we'll build out a normal tree for using the first four characters of the crate
/// name, 2 for the first directory and the other 2 for the second.
#[must_use]
pub fn get_crate_folder(crate_name: &str) -> ArrayVec<&'static str, 2> {
    let mut folders = ArrayVec::new();
@@ -24,7 +25,10 @@ pub fn get_crate_folder(crate_name: &str) -> ArrayVec<&'static str, 2> {
        0 => {}
        1 => folders.push("1"),
        2 => folders.push("2"),
        3 => folders.push("3"),
        3 => {
            folders.push("3");
            folders.push(ustr(&crate_name[..1]).as_str());
        }
        _ => {
            folders.push(ustr(&crate_name[..2]).as_str());
            folders.push(ustr(&crate_name[2..4]).as_str());
@@ -86,3 +90,28 @@ impl Display for ArcOrCowStr {
        std::fmt::Display::fmt(&**self, f)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_crate_paths() {
        let result = get_crate_folder("dfu");
        let expected = ArrayVec::from(["3", "d"]);
        assert_eq!(result, expected);
        let result = get_crate_folder("df");
        let mut expected = ArrayVec::new();
        expected.push("2");
        assert_eq!(result, expected);

        let result = get_crate_folder("d");
        let mut expected = ArrayVec::new();
        expected.push("1");
        assert_eq!(result, expected);

        let result = get_crate_folder("longname");
        let expected = ArrayVec::from(["lo", "ng"]);
        assert_eq!(result, expected);
    }
}