Files
text-correction/src/main.rs
2025-07-16 03:08:16 +02:00

95 lines
2.9 KiB
Rust

use clap::{Args, Parser, Subcommand};
use text_correction::utils;
use std::time::SystemTime;
use std::process::*;
use std::path::Path;
use spinners::{Spinner, Spinners};
use log::{error, trace};
#[derive(Parser)]
#[command(name = "word corrector")]
#[command(version, about, long_about = None)]
#[command(next_line_help = true)]
struct Cli {
#[command(subcommand)]
command: Commands
}
#[derive(Subcommand)]
enum Commands {
CorrectWord(WordArgs),
CorrectFile(FileArgs),
BenchFile(FileArgs)
}
#[derive(Args)]
struct WordArgs {
#[arg(short,long)]
input: String,
#[arg(short,long)]
list_path: String
}
#[derive(Args)]
struct FileArgs {
#[arg(short,long)]
input: String,
#[arg(short,long)]
list_path: String,
#[arg(short,long)]
output: String,
#[arg(short,long)]
overwrite: bool
}
fn main() {
let cli: Cli = Cli::parse();
match &cli.command {
Commands::CorrectWord(args) => {
let out: String = utils::correct(args.input.clone(), args.list_path.as_str());
println!("{}", out);
},
Commands::CorrectFile(args) => {
if args.overwrite == true {
trace!("Overwriting old file due to cli argument.");
std::fs::remove_file(args.output.clone()).unwrap();
} else {
trace!("Checking whether destination is writable.");
let path = "./".to_owned() + args.output.as_str();
if Path::new(path.as_str()).exists() {
error!("File already exists!");
exit(1);
}
}
let mut sp = Spinner::new(Spinners::Dots, "Processing file...".into());
utils::correct_file_concurr(args.input.clone(), args.list_path.as_str(), args.output.clone());
sp.stop_with_message("".into());
},
Commands::BenchFile(args) => {
let start_par = SystemTime::now();
utils::correct_file_concurr(args.input.clone(), args.list_path.as_str(), args.output.clone());
let stop_par = match start_par.elapsed() {
Ok(elapsed) => elapsed.as_millis(),
Err(e) => {
println!("Error: {e:?}");
exit(1);
}
};
println!("Parallel processing took: {stop_par:?} ms");
std::fs::remove_file(args.output.clone()).unwrap();
let start = SystemTime::now();
utils::correct_file(args.input.clone(), args.list_path.as_str(), args.output.clone());
let stop = match start.elapsed() {
Ok(elapsed) => elapsed.as_millis(),
Err(e) => {
println!("Error: {e:?}");
exit(1);
}
};
println!("Single-thread processing took: {stop:?} ms");
}
}
}