From 7ffbc9a6084cac9adc7256a9f1b2bf343914eff4 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Thu, 11 Apr 2024 01:15:20 +0200 Subject: [PATCH] =?UTF-8?q?src=20:=20la=20premi=C3=A8re=20commande=20de=20?= =?UTF-8?q?`CommandSequence`=20n'est=20plus=20optionnelle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/command.rs | 21 ++++++--------------- src/parser.rs | 4 ++-- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/command.rs b/src/command.rs index d013279..ac5479e 100644 --- a/src/command.rs +++ b/src/command.rs @@ -23,14 +23,14 @@ pub trait Command { } pub struct CommandSequence { - command: Option>, + command: Box, next_command: Option>, } impl CommandSequence { - pub fn new() -> Self { + pub fn new(command: impl Command + 'static) -> Self { Self { - command: None, + command: Box::new(command), next_command: None, } } @@ -47,11 +47,7 @@ impl CommandSequence { impl Command for CommandSequence { fn spawn(&self) -> ExitCode { - let mut exit_code = ExitCode::new(0); - - if self.command.is_some() { - exit_code = self.command.as_ref().unwrap().spawn(); - } + let mut exit_code = self.command.spawn(); if self.next_command.is_some() { exit_code = self.next_command.as_ref().unwrap().spawn(); @@ -66,13 +62,8 @@ pub struct UnixProgram { } impl UnixProgram { - pub fn new() -> Self { - Self { argv: Vec::new() } - } - - pub fn argv(&mut self, argv: Vec) -> &mut Self { - self.argv = argv; - self + pub fn new(argv: Vec) -> Self { + Self { argv } } } diff --git a/src/parser.rs b/src/parser.rs index dbdead1..a48dd30 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -4,8 +4,8 @@ pub fn parse_command_line(line: String) -> Option { let argv: Vec = line.split_whitespace().map(|s| s.to_string()).collect(); if !argv.is_empty() { - let _command = UnixProgram::new().argv(argv); - let command_sequence = CommandSequence::new(); + let command = UnixProgram::new(argv); + let command_sequence = CommandSequence::new(command); // command_sequence.add(_command); Some(command_sequence) } else {