features_cli/
checker.rs

1use anyhow::Result;
2use std::collections::HashMap;
3
4use crate::models::Feature;
5
6pub fn run_checks(features: &[Feature]) -> Result<()> {
7    let mut error_count = 0;
8
9    error_count += check_duplicate_names(features);
10
11    if error_count > 0 {
12        anyhow::bail!("Check failed: {} error(s) found", error_count);
13    }
14
15    eprintln!("All checks passed successfully.");
16    Ok(())
17}
18
19fn collect_features(features: &[Feature], name_to_paths: &mut HashMap<String, Vec<String>>) {
20    for feature in features {
21        name_to_paths
22            .entry(feature.name.clone())
23            .or_default()
24            .push(feature.path.clone());
25
26        collect_features(&feature.features, name_to_paths);
27    }
28}
29
30fn check_duplicate_names(features: &[Feature]) -> usize {
31    let mut name_to_paths: HashMap<String, Vec<String>> = HashMap::new();
32
33    collect_features(features, &mut name_to_paths);
34
35    let mut error_count = 0;
36
37    for (name, paths) in name_to_paths.iter() {
38        if paths.len() > 1 {
39            error_count += 1;
40            eprintln!(
41                "Error: Duplicate feature name '{}' found in {} locations:",
42                name,
43                paths.len()
44            );
45            for path in paths {
46                eprintln!("  - {}", path);
47            }
48        }
49    }
50
51    error_count
52}