feat: implement command mode

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-05-09 00:15:42 +02:00
parent 445a628ff5
commit ed5a5db7c5
8 changed files with 405 additions and 57 deletions

View File

@@ -0,0 +1,31 @@
use crate::models::Msg;
pub trait IntoCommand {
fn into_command(self) -> Command;
}
impl IntoCommand for () {
fn into_command(self) -> Command {
Command::new(|| None)
}
}
impl IntoCommand for Command {
fn into_command(self) -> Command {
self
}
}
pub struct Command {
func: Box<dyn FnOnce() -> Option<Msg>>,
}
impl Command {
pub fn new<T: FnOnce() -> Option<Msg> + 'static>(f: T) -> Self {
Self { func: Box::new(f) }
}
pub fn execute(self) -> Option<Msg> {
self.func.call_once(())
}
}