use crate::{analysis, complexity, deadcode};
use serde_json;
use std::path::Path;
pub fn handle_analyze_command(
path: &Path,
extensions: Option<&[String]>,
exclude: Option<&[String]>,
with_metrics: bool,
format: &str,
) -> Result<(), Box<dyn std::error::Error>> {
analysis::analyze_codebase(path, extensions, exclude)?;
if with_metrics {
use crate::codemetrics::{analyze_project_metrics, print_metrics_report};
let project_metrics = analyze_project_metrics(path, extensions, exclude)?;
if format == "json" {
println!("{}", serde_json::to_string_pretty(&project_metrics)?);
} else {
print_metrics_report(&project_metrics, false);
}
}
Ok(())
}
pub fn handle_complexity_command(
path: &Path,
extensions: Option<&[String]>,
exclude: Option<&[String]>,
threshold: Option<u32>,
sort: bool,
) -> Result<(), Box<dyn std::error::Error>> {
complexity::analyze_complexity(path, extensions, exclude, threshold, sort)?;
Ok(())
}
pub fn handle_deadcode_command(
path: &Path,
extensions: Option<&[String]>,
exclude: Option<&[String]>,
) -> Result<(), Box<dyn std::error::Error>> {
deadcode::detect_dead_code(path, extensions, exclude)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::tempdir;
#[test]
fn test_handle_analyze_command() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("test.rs"), "fn test() {}").unwrap();
let result = handle_analyze_command(dir.path(), None, None, false, "text");
assert!(result.is_ok());
}
#[test]
fn test_handle_complexity_command() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("test.rs"), "fn test() { if true { } }").unwrap();
let result = handle_complexity_command(dir.path(), None, None, Some(1), false);
assert!(result.is_ok());
}
#[test]
fn test_handle_deadcode_command() {
let dir = tempdir().unwrap();
fs::write(dir.path().join("test.rs"), "fn test() { let x = 1; }").unwrap();
let result = handle_deadcode_command(dir.path(), None, None);
assert!(result.is_ok());
}
}