builtin : organisation du code

This commit is contained in:
Ahurac 2024-04-14 15:11:52 +02:00
parent bb5598aee7
commit 08b7b57943

View file

@ -6,30 +6,6 @@ use std::{env::set_current_dir, path::PathBuf};
type BuiltinFunction = fn(&Vec<String>) -> ExitCode; type BuiltinFunction = fn(&Vec<String>) -> ExitCode;
pub struct Builtin {
function: BuiltinFunction,
args: Vec<String>,
}
impl Builtin {
pub fn new(argv: &Vec<String>) -> Result<Self, NoSuchBuiltinError> {
let mut args = argv.clone();
let program = args.remove(0);
if program == "cd" {
Ok(Self { function: cd, args })
} else {
Err(NoSuchBuiltinError)
}
}
}
impl Command for Builtin {
fn spawn(&mut self) -> ExitCode {
(self.function)(&self.args)
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct NoSuchBuiltinError; pub struct NoSuchBuiltinError;
@ -61,3 +37,27 @@ fn cd(args: &Vec<String>) -> ExitCode {
exit_code exit_code
} }
pub struct Builtin {
function: BuiltinFunction,
args: Vec<String>,
}
impl Builtin {
pub fn new(argv: &Vec<String>) -> Result<Self, NoSuchBuiltinError> {
let mut args = argv.clone();
let program = args.remove(0);
if program == "cd" {
Ok(Self { function: cd, args })
} else {
Err(NoSuchBuiltinError)
}
}
}
impl Command for Builtin {
fn spawn(&mut self) -> ExitCode {
(self.function)(&self.args)
}
}