Skip to main content

driven/context/
extractor.rs

1//! Convention extractor
2
3use super::{NamingConventions, NamingStyle, scanner::ScanResult};
4use crate::Result;
5
6/// Extracts coding conventions from a project
7#[derive(Debug, Default)]
8pub struct ConventionExtractor;
9
10impl ConventionExtractor {
11    /// Create a new convention extractor
12    pub fn new() -> Self {
13        Self
14    }
15
16    /// Extract naming conventions from scan results
17    pub fn extract(&self, scan_result: &ScanResult) -> Result<NamingConventions> {
18        let mut conventions = NamingConventions::default();
19
20        // Detect conventions based on languages
21        if scan_result.languages.contains(&"Rust".to_string()) {
22            conventions.functions = Some(NamingStyle::SnakeCase);
23            conventions.types = Some(NamingStyle::PascalCase);
24            conventions.variables = Some(NamingStyle::SnakeCase);
25            conventions.files = Some(NamingStyle::SnakeCase);
26        } else if scan_result.languages.contains(&"TypeScript".to_string())
27            || scan_result.languages.contains(&"JavaScript".to_string())
28        {
29            conventions.functions = Some(NamingStyle::CamelCase);
30            conventions.types = Some(NamingStyle::PascalCase);
31            conventions.variables = Some(NamingStyle::CamelCase);
32            conventions.files = Some(NamingStyle::KebabCase);
33        } else if scan_result.languages.contains(&"Python".to_string()) {
34            conventions.functions = Some(NamingStyle::SnakeCase);
35            conventions.types = Some(NamingStyle::PascalCase);
36            conventions.variables = Some(NamingStyle::SnakeCase);
37            conventions.files = Some(NamingStyle::SnakeCase);
38        } else if scan_result.languages.contains(&"Go".to_string()) {
39            conventions.functions = Some(NamingStyle::CamelCase);
40            conventions.types = Some(NamingStyle::PascalCase);
41            conventions.variables = Some(NamingStyle::CamelCase);
42            conventions.files = Some(NamingStyle::SnakeCase);
43        }
44
45        Ok(conventions)
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_extract_rust_conventions() {
55        let extractor = ConventionExtractor::new();
56        let scan_result = ScanResult {
57            languages: vec!["Rust".to_string()],
58            ..Default::default()
59        };
60
61        let conventions = extractor.extract(&scan_result).unwrap();
62        assert_eq!(conventions.functions, Some(NamingStyle::SnakeCase));
63        assert_eq!(conventions.types, Some(NamingStyle::PascalCase));
64    }
65
66    #[test]
67    fn test_extract_typescript_conventions() {
68        let extractor = ConventionExtractor::new();
69        let scan_result = ScanResult {
70            languages: vec!["TypeScript".to_string()],
71            ..Default::default()
72        };
73
74        let conventions = extractor.extract(&scan_result).unwrap();
75        assert_eq!(conventions.functions, Some(NamingStyle::CamelCase));
76        assert_eq!(conventions.types, Some(NamingStyle::PascalCase));
77    }
78}