From a2d8de6a8af5c569ff933f0e4983c6d9b1b091d7 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Wed, 20 Mar 2024 03:53:02 +0000 Subject: [PATCH] Improve REPL error handling --- crates/cli/src/repl.rs | 52 +++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/crates/cli/src/repl.rs b/crates/cli/src/repl.rs index b1a9e94..5cc4021 100644 --- a/crates/cli/src/repl.rs +++ b/crates/cli/src/repl.rs @@ -6,41 +6,51 @@ use rustyline::{error::ReadlineError, DefaultEditor}; use crate::Action; +pub fn process_input(input: &str, dvd_bnd: &DvdBnd) -> Result<(), Box> { + let args = shlex::split(input).ok_or("failed to parse input")?; + + let command = Action::augment_subcommands(clap::Command::new("repl").no_binary_name(true)); + let action = command + .clone() + .try_get_matches_from(args) + .and_then(|matches| Action::from_arg_matches(&matches)) + .map_err(|e| e.to_string())?; + + match action { + Action::Repl => { + println!("Already running repl."); + Ok(()) + } + action => { + action.run(dvd_bnd) + } + } +} + pub fn begin(dvd_bnd: &DvdBnd) -> Result<(), Box> { let mut rl = DefaultEditor::new()?; let _ = rl.load_history("history.txt"); - let command = Action::augment_subcommands(clap::Command::new("repl").no_binary_name(true)); - loop { let readline = rl.readline(">> "); + match readline { Ok(line) => { rl.add_history_entry(line.as_str())?; - let args = shlex::split(&line).ok_or("failed to parse input")?; - - let action = command - .clone() - .try_get_matches_from(args) - .and_then(|matches| Action::from_arg_matches(&matches)) - .map_err(|e| e.to_string()); - - match action { - Ok(Action::Repl) => { - println!("Running repl inside repl doesn't make sense"); - continue; - } - Ok(action) => { - let _ = action.run(dvd_bnd); + + match process_input(&line, dvd_bnd) { + Ok(_) => println!("Command successful"), + Err(e) => { + println!("{:#?}", e); } - Err(e) => println!("{}", e), - }; + } } Err(ReadlineError::Interrupted) => { - println!("CTRL-C"); + println!("^C"); + continue; } Err(ReadlineError::Eof) => { - println!("CTRL-D"); + println!("^D"); break; } Err(err) => {