thag_rs 0.1.8

A versatile cross-platform script runner and REPL for Rust snippets, expressions and programs. Accepts a script file or dynamic options.
#![allow(clippy::uninlined_format_args)]

#[cfg(debug_assertions)]
use thag_rs::debug_timings;
use thag_rs::logging::{configure_log, set_verbosity};
use thag_rs::{execute, get_args, ThagResult};

use std::cell::RefCell;
#[cfg(debug_assertions)]
use std::time::Instant;

pub fn main() -> ThagResult<()> {
    #[cfg(debug_assertions)]
    let start = Instant::now();
    let cli = RefCell::new(get_args()); // Wrap args in a RefCell

    set_verbosity(&cli.borrow())?;

    configure_log();
    #[cfg(debug_assertions)]
    debug_timings(&start, "Configured logging");

    // Check if firestorm profiling is enabled
    if firestorm::enabled() {
        // Profile the `execute` function
        firestorm::bench("./flames/", || {
            handle(&cli);
        })?;
    } else {
        // Regular execution when profiling is not enabled
        handle(&cli);
    }
    Ok(())
}

fn handle(cli: &RefCell<thag_rs::Cli>) {
    // Use borrow_mut to get a mutable reference
    let result = execute(&mut cli.borrow_mut());
    match result {
        Ok(()) => (),
        Err(e) => println!("{e}"),
    }
}