Repository: HigherOrderCO/Bend
File: src/main.rs (Rust)

Code before:

fn main() {
  fn run() -> Result<(), String> {
    #[cfg(not(feature = "cli"))]
    compile_error!("The 'cli' feature is needed for the hvm-lang cli");

    let cli = Cli::parse();
    let opts = Opts::light();
    let arg_verbose = cli.verbose;

    let verbose = |book: &_| {
      if arg_verbose {
        println!("{book:?}");
      }
    };

    run_mode(cli, &verbose, opts)?;

    Ok(())
  }
  if let Err(e) = run() {
    eprintln!("{e}");
  }
}

fn run_mode(cli: Cli, verbose: &dyn Fn(&hvml::term::Book), mut opts: Opts) -> Result<(), String> {
  Ok(match cli.mode {
    Mode::Check { path } => {
      let book = load_file_to_book(&path)?;
      verbose(&book);
      check_book(book)?;
    }
    Mode::Compile { path, cli_opts, wopts } => {
      let warning_opts = wopts.get_warning_opts();
      Opts::from_cli_opts(&mut opts, cli_opts)?;
      let mut book = load_file_to_book(&path)?;
      verbose(&book);
      let compiled = compile_book(&mut book, opts)?;
      hvml::display_warnings(warning_opts, &compiled.warnings)?;
      print!("{}", show_book(&compiled.core_book));
    }
    Mode::Desugar { path } => {
      let mut book = load_file_to_book(&path)?;
      verbose(&book);
      desugar_book(&mut book, opts)?;
      println!("{book}");
    }
    Mode::Run { path, mem, debug, single_core, linear, arg_stats, cli_opts, wopts } => {
      let warning_opts = wopts.get_warning_opts();
      Opts::from_cli_opts(&mut opts, cli_opts)?;
      opts.check();

Review comment (suggestion):
This name is a bit strange because we have a mode called "run".
Maybe something like `execute_cli_mode` to avoid the confusion.

Code after:

fn main() {
  fn run() -> Result<(), String> {
    #[cfg(not(feature = "cli"))]
    compile_error!("The 'cli' feature is needed for the hvm-lang cli");

    let cli = Cli::parse();
    let arg_verbose = cli.verbose;

    let verbose = |book: &_| {
      if arg_verbose {
        println!("{book:?}");
      }
    };

    execute_cli_mode(cli, &verbose)?;

    Ok(())
  }
  if let Err(e) = run() {
    eprintln!("{e}");
  }
}

fn execute_cli_mode(cli: Cli, verbose: &dyn Fn(&hvml::term::Book)) -> Result<(), String> {
  Ok(match cli.mode {
    Mode::Check { path } => {
      let book = load_file_to_book(&path)?;
      verbose(&book);
      check_book(book)?;
    }
    Mode::Compile { path, cli_opts, wopts } => {
      let warning_opts = wopts.get_warning_opts();
      let opts = OptArgs::opts_from_cli(&cli_opts);
      let mut book = load_file_to_book(&path)?;
      verbose(&book);
      let compiled = compile_book(&mut book, opts)?;
      hvml::display_warnings(warning_opts, &compiled.warnings)?;
      print!("{}", show_book(&compiled.core_book));
    }
    Mode::Desugar { path } => {
      let mut book = load_file_to_book(&path)?;
      verbose(&book);
      desugar_book(&mut book, Opts::light())?;
      println!("{book}");
    }
    Mode::Run { path, mem, debug, single_core, linear, arg_stats, cli_opts, wopts } => {
      let warning_opts = wopts.get_warning_opts();
      let opts = OptArgs::opts_from_cli(&cli_opts);
      opts.check();
      let book = load_file_to_book(&path)?;