cargo_autodd/utils/
crate_utils.rs

1use std::path::Path;
2
3/// Checks if a path represents a hidden file or directory
4pub fn is_hidden(path: &Path) -> bool {
5    path.components()
6        .any(|c| c.as_os_str().to_string_lossy().starts_with('.'))
7}
8
9/// Checks if a crate name represents a standard library crate or type
10pub fn is_std_crate(name: &str) -> bool {
11    let std_crates = [
12        // Standard library crates
13        "std",
14        "core",
15        "alloc",
16        "test",
17        "proc_macro",
18        "collections",
19        // Common standard library types that might be mistaken for crates
20        "String",
21        "Vec",
22        "HashMap",
23        "HashSet",
24        "BTreeMap",
25        "BTreeSet",
26        "PathBuf",
27        "Path",
28        "Result",
29        "Option",
30        "Box",
31        "Arc",
32        "Rc",
33        "Cell",
34        "RefCell",
35        "Mutex",
36        "RwLock",
37    ];
38    std_crates.contains(&name)
39        || name.starts_with("std::")
40        || name.starts_with("core::")
41        || name.starts_with("alloc::")
42}
43
44/// Checks if a dependency is considered essential and should not be removed
45pub fn is_essential_dep(name: &str) -> bool {
46    let essential_deps = [
47        "serde",
48        "tokio",
49        "anyhow",
50        "thiserror",
51        "async-trait",
52        "futures",
53    ];
54    essential_deps.contains(&name)
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_is_hidden() {
63        assert!(is_hidden(Path::new(".git")));
64        assert!(is_hidden(Path::new("/path/to/.hidden")));
65        assert!(!is_hidden(Path::new("visible")));
66        assert!(!is_hidden(Path::new("/path/to/visible")));
67    }
68
69    #[test]
70    fn test_is_std_crate() {
71        // Standard library crates
72        assert!(is_std_crate("std"));
73        assert!(is_std_crate("core"));
74        assert!(is_std_crate("alloc"));
75        assert!(is_std_crate("proc_macro"));
76
77        // Standard library types
78        assert!(is_std_crate("String"));
79        assert!(is_std_crate("Vec"));
80        assert!(is_std_crate("HashMap"));
81
82        // Paths starting with std/core/alloc
83        assert!(is_std_crate("std::collections"));
84        assert!(is_std_crate("core::mem"));
85        assert!(is_std_crate("alloc::vec"));
86
87        // External crates (NOT std)
88        assert!(!is_std_crate("serde"));
89        assert!(!is_std_crate("tokio"));
90        assert!(!is_std_crate("rand")); // rand is NOT a std crate
91        assert!(!is_std_crate("libc")); // libc is NOT a std crate
92        assert!(!is_std_crate("custom_crate"));
93    }
94
95    #[test]
96    fn test_is_essential_dep() {
97        assert!(is_essential_dep("serde"));
98        assert!(is_essential_dep("tokio"));
99        assert!(!is_essential_dep("custom_crate"));
100        assert!(!is_essential_dep("std"));
101    }
102}