exit_code : passage en mode référence mutable

This commit is contained in:
Ahurac 2024-04-19 10:24:13 +02:00
parent 2337bd3127
commit 74fb3573be
5 changed files with 28 additions and 32 deletions

View file

@ -5,7 +5,7 @@ use crate::exit_code::ExitCode;
use std::env; use std::env;
use std::{env::set_current_dir, path::PathBuf}; use std::{env::set_current_dir, path::PathBuf};
pub(super) fn cd(args: &Vec<String>) -> ExitCode { pub(super) fn cd(args: &Vec<String>, exit_code: &mut ExitCode) {
let path: Option<PathBuf>; let path: Option<PathBuf>;
if !args.is_empty() { if !args.is_empty() {
@ -17,7 +17,7 @@ pub(super) fn cd(args: &Vec<String>) -> ExitCode {
}; };
} }
let mut exit_code = ExitCode::success(); exit_code.set_success();
if path.is_some() { if path.is_some() {
let path = path.unwrap(); let path = path.unwrap();
let result = set_current_dir(&path); let result = set_current_dir(&path);
@ -25,13 +25,11 @@ pub(super) fn cd(args: &Vec<String>) -> ExitCode {
if result.is_err() { if result.is_err() {
let error = CdError::new(&args[0]); let error = CdError::new(&args[0]);
error::print(Box::new(error)); error::print(Box::new(error));
exit_code = ExitCode::new(2); exit_code.set(2);
} }
} }
exit_code
} }
pub(super) fn colon(_args: &Vec<String>) -> ExitCode { pub(super) fn colon(_args: &Vec<String>, exit_code: &mut ExitCode) {
ExitCode::success() exit_code.set_success();
} }

View file

@ -34,17 +34,13 @@ impl CommandSequence {
} }
impl Command for CommandSequence { impl Command for CommandSequence {
fn spawn(&mut self) -> ExitCode { fn spawn(&mut self, exit_code: &mut ExitCode) {
let mut exit_code = ExitCode::success();
if self.command.is_some() { if self.command.is_some() {
exit_code = self.command.as_mut().unwrap().spawn(); self.command.as_mut().unwrap().spawn(exit_code);
if self.next_command.is_some() { if self.next_command.is_some() {
exit_code = self.next_command.as_mut().unwrap().spawn(); self.next_command.as_mut().unwrap().spawn(exit_code);
} }
} }
exit_code
} }
} }

View file

@ -6,13 +6,13 @@ use crate::error;
use crate::error::CommandNotFoundError; use crate::error::CommandNotFoundError;
use crate::exit_code::ExitCode; use crate::exit_code::ExitCode;
type BuiltinFunction = fn(&Vec<String>) -> ExitCode; type BuiltinFunction = fn(&Vec<String>, &mut ExitCode);
#[derive(Debug)] #[derive(Debug)]
pub struct NoSuchBuiltinError; pub struct NoSuchBuiltinError;
pub trait Command { pub trait Command {
fn spawn(&mut self) -> ExitCode; fn spawn(&mut self, exit_code: &mut ExitCode);
} }
pub struct Builtin { pub struct Builtin {
@ -43,8 +43,8 @@ impl Builtin {
} }
impl Command for Builtin { impl Command for Builtin {
fn spawn(&mut self) -> ExitCode { fn spawn(&mut self, exit_code: &mut ExitCode) {
(self.function)(&self.args) (self.function)(&self.args, exit_code)
} }
} }
@ -64,27 +64,25 @@ impl UnixProgram {
} }
impl Command for UnixProgram { impl Command for UnixProgram {
fn spawn(&mut self) -> ExitCode { fn spawn(&mut self, exit_code: &mut ExitCode) {
let handle = self.command.spawn(); let handle = self.command.spawn();
if handle.is_ok() { if handle.is_ok() {
let exit_code = handle let raw_exit_code = handle
.unwrap() .unwrap()
.wait() .wait()
.expect("error waiting for the child") .expect("error waiting for the child")
.code() .code()
.unwrap_or(1); .unwrap_or(1);
let exit_code = match u8::try_from(exit_code) { exit_code.set(match u8::try_from(raw_exit_code) {
Ok(code) => ExitCode::new(code), Ok(code) => code,
Err(_e) => ExitCode::new(u8::MAX), Err(_e) => u8::MAX,
}; });
exit_code
} else { } else {
let error = CommandNotFoundError::new(self.command.get_program().to_str().unwrap()); let error = CommandNotFoundError::new(self.command.get_program().to_str().unwrap());
error::print(Box::new(error)); error::print(Box::new(error));
ExitCode::not_found() exit_code.set_command_not_found();
} }
} }
} }

View file

@ -21,7 +21,7 @@ pub fn run() {
let mut command_sequence = parse(user_input.unwrap(), &variables); let mut command_sequence = parse(user_input.unwrap(), &variables);
if !command_sequence.is_empty() { if !command_sequence.is_empty() {
current_exit_code = command_sequence.spawn(); command_sequence.spawn(&mut current_exit_code);
} }
} else { } else {
println!(); println!();

View file

@ -11,11 +11,15 @@ impl ExitCode {
self.exit_code self.exit_code
} }
pub fn not_found() -> Self { pub fn set(&mut self, new_code: u8) {
Self { exit_code: 127 } self.exit_code = new_code;
} }
pub fn success() -> Self { pub fn set_success(&mut self) {
Self { exit_code: 0 } self.exit_code = 0;
}
pub fn set_command_not_found(&mut self) {
self.exit_code = 127;
} }
} }