#![cfg_attr(coverage_nightly, coverage(off))]
use super::analysis::{run_complexity_analysis, run_satd_analysis, run_tdg_analysis};
use super::types::{
EnforcementProgress, EnforcementResult, EnforcementState, QualityProfile, QualityViolation,
};
use anyhow::Result;
use std::path::{Path, PathBuf};
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub async fn handle_analyzing_state(
project_path: &Path,
profile: &QualityProfile,
single_file_mode: bool,
_dry_run: bool,
specific_file: Option<&PathBuf>,
) -> Result<EnforcementResult> {
let mut violations = Vec::new();
let complexity_violations = run_complexity_analysis(project_path, profile).await?;
let satd_violations = run_satd_analysis(project_path, profile).await?;
let tdg_violations = run_tdg_analysis(project_path, profile).await?;
violations.extend(complexity_violations);
violations.extend(satd_violations);
violations.extend(tdg_violations);
let complexity_score = 0.8; let satd_score = if profile.satd_allowed == 0 { 1.0 } else { 0.5 };
let tdg_score = 0.7; let coverage_score = 0.65; let total_score = (complexity_score + satd_score + tdg_score + coverage_score) / 4.0;
let next_state = if violations.is_empty() {
EnforcementState::Complete
} else {
EnforcementState::Violating
};
Ok(EnforcementResult {
state: next_state,
score: total_score,
target: 1.0,
current_file: specific_file.map(|p| p.display().to_string()),
violations,
next_action: if next_state == EnforcementState::Complete {
"none".to_string()
} else {
"review_violations".to_string()
},
progress: EnforcementProgress {
files_completed: 0,
files_remaining: if single_file_mode { 1 } else { 100 },
estimated_iterations: ((1.0 - total_score) * 10.0) as u32,
},
})
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub fn handle_violating_state(
violations: Vec<QualityViolation>,
total_score: f64,
apply_suggestions: bool,
dry_run: bool,
specific_file: Option<&PathBuf>,
) -> Result<EnforcementResult> {
if apply_suggestions && !dry_run {
Ok(EnforcementResult {
state: EnforcementState::Refactoring,
score: total_score,
target: 1.0,
current_file: specific_file.map(|p| p.display().to_string()),
violations: violations.clone(),
next_action: "apply_refactoring".to_string(),
progress: EnforcementProgress {
files_completed: 0,
files_remaining: violations.len(),
estimated_iterations: violations.len() as u32,
},
})
} else {
Ok(EnforcementResult {
state: EnforcementState::Violating,
score: total_score,
target: 1.0,
current_file: specific_file.map(|p| p.display().to_string()),
violations,
next_action: "manual_intervention_required".to_string(),
progress: EnforcementProgress {
files_completed: 0,
files_remaining: 0,
estimated_iterations: 0,
},
})
}
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub fn handle_refactoring_state(
total_score: f64,
specific_file: Option<&PathBuf>,
) -> Result<EnforcementResult> {
eprintln!("🔧 Applying automated refactoring...");
Ok(EnforcementResult {
state: EnforcementState::Validating,
score: total_score + 0.1, target: 1.0,
current_file: specific_file.map(|p| p.display().to_string()),
violations: vec![], next_action: "validate_changes".to_string(),
progress: EnforcementProgress {
files_completed: 1,
files_remaining: 0,
estimated_iterations: 1,
},
})
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn handle_complete_state() -> Result<EnforcementResult> {
Ok(EnforcementResult {
state: EnforcementState::Complete,
score: 1.0,
target: 1.0,
current_file: None,
violations: vec![],
next_action: "none".to_string(),
progress: EnforcementProgress {
files_completed: 100, files_remaining: 0,
estimated_iterations: 0,
},
})
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub async fn handle_violating_enforcement_state_proxy(
project_path: &PathBuf,
profile: &QualityProfile,
single_file_mode: bool,
dry_run: bool,
specific_file: Option<&PathBuf>,
apply_suggestions: bool,
) -> Result<EnforcementResult> {
let analyzing_result = handle_analyzing_state(
project_path,
profile,
single_file_mode,
dry_run,
specific_file,
)
.await?;
handle_violating_state(
analyzing_result.violations,
analyzing_result.score,
apply_suggestions,
dry_run,
specific_file,
)
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub fn handle_refactoring_enforcement_state(
base_score: f64,
specific_file: Option<&PathBuf>,
) -> Result<EnforcementResult> {
handle_refactoring_state(base_score, specific_file)
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub async fn handle_validating_enforcement_state(
project_path: &PathBuf,
profile: &QualityProfile,
single_file_mode: bool,
dry_run: bool,
specific_file: Option<&PathBuf>,
_include_pattern: Option<&String>,
_exclude_pattern: Option<&String>,
) -> Result<EnforcementResult> {
let mut result = handle_analyzing_state(
project_path,
profile,
single_file_mode,
dry_run,
specific_file,
)
.await?;
if result.violations.is_empty() {
result.state = EnforcementState::Complete;
} else {
result.state = EnforcementState::Violating;
}
Ok(result)
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
pub async fn handle_analyzing_enforcement_state(
project_path: &PathBuf,
profile: &QualityProfile,
single_file_mode: bool,
dry_run: bool,
specific_file: Option<&PathBuf>,
) -> Result<EnforcementResult> {
handle_analyzing_state(
project_path,
profile,
single_file_mode,
dry_run,
specific_file,
)
.await
}
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn handle_complete_enforcement_state() -> Result<EnforcementResult> {
handle_complete_state()
}