use crate::cli::Cli;
use fluent_i18n::t;
use std::io::{self, Write};
use std::process::{Command, Output};
#[derive(Eq, Hash, PartialEq, Debug)]
pub struct Key {
pub typo: String,
pub corrections: Vec<String>,
}
fn output_detected_errors(output: &Output, message: &str) {
eprintln!("{message}");
if let Err(e) = io::stderr().write_all(&output.stderr) {
eprintln!("{}", t!("keyvalue-error-stderr", {"e" => e.to_string()}));
}
if let Err(e) = io::stdout().write_all(&output.stdout) {
eprintln!("{}", t!("keyvalue-error-stdout", {"e" => e.to_string()}));
}
}
impl Key {
#[must_use]
pub fn is_typo_correctable(&self, cli: &Cli) -> bool {
match &cli.typo {
Some(typos) => typos.contains(&self.typo) && self.corrections.len() == 1 && self.typo.len() >= cli.minlen,
None => self.corrections.len() == 1 && self.typo.len() >= cli.minlen,
}
}
pub fn run_sed(&self, files: &Vec<String>, cli: &Cli) {
if !self.is_typo_correctable(cli) {
return;
}
let sed_script = format!("s/\\b{}\\b/{}/g", self.typo, self.corrections[0]);
if cli.noop {
println!("sed --in-place --expression={sed_script} {files:?}");
} else {
let output = Command::new("sed")
.arg("--in-place")
.arg(format!("--expression={sed_script}"))
.args(files)
.output()
.unwrap_or_else(|_| panic!("{}", t!("keyvalue-process-sed")));
if !output.status.success() {
output_detected_errors(&output, t!("keyvalue-error-sed", { "typo" => self.typo, "correction" => format!("{:?}", self.corrections[0])}).as_str());
}
}
}
fn format_git_message(&self, cli: &Cli) -> String {
if let Some(correction) = self.corrections.first() {
cli.message.replace("{typo}", &self.typo).replace("{correction}", correction)
} else {
cli.message.replace("{typo}", &self.typo)
}
}
pub fn run_git_commit(&self, cli: &Cli) {
if !self.is_typo_correctable(cli) {
return;
}
let git_message = self.format_git_message(cli);
if cli.noop {
println!("git commit --all --message={git_message}");
} else {
let output = Command::new("git")
.arg("commit")
.arg("--all")
.arg(format!("--message={git_message}"))
.output()
.expect("failed to execute git process");
if !output.status.success() {
output_detected_errors(&output, t!("keyvalue-error-git", { "typo" => self.typo, "correction" => format!("{:?}", self.corrections[0])}).as_str());
}
}
}
}
#[derive(Eq, Hash, PartialEq, Debug)]
pub struct Value {
pub path: String,
pub line_num: u32,
pub byte_offset: u32,
}
impl Value {
pub fn print_value_details(&self) {
println!(
"\t{}",
t!("thashmap-typo-details", {"path" => self.path, "line" => self.line_num, "offset" => self.byte_offset})
);
}
}