impl QualityCodeGenerator {
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub(crate) fn needs_decomposition(&self, code: &str) -> Result<bool> {
let complexity = self.estimate_complexity(code);
Ok(complexity > self.profile.thresholds.max_complexity)
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub(crate) fn decompose_function(&self, code: String) -> Result<String> {
Ok(code)
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub(crate) fn estimate_complexity(&self, code: &str) -> u32 {
let if_count = code.matches("if ").count() as u32;
let match_count = code.matches("match ").count() as u32;
let loop_count =
code.matches("for ").count() as u32 + code.matches("while ").count() as u32;
1 + if_count + match_count + loop_count
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "score_range")]
pub(crate) fn calculate_quality_score(&self, code: &str) -> Result<QualityScore> {
let complexity = self.estimate_complexity(code);
let coverage = 100.0; let tdg = if complexity <= 5 { 1 } else { complexity / 2 };
Ok(QualityScore {
overall: 100.0 - (f64::from(complexity) * 2.0),
complexity,
coverage,
tdg,
})
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "score_range")]
pub(crate) fn calculate_metrics(&self, code: &str, tests: &str) -> Result<QualityMetrics> {
Ok(QualityMetrics {
complexity: self.estimate_complexity(code),
cognitive_complexity: self.estimate_complexity(code), coverage: 100, tdg: 1, satd_count: code.matches("TODO").count() as u32,
dead_code_percentage: 0, has_doctests: code.contains("```") && code.contains("assert"),
has_property_tests: tests.contains("proptest!"),
})
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn enhance_with_features(&self, base_code: &str, features: &[String]) -> Result<String> {
let mut enhanced = base_code.to_string();
for feature in features {
enhanced.push_str(&format!("\n\n// Feature: {feature}\n"));
enhanced.push_str(&self.generate_feature_code(feature)?);
}
Ok(enhanced)
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn generate_tests(&self, code: &str) -> Result<String> {
self.test_generator.generate_tests(code)
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn generate_documentation(&self, code: &str) -> Result<String> {
self.doc_generator.generate_documentation(code)
}
fn generate_feature_code(&self, feature: &str) -> Result<String> {
Ok(format!(
r"
pub fn {}(&self) -> Result<()> {{
// Implementation for {}
Ok(())
}}
",
feature.to_lowercase().replace(' ', "_"),
feature
))
}
}