Skip to content

Commit a2d8de6

Browse files
committed
Improve REPL error handling
1 parent 9856c88 commit a2d8de6

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

crates/cli/src/repl.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,51 @@ use rustyline::{error::ReadlineError, DefaultEditor};
66

77
use crate::Action;
88

9+
pub fn process_input(input: &str, dvd_bnd: &DvdBnd) -> Result<(), Box<dyn Error>> {
10+
let args = shlex::split(input).ok_or("failed to parse input")?;
11+
12+
let command = Action::augment_subcommands(clap::Command::new("repl").no_binary_name(true));
13+
let action = command
14+
.clone()
15+
.try_get_matches_from(args)
16+
.and_then(|matches| Action::from_arg_matches(&matches))
17+
.map_err(|e| e.to_string())?;
18+
19+
match action {
20+
Action::Repl => {
21+
println!("Already running repl.");
22+
Ok(())
23+
}
24+
action => {
25+
action.run(dvd_bnd)
26+
}
27+
}
28+
}
29+
930
pub fn begin(dvd_bnd: &DvdBnd) -> Result<(), Box<dyn Error>> {
1031
let mut rl = DefaultEditor::new()?;
1132
let _ = rl.load_history("history.txt");
1233

13-
let command = Action::augment_subcommands(clap::Command::new("repl").no_binary_name(true));
14-
1534
loop {
1635
let readline = rl.readline(">> ");
36+
1737
match readline {
1838
Ok(line) => {
1939
rl.add_history_entry(line.as_str())?;
20-
let args = shlex::split(&line).ok_or("failed to parse input")?;
21-
22-
let action = command
23-
.clone()
24-
.try_get_matches_from(args)
25-
.and_then(|matches| Action::from_arg_matches(&matches))
26-
.map_err(|e| e.to_string());
27-
28-
match action {
29-
Ok(Action::Repl) => {
30-
println!("Running repl inside repl doesn't make sense");
31-
continue;
32-
}
33-
Ok(action) => {
34-
let _ = action.run(dvd_bnd);
40+
41+
match process_input(&line, dvd_bnd) {
42+
Ok(_) => println!("Command successful"),
43+
Err(e) => {
44+
println!("{:#?}", e);
3545
}
36-
Err(e) => println!("{}", e),
37-
};
46+
}
3847
}
3948
Err(ReadlineError::Interrupted) => {
40-
println!("CTRL-C");
49+
println!("^C");
50+
continue;
4151
}
4252
Err(ReadlineError::Eof) => {
43-
println!("CTRL-D");
53+
println!("^D");
4454
break;
4555
}
4656
Err(err) => {

0 commit comments

Comments
 (0)