typos-git-commit 0.8.2

This program analyzes a json file produced with `typos` and makes commits for each correction.
Documentation
use crate::cli::Cli;
use fluent_i18n::t;
use std::io::{self, Write};
use std::process::{Command, Output};

// Convenient Key, Value structures to be used with a HashMap.
// Key is identified by the typo and its suggested corrections
#[derive(Eq, Hash, PartialEq, Debug)]
pub struct Key {
    pub typo: String,
    pub corrections: Vec<String>,
}

// Helper function to print the output of the command that
// has been executed and that returned an error status.
// Prints an another message if an error occurred when printing
// to stderr or stdout.
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 {
    /// Returns true if the Key contains a typo that may be corrected
    #[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,
        }
    }

    /// Runs sed command. This is way too basic and may lead to replacement
    /// mistakes.
    ///
    /// # Panics
    ///
    /// Panics if the sed subprocess fails
    pub fn run_sed(&self, files: &Vec<String>, cli: &Cli) {
        if !self.is_typo_correctable(cli) {
            return;
        }

        // Here we know that we have exactly one correction
        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());
            }
        }
    }

    /// Replaces {typo} and {correction} by their respective values
    /// self.corrections is expected to have one (and exactly one) value
    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)
        }
    }

    /// Runs git commit command.
    ///
    /// # Panics
    ///
    /// Panics if the git subprocess fails
    // @TODO: check whether the sed command did modify something before
    //        trying to commit anything
    // @TODO: avoid spawning external process
    pub fn run_git_commit(&self, cli: &Cli) {
        if !self.is_typo_correctable(cli) {
            return;
        }

        // Here we know that we have exactly one correction (because it
        // is a correctable typo)
        let git_message = self.format_git_message(cli);

        if cli.noop {
            println!("git commit --all --message={git_message}");
        } else {
            // @todo: detect if a correction has been done or not as we do
            // only correct whole words. This is to avoid a blank commit and
            // get an error. When detected print a message about the typo that
            // was not corrected
            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());
            }
        }
    }
}

// Value contains information about where the typo is located
#[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})
        );
    }
}