From d77c7351aabac535839a98023e85052e32c73a2b Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Thu, 16 Feb 2023 10:59:26 +0100 Subject: [PATCH] Fix: compile for windows and handle errors --- receiver/src/comm.rs | 7 +++++-- receiver/src/keyboard.rs | 10 ++++++---- receiver/src/main.rs | 9 +++++---- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/receiver/src/comm.rs b/receiver/src/comm.rs index c16f654..32b3347 100644 --- a/receiver/src/comm.rs +++ b/receiver/src/comm.rs @@ -4,7 +4,7 @@ extern crate rumqttc as mqtt; use serde::{Deserialize, Serialize}; -pub type TimeCallback = fn(u32) -> (); +pub type TimeCallback = fn(u32) -> Result<(),()>; #[derive(Serialize, Deserialize, PartialEq)] enum MqttMessageKind { @@ -95,7 +95,10 @@ impl Comm { return; } - (self.handler)(msg.time); + if (self.handler)(msg.time).is_err() { + println!("[WARN] error in time handler -> not sending confirmation"); + return; + } let reply = MqttMessage { id: msg.id, diff --git a/receiver/src/keyboard.rs b/receiver/src/keyboard.rs index eb4dcc3..d6d7e01 100644 --- a/receiver/src/keyboard.rs +++ b/receiver/src/keyboard.rs @@ -1,20 +1,22 @@ #[cfg(target_family = "unix")] use enigo::{KeyboardControllable}; +#[cfg(target_family = "unix")] pub fn type_text(text: &str) { enigo::Enigo::default().key_sequence(text); } +#[cfg(target_family = "unix")] pub fn click_tab() { enigo::Enigo::default().key_click(enigo::Key::Tab); } #[cfg(target_family = "windows")] -pub fn type_text(&self, text: &str) { - simulate::type_str(text); +pub fn type_text(text: &str) -> Result<(),()>{ + simulate::type_str(text).map_err(|_| ()) } #[cfg(target_family = "windows")] -pub fn click_tab(&self) { - simulate::send(simulate::Key::Tab); +pub fn click_tab() -> Result<(),()> { + simulate::send(simulate::Key::Tab).map_err(|_| ()) } \ No newline at end of file diff --git a/receiver/src/main.rs b/receiver/src/main.rs index 6a05b8e..4516dff 100644 --- a/receiver/src/main.rs +++ b/receiver/src/main.rs @@ -66,14 +66,15 @@ fn millis_to_string(millis: u32) -> String { formatted.replace(".", ",") } -fn handle_time(time: u32) { +fn handle_time(time: u32) -> Result<(),()> { println!("Got time: {}", time); let time_with_comma = millis_to_string(time); println!("Trying to type {time_with_comma}"); - keyboard::type_text(&time_with_comma); - keyboard::click_tab(); - keyboard::click_tab(); + keyboard::type_text(&time_with_comma)?; + keyboard::click_tab()?; + keyboard::click_tab()?; + Ok(()) } fn main() {