Skip to main content

ferrous_forge/standards/
implementation.rs

1//! Implementation of coding standards methods
2
3use super::types::*;
4use crate::Result;
5
6impl CodingStandards {
7    /// Load standards from configuration
8    ///
9    /// # Errors
10    ///
11    /// Currently infallible, but returns `Result` for future configuration loading.
12    pub fn load() -> Result<Self> {
13        // For now, return defaults
14        // TODO: Load from configuration file or remote source
15        Ok(Self::default())
16    }
17
18    /// Save standards to configuration
19    ///
20    /// # Errors
21    ///
22    /// Currently infallible, but returns `Result` for future configuration saving.
23    pub fn save(&self) -> Result<()> {
24        // TODO: Save to configuration file
25        Ok(())
26    }
27
28    /// Get all clippy rules based on these standards
29    pub fn get_clippy_rules(&self) -> Vec<String> {
30        let mut rules = vec!["-D warnings".to_string()];
31
32        if self.banned_patterns.ban_unwrap {
33            rules.push("-D clippy::unwrap_used".to_string());
34        }
35
36        if self.banned_patterns.ban_expect {
37            rules.push("-D clippy::expect_used".to_string());
38        }
39
40        if self.banned_patterns.ban_panic {
41            rules.push("-D clippy::panic".to_string());
42        }
43
44        if self.banned_patterns.ban_todo {
45            rules.push("-D clippy::todo".to_string());
46        }
47
48        if self.banned_patterns.ban_unimplemented {
49            rules.push("-D clippy::unimplemented".to_string());
50        }
51
52        if self.documentation.require_public_docs {
53            rules.push("-D missing_docs".to_string());
54        }
55
56        if self.security.ban_unsafe {
57            rules.push("-F unsafe_code".to_string());
58        }
59
60        // Add performance and style lints
61        rules.extend([
62            "-W clippy::pedantic".to_string(),
63            "-W clippy::nursery".to_string(),
64            "-W clippy::cargo".to_string(),
65            "-D clippy::dbg_macro".to_string(),
66            "-D clippy::print_stdout".to_string(),
67            "-D clippy::print_stderr".to_string(),
68        ]);
69
70        rules
71    }
72
73    /// Generate clippy.toml configuration
74    pub fn generate_clippy_config(&self) -> String {
75        format!(
76            r#"# Ferrous Forge - Rust Standards Enforcement
77# Generated automatically - do not edit manually
78
79msrv = "{}"
80max-fn-params-bools = 2
81max-struct-bools = 2
82max-trait-bounds = 2
83max-include-file-size = {}
84min-ident-chars-threshold = 2
85literal-representation-threshold = 1000
86check-private-items = {}
87missing-docs-allow-unused = false
88allow-comparison-to-zero = false
89allow-mixed-uninlined-format-args = false
90allow-one-hash-in-raw-strings = false
91allow-useless-vec-in-tests = false
92allow-indexing-slicing-in-tests = false
93allowed-idents-below-min-chars = ["i", "j", "x", "y", "z"]
94allowed-wildcard-imports = []
95allow-exact-repetitions = false
96allow-private-module-inception = false
97too-large-for-stack = 100
98upper-case-acronyms-aggressive = true
99allowed-scripts = ["Latin"]
100disallowed-names = ["foo", "bar", "baz", "qux", "quux", "test", "tmp", "temp"]
101unreadable-literal-lint-fractions = true
102semicolon-inside-block-ignore-singleline = false
103semicolon-outside-block-ignore-multiline = false
104arithmetic-side-effects-allowed = []
105"#,
106            self.edition.min_rust_version,
107            self.file_limits.max_lines * 1000, // Convert to bytes approximation
108            self.documentation.require_private_docs,
109        )
110    }
111
112    /// Check if a project complies with these standards
113    ///
114    /// # Errors
115    ///
116    /// Returns an error if reading `Cargo.toml` fails.
117    pub async fn check_compliance(&self, project_path: &std::path::Path) -> Result<Vec<String>> {
118        let mut violations = Vec::new();
119
120        // Check Cargo.toml for edition
121        let cargo_toml = project_path.join("Cargo.toml");
122        if cargo_toml.exists() {
123            let content = tokio::fs::read_to_string(&cargo_toml).await?;
124            if !content.contains(&format!(r#"edition = "{}""#, self.edition.required_edition)) {
125                violations.push(format!(
126                    "Project must use Rust Edition {}",
127                    self.edition.required_edition
128                ));
129            }
130        }
131
132        // Additional compliance checks would go here
133
134        Ok(violations)
135    }
136}