Browse Source

feat: exit directly when using exit command

main
fdai7451 2 years ago
parent
commit
f0bd9f8397
  1. 6
      src/builtins/exit.rs
  2. 2
      src/error.rs
  3. 3
      src/main.rs

6
src/builtins/exit.rs

@ -5,11 +5,11 @@ pub struct Exit;
impl Builtin for Exit { impl Builtin for Exit {
fn execute(&mut self, _: Vec<String>) -> Result<(), ShellError> { fn execute(&mut self, _: Vec<String>) -> Result<(), ShellError> {
Err(ShellError::Exit)
std::process::exit(0)
} }
} }
#[cfg(test)]
/*#[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@ -22,4 +22,4 @@ mod tests {
fn test_exit_without_args() { fn test_exit_without_args() {
assert_eq!(Exit.execute(vec![]), Err(ShellError::Exit)) assert_eq!(Exit.execute(vec![]), Err(ShellError::Exit))
} }
}
}*/

2
src/error.rs

@ -3,7 +3,6 @@ use std::process::Command;
#[derive(Debug)] #[derive(Debug)]
pub enum ShellError { pub enum ShellError {
EmptyLine, EmptyLine,
Exit,
NotFound(Command), NotFound(Command),
ExecuteFailure(String), ExecuteFailure(String),
MalformedArgs(String), MalformedArgs(String),
@ -13,7 +12,6 @@ impl PartialEq<Self> for ShellError {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
match self { match self {
&ShellError::EmptyLine => matches!(other, ShellError::EmptyLine), &ShellError::EmptyLine => matches!(other, ShellError::EmptyLine),
&ShellError::Exit => matches!(other, ShellError::Exit),
&ShellError::NotFound(_) => matches!(other, ShellError::NotFound(_)), &ShellError::NotFound(_) => matches!(other, ShellError::NotFound(_)),
&ShellError::ExecuteFailure(_) => matches!(other, ShellError::ExecuteFailure(_)), &ShellError::ExecuteFailure(_) => matches!(other, ShellError::ExecuteFailure(_)),
&ShellError::MalformedArgs(_) => matches!(other, ShellError::MalformedArgs(_)), &ShellError::MalformedArgs(_) => matches!(other, ShellError::MalformedArgs(_)),

3
src/main.rs

@ -25,9 +25,6 @@ fn main() -> Result<()> {
Err(ShellError::EmptyLine) => { Err(ShellError::EmptyLine) => {
continue; continue;
} }
Err(ShellError::Exit) => {
break;
}
Err(ShellError::NotFound(cmd)) => { Err(ShellError::NotFound(cmd)) => {
eprintln!("{}: command not found", cmd.get_program().to_string_lossy()) eprintln!("{}: command not found", cmd.get_program().to_string_lossy())
} }

Loading…
Cancel
Save