use crate::adapters::analyzers::iosp::FunctionAnalysis;
use crate::cli::Cli;
use crate::report;
use clap::CommandFactory;
use std::path::Path;
pub(crate) fn handle_init(content: &str) -> Result<(), i32> {
let path = Path::new("rustqual.toml");
if path.exists() {
eprintln!("Error: rustqual.toml already exists in the current directory.");
return Err(1);
}
match std::fs::write(path, content) {
Ok(()) => {
eprintln!("Created rustqual.toml with tailored configuration.");
Ok(())
}
Err(e) => {
eprintln!("Error writing rustqual.toml: {e}");
Err(1)
}
}
}
pub(crate) fn handle_completions(shell: clap_complete::Shell) {
clap_complete::generate(
shell,
&mut Cli::command(),
"rustqual",
&mut std::io::stdout(),
);
}
pub(crate) fn handle_save_baseline(
path: &Path,
all_results: &[FunctionAnalysis],
summary: &report::Summary,
) -> Result<(), i32> {
let baseline = report::create_baseline(all_results, summary);
match std::fs::write(path, baseline) {
Ok(()) => {
eprintln!("Baseline saved to {}", path.display());
Ok(())
}
Err(e) => {
eprintln!("Error saving baseline: {e}");
Err(1)
}
}
}
pub(crate) fn handle_compare(
path: &Path,
all_results: &[FunctionAnalysis],
summary: &report::Summary,
) -> Result<bool, i32> {
let baseline_content = std::fs::read_to_string(path).map_err(|e| {
eprintln!("Error reading baseline: {e}");
1
})?;
Ok(report::print_comparison(
&baseline_content,
all_results,
summary,
))
}