From 866ec7c277d11b41743001b2e86a900fa2b6f7ab Mon Sep 17 00:00:00 2001 From: Soispha Date: Mon, 17 Jul 2023 00:17:53 +0200 Subject: [PATCH] Refactor(ci_command_handling): Move to the event handlers --- .../event_types/event/handlers/ci_output.rs | 11 ++++++++++ .../event_types/event/handlers/lua_command.rs | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/app/events/event_types/event/handlers/ci_output.rs create mode 100644 src/app/events/event_types/event/handlers/lua_command.rs diff --git a/src/app/events/event_types/event/handlers/ci_output.rs b/src/app/events/event_types/event/handlers/ci_output.rs new file mode 100644 index 0000000..9df9da4 --- /dev/null +++ b/src/app/events/event_types/event/handlers/ci_output.rs @@ -0,0 +1,11 @@ +use anyhow::Result; +use cli_log::info; + +use crate::app::{events::event_types::EventStatus, App}; + +pub async fn handle(app: &mut App<'_>, output: &String) -> Result { + info!("Recieved command output: `{}`", output); + app.ui.set_command_output(output); + + Ok(EventStatus::Ok) +} diff --git a/src/app/events/event_types/event/handlers/lua_command.rs b/src/app/events/event_types/event/handlers/lua_command.rs new file mode 100644 index 0000000..ac459e6 --- /dev/null +++ b/src/app/events/event_types/event/handlers/lua_command.rs @@ -0,0 +1,22 @@ +use anyhow::{Context, Result}; +use cli_log::info; + +use crate::app::{App, events::event_types::{EventStatus, Event}}; + +pub async fn handle(app: &mut App<'_>, command: &str) -> Result { + info!("Recieved ci command: `{command}`; executing.."); + + // TODO: Should the ci support more than strings? + let output = app.lua.context(|context| -> Result { + let output = context + .load(&command) + .eval::() + .with_context(|| format!("Failed to execute: `{command}`"))?; + info!("Function evaluated to: `{output}`"); + Ok(output) + })?; + + app.transmitter.send(Event::CiOutput(output)); + + Ok(EventStatus::Ok) +}