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 {