mod cli;
use clap::{Parser, Subcommand};
use std::time::Instant;
#[derive(Parser)]
#[command(name = "anyrepair")]
#[command(about = "A tool for repairing LLM responses including JSON, YAML, and Markdown")]
#[command(version)]
struct Cli {
#[arg(short, long)]
verbose: bool,
#[arg(short, long)]
quiet: bool,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
Repair {
#[arg(value_name = "FILE")]
file: Option<String>,
#[arg(short, long)]
input: Option<String>,
#[arg(short, long)]
output: Option<String>,
#[arg(long)]
confidence: bool,
#[arg(short, long)]
format: Option<String>,
},
Validate {
#[arg(short, long)]
input: Option<String>,
#[arg(short, long)]
format: Option<String>,
},
Batch {
#[arg(short, long)]
input: String,
#[arg(short, long)]
output: String,
#[arg(short, long)]
pattern: Option<String>,
#[arg(short, long)]
recursive: bool,
},
Stream {
#[arg(short, long)]
input: Option<String>,
#[arg(short, long)]
output: Option<String>,
#[arg(short, long)]
format: Option<String>,
#[arg(long)]
buffer_size: Option<usize>,
},
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let cli = Cli::parse();
let start_time = Instant::now();
match cli.command {
Commands::Repair { file, input, output, confidence, format } => {
let input_path = file.as_deref().or(input.as_deref());
cli::repair_cmd::handle_repair(input_path, output.as_deref(), confidence, cli.verbose, format.as_deref())?;
}
Commands::Validate { input, format } => {
cli::validate_cmd::handle_validate(input.as_deref(), format.as_deref(), cli.verbose)?;
}
Commands::Batch { input, output, pattern, recursive } => {
cli::batch_cmd::handle_batch(&input, &output, pattern.as_deref(), recursive, cli.verbose)?;
}
Commands::Stream { input, output, format, buffer_size } => {
let fmt = format.as_deref().unwrap_or("auto");
cli::stream_cmd::handle_stream(input.as_deref(), output.as_deref(), fmt, buffer_size, cli.verbose)?;
}
}
if cli.verbose && !cli.quiet {
let duration = start_time.elapsed();
eprintln!("Completed in {:?}", duration);
}
Ok(())
}