thag_rs 0.2.1

A versatile cross-platform playground and REPL for Rust snippets, expressions and programs. Accepts a script file or dynamic options.
Documentation
/// Demo of invoking the Rust formatter programmatically, with the addition of an `rfd`
/// (`Rusty File Dialogs`) cross-platform file chooser to select the file to format.
/// Compare with `demo/file_dialog_thag.rs`, which uses `thag_proc_macros` text-based
/// file navigator and `inquire` to choose the file.
//# Purpose: Demo file chooser and calling an external program, in this case the Rust formatter.
//# Categories: crates, technique
use rfd::FileDialog;
use std::error::Error;
use std::process::Command;

fn main() -> Result<(), Box<dyn Error>> {
    // Check if rustfmt is available
    if Command::new("rustfmt").arg("--version").output().is_ok() {
        let source_file = FileDialog::new()
            .set_title("Pick a .rs file to format")
            .add_filter("rust", &["rs"])
            .set_directory(".")
            .pick_file()
            .ok_or("No file selected")?;

        println!("Selected file: {}\n", source_file.display());

        // Run rustfmt on the source file
        let mut command = Command::new("rustfmt");
        command.arg("--edition");
        command.arg("2021");
        command.arg(&source_file);
        let output = command.output().expect("Failed to run rustfmt");

        if output.status.success() {
            println!("Successfully formatted {source_file:#?} with rustfmt.");
        } else {
            eprintln!(
                "Failed to format {source_file:#?} with rustfmt:\n{}",
                String::from_utf8_lossy(&output.stderr)
            );
        }
    } else {
        eprintln!("`rustfmt` not found. Please install it to use this script.");
    }
    Ok(())
}