Browse Source

refactoring: move error catching match statement to other file

main
fdai7451 2 years ago
parent
commit
baaa219b40
  1. 19
      src/execute.rs
  2. 16
      src/main.rs

19
src/execute.rs

@ -8,7 +8,24 @@ use crate::error::ShellError;
use crate::parse::parse_line; use crate::parse::parse_line;
use crate::preprocess::preprocess; use crate::preprocess::preprocess;
pub fn interpret(
/// This function is not directly in main.rs because it might be called by other function too (eg.
/// when piping commands).
pub fn interpret(line: String, config: &mut BuiltinConfig, ctrlc_recv: Receiver<()>) {
match try_interpret(line, config, ctrlc_recv.clone()) {
Ok(_) | Err(ShellError::Interrupt) | Err(ShellError::EmptyLine) => (),
Err(ShellError::NotFound(cmd)) => {
eprintln!("{}: command not found", cmd.get_program().to_string_lossy())
}
Err(ShellError::ExecuteFailure(msg)) => {
eprintln!("{}", msg)
}
Err(ShellError::MalformedArgs(args)) => {
eprintln!("Malformed arguments: {}", args)
}
}
}
pub fn try_interpret(
mut line: String, mut line: String,
config: &mut BuiltinConfig, config: &mut BuiltinConfig,
ctrlc_recv: Receiver<()>, ctrlc_recv: Receiver<()>,

16
src/main.rs

@ -6,7 +6,6 @@ mod preprocess;
mod prompt; mod prompt;
use crate::builtins::BuiltinConfig; use crate::builtins::BuiltinConfig;
use crate::error::ShellError;
use execute::interpret; use execute::interpret;
use rustyline::config::Configurer; use rustyline::config::Configurer;
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
@ -30,20 +29,7 @@ fn main() -> Result<()> {
let readline = rl.readline(&prompt.get_prompt()); let readline = rl.readline(&prompt.get_prompt());
match readline { match readline {
Ok(line) => match interpret(line, &mut config, ctrlc_recv.clone()) {
Ok(_) => prompt.style = config.prompt_style.clone(),
Err(ShellError::EmptyLine) => continue,
Err(ShellError::NotFound(cmd)) => {
eprintln!("{}: command not found", cmd.get_program().to_string_lossy())
}
Err(ShellError::ExecuteFailure(msg)) => {
eprintln!("{}", msg)
}
Err(ShellError::MalformedArgs(args)) => {
eprintln!("Malformed arguments: {}", args)
}
Err(ShellError::Interrupt) => continue,
},
Ok(line) => interpret(line, &mut config, ctrlc_recv.clone()),
Err(ReadlineError::Interrupted) => continue, Err(ReadlineError::Interrupted) => continue,
Err(ReadlineError::Eof) => break, Err(ReadlineError::Eof) => break,
Err(err) => { Err(err) => {

Loading…
Cancel
Save