cargo_autodd/models/
crate_reference.rs

1use std::collections::HashSet;
2use std::path::PathBuf;
3
4/// A reference to a crate and its usage within the project
5#[derive(Debug, Clone)]
6pub struct CrateReference {
7    /// Name of the crate
8    pub name: String,
9    /// Set of features used by this crate
10    pub features: HashSet<String>,
11    /// Set of file paths where this crate is used
12    pub used_in: HashSet<PathBuf>,
13}
14
15impl CrateReference {
16    pub fn new(name: String) -> Self {
17        Self {
18            name,
19            features: HashSet::new(),
20            used_in: HashSet::new(),
21        }
22    }
23
24    pub fn add_usage(&mut self, path: PathBuf) {
25        self.used_in.insert(path);
26    }
27
28    pub fn add_feature(&mut self, feature: String) {
29        self.features.insert(feature);
30    }
31
32    pub fn usage_count(&self) -> usize {
33        self.used_in.len()
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40    use std::path::Path;
41
42    #[test]
43    fn test_new_crate_reference() {
44        let crate_ref = CrateReference::new("test_crate".to_string());
45        assert_eq!(crate_ref.name, "test_crate");
46        assert!(crate_ref.features.is_empty());
47        assert!(crate_ref.used_in.is_empty());
48    }
49
50    #[test]
51    fn test_add_usage() {
52        let mut crate_ref = CrateReference::new("test_crate".to_string());
53        let path = Path::new("/test/path.rs").to_path_buf();
54        crate_ref.add_usage(path.clone());
55        assert!(crate_ref.used_in.contains(&path));
56        assert_eq!(crate_ref.usage_count(), 1);
57    }
58
59    #[test]
60    fn test_add_feature() {
61        let mut crate_ref = CrateReference::new("test_crate".to_string());
62        crate_ref.add_feature("test_feature".to_string());
63        assert!(crate_ref.features.contains("test_feature"));
64    }
65}