use crate::core::refined::{DebtDensity, RiskScore, WeightFactor};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GodObjectThresholds {
#[serde(default = "default_max_methods")]
pub max_methods: usize,
#[serde(default = "default_max_fields")]
pub max_fields: usize,
#[serde(default = "default_max_traits")]
pub max_traits: usize,
#[serde(default = "default_max_lines")]
pub max_lines: usize,
#[serde(default = "default_max_complexity")]
pub max_complexity: u32,
}
impl Default for GodObjectThresholds {
fn default() -> Self {
Self {
max_methods: default_max_methods(),
max_fields: default_max_fields(),
max_traits: default_max_traits(),
max_lines: default_max_lines(),
max_complexity: default_max_complexity(),
}
}
}
impl GodObjectThresholds {
pub fn rust_defaults() -> Self {
Self {
max_methods: 20,
max_fields: 15,
max_traits: 5,
max_lines: 1000,
max_complexity: 200,
}
}
pub fn python_defaults() -> Self {
Self {
max_methods: 15,
max_fields: 10,
max_traits: 3,
max_lines: 500,
max_complexity: 150,
}
}
pub fn javascript_defaults() -> Self {
Self {
max_methods: 15,
max_fields: 20,
max_traits: 3,
max_lines: 500,
max_complexity: 150,
}
}
}
fn default_max_methods() -> usize {
20
}
fn default_max_fields() -> usize {
15
}
fn default_max_traits() -> usize {
5
}
fn default_max_lines() -> usize {
1000
}
fn default_max_complexity() -> u32 {
200
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ThresholdsConfig {
pub complexity: Option<u32>,
pub duplication: Option<u32>,
pub max_file_length: Option<usize>,
pub max_function_length: Option<usize>,
#[serde(default)]
pub minimum_debt_score: Option<f64>,
#[serde(default)]
pub minimum_cyclomatic_complexity: Option<u32>,
#[serde(default)]
pub minimum_cognitive_complexity: Option<u32>,
#[serde(default)]
pub minimum_risk_score: Option<f64>,
#[serde(default)]
pub min_score_threshold: Option<f64>,
#[serde(default)]
pub validation: Option<ValidationThresholds>,
#[serde(default)]
pub file_size: Option<FileSizeThresholds>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidationThresholds {
#[serde(default = "default_max_avg_complexity")]
pub max_average_complexity: f64,
#[serde(default = "default_max_debt_density")]
pub max_debt_density: f64,
#[serde(default = "default_max_codebase_risk")]
pub max_codebase_risk_score: f64,
#[serde(default = "default_min_coverage")]
pub min_coverage_percentage: f64,
#[serde(default = "default_max_total_debt_score_high")]
pub max_total_debt_score: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[deprecated(since = "0.3.0", note = "Use max_debt_density instead")]
pub max_high_complexity_count: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[deprecated(since = "0.3.0", note = "Use max_debt_density instead")]
pub max_debt_items: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[deprecated(
since = "0.3.0",
note = "Use max_debt_density and max_codebase_risk_score instead"
)]
pub max_high_risk_functions: Option<usize>,
}
impl Default for ValidationThresholds {
#[allow(deprecated)]
fn default() -> Self {
Self {
max_average_complexity: default_max_avg_complexity(),
max_debt_density: default_max_debt_density(),
max_codebase_risk_score: default_max_codebase_risk(),
min_coverage_percentage: default_min_coverage(),
max_total_debt_score: default_max_total_debt_score_high(),
max_high_complexity_count: None,
max_debt_items: None,
max_high_risk_functions: None,
}
}
}
impl ValidationThresholds {
pub fn debt_density_refined(&self) -> Option<DebtDensity> {
DebtDensity::new(self.max_debt_density).ok()
}
pub fn codebase_risk_refined(&self) -> Option<RiskScore> {
let normalized = self.max_codebase_risk_score / 10.0;
RiskScore::new(normalized).ok()
}
pub fn min_coverage_refined(&self) -> Option<WeightFactor> {
let normalized = self.min_coverage_percentage / 100.0;
WeightFactor::new(normalized).ok()
}
pub fn validate_refined(&self) -> Vec<String> {
let mut errors = Vec::new();
if self.debt_density_refined().is_none() {
errors.push(format!(
"max_debt_density {} is invalid (must be >= 0)",
self.max_debt_density
));
}
if self.max_codebase_risk_score < 0.0 || self.max_codebase_risk_score > 10.0 {
errors.push(format!(
"max_codebase_risk_score {} is invalid (must be 0-10)",
self.max_codebase_risk_score
));
}
if self.min_coverage_percentage < 0.0 || self.min_coverage_percentage > 100.0 {
errors.push(format!(
"min_coverage_percentage {} is invalid (must be 0-100)",
self.min_coverage_percentage
));
}
errors
}
}
fn default_max_avg_complexity() -> f64 {
10.0
}
fn default_max_codebase_risk() -> f64 {
7.0
}
fn default_min_coverage() -> f64 {
0.0
}
fn default_max_debt_density() -> f64 {
50.0 }
fn default_max_total_debt_score_high() -> u32 {
10000 }
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileSizeThresholds {
#[serde(default = "default_business_logic_threshold")]
pub business_logic: usize,
#[serde(default = "default_test_code_threshold")]
pub test_code: usize,
#[serde(default = "default_declarative_config_threshold")]
pub declarative_config: usize,
#[serde(default = "default_generated_code_threshold")]
pub generated_code: usize,
#[serde(default = "default_proc_macro_threshold")]
pub proc_macro: usize,
#[serde(default = "default_build_script_threshold")]
pub build_script: usize,
#[serde(default = "default_min_lines_per_function")]
pub min_lines_per_function: f32,
#[serde(default)]
pub overrides: std::collections::HashMap<String, usize>,
}
impl Default for FileSizeThresholds {
fn default() -> Self {
Self {
business_logic: default_business_logic_threshold(),
test_code: default_test_code_threshold(),
declarative_config: default_declarative_config_threshold(),
generated_code: default_generated_code_threshold(),
proc_macro: default_proc_macro_threshold(),
build_script: default_build_script_threshold(),
min_lines_per_function: default_min_lines_per_function(),
overrides: std::collections::HashMap::new(),
}
}
}
fn default_business_logic_threshold() -> usize {
400
}
fn default_test_code_threshold() -> usize {
650
}
fn default_declarative_config_threshold() -> usize {
1200
}
fn default_generated_code_threshold() -> usize {
5000
}
fn default_proc_macro_threshold() -> usize {
500
}
fn default_build_script_threshold() -> usize {
300
}
fn default_min_lines_per_function() -> f32 {
3.0
}