use crate::complexity::ProjectComplexity;
use crabscore_core::{
metrics::{CostMetrics, EnergyMetrics, PerformanceMetrics, SafetyMetrics},
scoring::ScoringEngine,
CrabScore, IndustryProfile,
};
pub struct ComplexityAwareScoringEngine {
base_engine: ScoringEngine,
complexity: ProjectComplexity,
}
impl ComplexityAwareScoringEngine {
pub fn new(profile: IndustryProfile, complexity: ProjectComplexity) -> Self {
Self {
base_engine: ScoringEngine::new(profile),
complexity,
}
}
pub fn calculate_score(
&self,
performance: &PerformanceMetrics,
energy: &EnergyMetrics,
cost: &CostMetrics,
safety: &SafetyMetrics,
) -> CrabScore {
let mut score = self
.base_engine
.calculate_score(performance, energy, cost, safety);
let bonus = self.calculate_complexity_bonus();
score.bonuses += bonus;
score.overall += bonus;
score.metadata.measurements.environment.os = std::env::consts::OS.to_string();
score.metadata.measurements.environment.rust_version = "1.88.0".to_string();
score
}
fn calculate_complexity_bonus(&self) -> f64 {
let mut bonus: f64 = 0.0;
if self.complexity.total_lines < 100 {
bonus += 2.0;
} else if self.complexity.total_lines < 500 {
bonus += 1.0;
}
let doc_ratio = self.complexity.doc_coverage();
if doc_ratio > 0.2 {
bonus += 2.0; } else if doc_ratio > 0.1 {
bonus += 1.0; }
let test_ratio = self.complexity.test_coverage();
if test_ratio > 0.8 {
bonus += 3.0; } else if test_ratio > 0.5 {
bonus += 2.0; } else if test_ratio > 0.2 {
bonus += 1.0; }
if self.complexity.dependency_count == 0 {
bonus += 3.0; } else if self.complexity.dependency_count < 5 {
bonus += 2.0; } else if self.complexity.dependency_count < 10 {
bonus += 1.0; }
bonus.min(10.0)
}
pub fn get_bonus_breakdown(&self) -> Vec<(String, f64)> {
let mut bonuses = Vec::new();
if self.complexity.total_lines < 100 {
bonuses.push(("Small Project Bonus".to_string(), 2.0));
} else if self.complexity.total_lines < 500 {
bonuses.push(("Compact Project Bonus".to_string(), 1.0));
}
let doc_ratio = self.complexity.doc_coverage();
if doc_ratio > 0.2 {
bonuses.push(("Excellent Documentation".to_string(), 2.0));
} else if doc_ratio > 0.1 {
bonuses.push(("Good Documentation".to_string(), 1.0));
}
let test_ratio = self.complexity.test_coverage();
if test_ratio > 0.8 {
bonuses.push(("Excellent Tests".to_string(), 3.0));
} else if test_ratio > 0.5 {
bonuses.push(("Good Test Coverage".to_string(), 2.0));
} else if test_ratio > 0.2 {
bonuses.push(("Basic Test Coverage".to_string(), 1.0));
}
if self.complexity.dependency_count == 0 {
bonuses.push(("Zero Dependencies".to_string(), 3.0));
} else if self.complexity.dependency_count < 5 {
bonuses.push(("Minimal Dependencies".to_string(), 2.0));
} else if self.complexity.dependency_count < 10 {
bonuses.push(("Reasonable Dependencies".to_string(), 1.0));
}
bonuses
}
}