feat: add command pattern
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-05-12 14:29:14 +02:00
parent cf26422673
commit 5548d8e36e
10 changed files with 318 additions and 207 deletions

View File

@@ -0,0 +1,42 @@
use super::IntoCommand;
#[derive(Default)]
pub struct BatchCommand {
commands: Vec<super::Command>,
}
impl BatchCommand {
pub fn with(&mut self, cmd: impl IntoCommand) -> &mut Self {
self.commands.push(cmd.into_command());
self
}
}
impl IntoCommand for Vec<super::Command> {
fn into_command(self) -> super::Command {
BatchCommand::from(self).into_command()
}
}
impl From<Vec<super::Command>> for BatchCommand {
fn from(value: Vec<super::Command>) -> Self {
BatchCommand { commands: value }
}
}
impl IntoCommand for BatchCommand {
fn into_command(self) -> super::Command {
super::Command::new(|dispatch| {
for command in self.commands {
let msg = command.execute(dispatch.clone());
if let Some(msg) = msg {
tracing::info!("executing batch command for msg: {:?}", msg);
dispatch.send(msg);
}
}
None
})
}
}