#![cfg_attr(coverage_nightly, coverage(off))]
use super::display::format_explain_output;
use super::formatting::write_tdg_output;
use super::quality_gates::execute_tdg_analysis;
use super::TdgCommandConfig;
use crate::tdg::TdgAnalyzer;
use anyhow::Result;
pub(super) async fn handle_explain_mode(
analyzer: &TdgAnalyzer,
config: &TdgCommandConfig,
) -> Result<()> {
use crate::tdg::explain::ExplainedTDGScore;
let score = execute_tdg_analysis(analyzer, config).await?;
let mut explained = ExplainedTDGScore::new(score.clone());
#[cfg(feature = "rust-ast")]
if config.path.is_file() && config.path.extension().is_some_and(|e| e == "rs") {
use crate::tdg::function_analyzer::FunctionAnalyzer;
use crate::tdg::recommendation_engine::generate_recommendations;
let mut func_analyzer = FunctionAnalyzer::new()?;
let functions = func_analyzer.analyze_file(&config.path)?;
for func in functions {
explained.add_function(func);
}
explained.filter_functions_by_threshold(config.threshold);
explained.sort_functions_by_impact();
let recommendations = generate_recommendations(&explained);
for rec in recommendations {
explained.add_recommendation(rec);
}
explained.sort_recommendations();
}
let output_str = format_explain_output(&explained, config)?;
write_tdg_output(&output_str, config)?;
Ok(())
}