From aa50d3e5af7e4439cb397530c71accb0f12195a1 Mon Sep 17 00:00:00 2001 From: Compey Date: Mon, 14 Aug 2023 22:59:29 +0530 Subject: [PATCH] refactor: simplify history file path err handling --- src/cli/repl.rs | 79 ++++++++++++++----------------------------------- src/lib.rs | 2 +- 2 files changed, 24 insertions(+), 57 deletions(-) diff --git a/src/cli/repl.rs b/src/cli/repl.rs index 9134c63..2ef6e7a 100644 --- a/src/cli/repl.rs +++ b/src/cli/repl.rs @@ -1,22 +1,19 @@ use std::{ fmt::Write, - io::ErrorKind, - path::PathBuf, + path::{Path, PathBuf}, process::{exit, ExitCode}, }; -use anyhow::Result; +use anyhow::{Error, Result}; use clap::Command; use directories::UserDirs; -use lune::lua::stdio::formatting::pretty_format_luau_error; use lune::Lune; -use mlua::ExternalError; -use rustyline::{error::ReadlineError, history::FileHistory, DefaultEditor, Editor}; +use rustyline::{error::ReadlineError, DefaultEditor}; #[derive(PartialEq)] enum PromptState { Regular, - Continuation + Continuation, } // Isn't dependency injection plain awesome?! @@ -29,40 +26,23 @@ pub async fn show_interface(cmd: Command) -> Result { let lune_instance = Lune::new(); let mut repl = DefaultEditor::new()?; + let history_file_path: &PathBuf = &UserDirs::new() + .ok_or(Error::msg("cannot find user home directory"))? + .home_dir() + .join(".lune_history"); - match repl.load_history(&(|| -> PathBuf { - let dir_opt = UserDirs::new(); + if !history_file_path.exists() { + std::fs::write( + // We know for sure that the home dir already exists + directories::UserDirs::new() + .unwrap() + .home_dir() + .join(".lune_history"), + String::new(), + )?; + } - if let Some(dirs) = dir_opt { - let home_dir = dirs.home_dir(); - - home_dir.join(".lune_history") - } else { - eprintln!("Failed to find user home directory, abort!"); - // Doesn't feel right to exit directly with a exit code of 1 - // Lmk if there is a better way of doing this - exit(1); - } - })()) { - Ok(_) => (), - Err(err) => { - if let ReadlineError::Io(io_err) = err { - // If global history file does not exist, we create it - if io_err.kind() == ErrorKind::NotFound { - std::fs::write( - // We know for sure that the home dir already exists - directories::UserDirs::new() - .unwrap() - .home_dir() - .join(".lune_history"), - String::new(), - )?; - } - } - - eprintln!("WARN: Failed to load REPL history") - } - }; + repl.load_history(history_file_path)?; let mut interrupt_counter = 0u32; let mut prompt_state: PromptState = PromptState::Regular; @@ -71,7 +51,7 @@ pub async fn show_interface(cmd: Command) -> Result { loop { let prompt = match prompt_state { PromptState::Regular => "> ", - PromptState::Continuation => ">> " + PromptState::Continuation => ">> ", }; match repl.readline(prompt) { @@ -98,12 +78,12 @@ pub async fn show_interface(cmd: Command) -> Result { // Increment the counter interrupt_counter += 1; } else { - save_repl_activity(repl)?; + repl.save_history(history_file_path)?; break; } } Err(ReadlineError::Eof) => { - save_repl_activity(repl)?; + repl.save_history(history_file_path)?; break; } Err(err) => { @@ -124,7 +104,7 @@ pub async fn show_interface(cmd: Command) -> Result { prompt_state = PromptState::Continuation; source_code.push_str("\n") } else { - eprintln!("{}", pretty_format_luau_error(&err.into_lua_err(), true)) + eprintln!("{}", err); } } }; @@ -132,16 +112,3 @@ pub async fn show_interface(cmd: Command) -> Result { Ok(ExitCode::SUCCESS) } - -fn save_repl_activity(mut repl: Editor<(), FileHistory>) -> Result<()> { - // Once again, we know that the specified home directory - // and history file already exist - repl.save_history( - &directories::UserDirs::new() - .unwrap() - .home_dir() - .join(".lune_history"), - )?; - - Ok(()) -} diff --git a/src/lib.rs b/src/lib.rs index 55ce4f6..b798775 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,4 +6,4 @@ pub mod roblox; #[cfg(test)] mod tests; -pub use crate::lune::{lua, Lune, LuneError}; +pub use crate::lune::{Lune, LuneError};