cargo-uwu 0.2.1

Is supposed to uwuify cargo output
Documentation
use std::process::Command;

fn parse_command_output(stream: Vec<u8>) -> Result<String, String> {
    match String::from_utf8(stream) {
        Ok(str) => match uwu_rs::uwuify(str) {
            Ok(uwu_text) => Ok(uwu_text),
            Err(err) => Err(err.to_string()),
        },
        Err(err) => Err(err.to_string()),
    }
}

const STDERR_BEGIN: &str =
    "Onwy keep :3 weading ^-^ if *boops your nose* you messed ;;w;; up >w<:\n";
fn run_command(args: impl Iterator<Item = String>) -> Result<String, String> {
    let mut command = Command::new("cargo");
    let command_chain = command.arg("--color=always");
    for arg in args {
        command_chain.arg(arg);
    }
    match command.output() {
        Ok(streams) => {
            // not an uwu solution
            let err_stream = if streams.stderr.len() > 0 {
                format!("{STDERR_BEGIN}{}", parse_command_output(streams.stderr)?)
            } else {
                String::new()
            };
            Ok(format!(
                "{}\n{}",
                parse_command_output(streams.stdout)?,
                err_stream
            ))
        }
        Err(err) => Err(format!("F-failed to execute?! ({err})")),
    }
}

const DRY_RUN_ERR_MSG: &str = "Dry Run? *confused >.<*";
const _EMPTY_ERR_MSG: &str = "UwU, did you just want to see me? :3"; // no way of checking this;
                                                                     // actually it just prints the help message in that case, so we're fine
fn parse_commands(mut args: impl Iterator<Item = String>) -> Result<String, String> {
    args.next();
    if let None = args.next() {
        return Err(DRY_RUN_ERR_MSG.to_string());
    }

    Ok(run_command(args)?)
}

pub fn init(args: impl Iterator<Item = String>) {
    match parse_commands(args) {
        Ok(res) => print!("{res}"),
        Err(err) => eprint!("{err}"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ok() {
        let vec = vec![
            "pwogwam".to_string(),
            "uwu".to_string(),
            "build".to_string(),
        ];
        parse_commands(vec.into_iter()).unwrap(); // needs more testing, but whatever
    }

    #[test]
    fn test_dry_run() {
        let vec = vec!["pwogwam".to_string()];
        let res = parse_commands(vec.into_iter());
        assert_eq!(res, Err(DRY_RUN_ERR_MSG.to_string()));
    }

    // #[test]
    // fn invalid_unicode() {}
}