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::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>;
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() {
let path = path.unwrap();
let result = set_current_dir(&path);
@ -25,13 +25,11 @@ pub(super) fn cd(args: &Vec<String>) -> ExitCode {
if result.is_err() {
let error = CdError::new(&args[0]);
error::print(Box::new(error));
exit_code = ExitCode::new(2);
exit_code.set(2);
}
}
}
exit_code
}
pub(super) fn colon(_args: &Vec<String>) -> ExitCode {
ExitCode::success()
pub(super) fn colon(_args: &Vec<String>, exit_code: &mut ExitCode) {
exit_code.set_success();
}

View file

@ -34,17 +34,13 @@ impl CommandSequence {
}
impl Command for CommandSequence {
fn spawn(&mut self) -> ExitCode {
let mut exit_code = ExitCode::success();
fn spawn(&mut self, exit_code: &mut ExitCode) {
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() {
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::exit_code::ExitCode;
type BuiltinFunction = fn(&Vec<String>) -> ExitCode;
type BuiltinFunction = fn(&Vec<String>, &mut ExitCode);
#[derive(Debug)]
pub struct NoSuchBuiltinError;
pub trait Command {
fn spawn(&mut self) -> ExitCode;
fn spawn(&mut self, exit_code: &mut ExitCode);
}
pub struct Builtin {
@ -43,8 +43,8 @@ impl Builtin {
}
impl Command for Builtin {
fn spawn(&mut self) -> ExitCode {
(self.function)(&self.args)
fn spawn(&mut self, exit_code: &mut ExitCode) {
(self.function)(&self.args, exit_code)
}
}
@ -64,27 +64,25 @@ impl UnixProgram {
}
impl Command for UnixProgram {
fn spawn(&mut self) -> ExitCode {
fn spawn(&mut self, exit_code: &mut ExitCode) {
let handle = self.command.spawn();
if handle.is_ok() {
let exit_code = handle
let raw_exit_code = handle
.unwrap()
.wait()
.expect("error waiting for the child")
.code()
.unwrap_or(1);
let exit_code = match u8::try_from(exit_code) {
Ok(code) => ExitCode::new(code),
Err(_e) => ExitCode::new(u8::MAX),
};
exit_code
exit_code.set(match u8::try_from(raw_exit_code) {
Ok(code) => code,
Err(_e) => u8::MAX,
});
} else {
let error = CommandNotFoundError::new(self.command.get_program().to_str().unwrap());
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);
if !command_sequence.is_empty() {
current_exit_code = command_sequence.spawn();
command_sequence.spawn(&mut current_exit_code);
}
} else {
println!();

View file

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