Skip to content

Commit

Permalink
Improve REPL error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
garyttierney committed Mar 20, 2024
1 parent 9856c88 commit a2d8de6
Showing 1 changed file with 31 additions and 21 deletions.
52 changes: 31 additions & 21 deletions crates/cli/src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,51 @@ use rustyline::{error::ReadlineError, DefaultEditor};

use crate::Action;

pub fn process_input(input: &str, dvd_bnd: &DvdBnd) -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
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) => {
Expand Down

0 comments on commit a2d8de6

Please sign in to comment.