From b82bc136e22c779584972f0dff658a8516b3bf03 Mon Sep 17 00:00:00 2001 From: ninjadev64 <63245705+ninjadev64@users.noreply.github.com> Date: Sun, 2 Jun 2024 18:09:35 +0100 Subject: [PATCH] Improve error messages, logging, and code consistency --- src-tauri/src/devices/prontokey.rs | 4 +-- src-tauri/src/events/inbound/misc.rs | 2 +- src-tauri/src/events/outbound/states.rs | 2 +- src-tauri/src/main.rs | 8 ++++- src-tauri/src/plugins/manifest.rs | 2 +- src-tauri/src/plugins/mod.rs | 44 +++++++++++------------ src-tauri/src/shared.rs | 48 ++++++++++++------------- src-tauri/src/store/profiles.rs | 5 ++- src/components/PluginManager.svelte | 14 ++++---- src/routes/+page.svelte | 2 +- 10 files changed, 67 insertions(+), 64 deletions(-) diff --git a/src-tauri/src/devices/prontokey.rs b/src-tauri/src/devices/prontokey.rs index f0ea17d..ee1e5eb 100644 --- a/src-tauri/src/devices/prontokey.rs +++ b/src-tauri/src/devices/prontokey.rs @@ -17,7 +17,7 @@ enum ProntoKeyMessage { #[allow(clippy::assigning_clones)] pub async fn init(port: String) { let mut initialised = false; - let mut device_id = "".to_owned(); + let mut device_id = String::new(); let mut last_keys: [u8; 9] = [0; 9]; let mut last_sliders: [u16; 2] = [0; 2]; @@ -32,7 +32,7 @@ pub async fn init(port: String) { let _ = port.write("#".as_bytes()); let serial_buf: &mut [u8] = &mut [0; 64]; - let mut holding_string = String::from(""); + let mut holding_string = String::new(); loop { match port.read(serial_buf) { diff --git a/src-tauri/src/events/inbound/misc.rs b/src-tauri/src/events/inbound/misc.rs index b59e10e..844fdb5 100644 --- a/src-tauri/src/events/inbound/misc.rs +++ b/src-tauri/src/events/inbound/misc.rs @@ -21,7 +21,7 @@ pub async fn open_url(event: PayloadEvent) -> Result<(), anyhow::E } pub async fn log_message(event: PayloadEvent) -> Result<(), anyhow::Error> { - log::info!("{}", event.payload.message); + log::info!("{}", event.payload.message.trim()); Ok(()) } diff --git a/src-tauri/src/events/outbound/states.rs b/src-tauri/src/events/outbound/states.rs index b9656f3..2f909e9 100644 --- a/src-tauri/src/events/outbound/states.rs +++ b/src-tauri/src/events/outbound/states.rs @@ -55,7 +55,7 @@ pub async fn title_parameters_did_change(instance: &ActionInstance, state: u16) state: instance.current_state, title: state.text, titleParameters: TitleParameters { - fontFamily: "".to_owned(), + fontFamily: String::new(), fontSize: state.size.parse().unwrap(), fontStyle: state.style, fontUnderline: state.underline, diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 25f06c1..a72ddbf 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -63,6 +63,12 @@ async fn main() { tauri_plugin_log::Builder::default() .targets([LogTarget::LogDir, LogTarget::Stdout]) .level(log::LevelFilter::Debug) + .filter(|v| { + !matches!( + v.target(), + "tungstenite::handshake::server" | "tungstenite::protocol" | "tracing::span" | "zbus::handshake" | "zbus::connection" + ) + }) .build(), ) .plugin(tauri_plugin_autostart::init(tauri_plugin_autostart::MacosLauncher::LaunchAgent, Some(vec!["--hide"]))) @@ -91,7 +97,7 @@ async fn main() { .build(tauri::generate_context!()) { Ok(app) => app, - Err(error) => panic!("Failed to create Tauri application: {}", error), + Err(error) => panic!("failed to create Tauri application: {}", error), }; if std::env::args().any(|v| v == "--hide") { diff --git a/src-tauri/src/plugins/manifest.rs b/src-tauri/src/plugins/manifest.rs index 258a63f..cb71c48 100644 --- a/src-tauri/src/plugins/manifest.rs +++ b/src-tauri/src/plugins/manifest.rs @@ -25,7 +25,7 @@ pub struct PluginManifest { #[serde(alias = "Icon")] pub icon: String, - #[serde_inline_default(String::from("Custom"))] + #[serde_inline_default("Custom".to_owned())] #[serde(alias = "Category")] pub category: String, diff --git a/src-tauri/src/plugins/mod.rs b/src-tauri/src/plugins/mod.rs index 1ebb06e..d3bee46 100644 --- a/src-tauri/src/plugins/mod.rs +++ b/src-tauri/src/plugins/mod.rs @@ -34,8 +34,8 @@ pub async fn initialise_plugin(path: &path::PathBuf) -> anyhow::Result<()> { let plugin_uuid = path.file_name().unwrap().to_str().unwrap(); let manifest_path = path.join("manifest.json"); - let manifest = fs::read(&manifest_path).context("Failed to read manifest")?; - let mut manifest: manifest::PluginManifest = serde_json::from_slice(&manifest).context("Failed to parse manifest")?; + let manifest = fs::read(&manifest_path).context("failed to read manifest")?; + let mut manifest: manifest::PluginManifest = serde_json::from_slice(&manifest).context("failed to parse manifest")?; for action in &mut manifest.actions { plugin_uuid.clone_into(&mut action.plugin); @@ -145,7 +145,7 @@ pub async fn initialise_plugin(path: &path::PathBuf) -> anyhow::Result<()> { if code_path.ends_with(".html") { // Create a webview window for the plugin and call its registration function. - let url = String::from("http://localhost:57118/") + path.join(code_path).to_str().unwrap(); + let url = "http://localhost:57118/".to_owned() + path.join(code_path).to_str().unwrap(); let window = tauri::WindowBuilder::new(APP_HANDLE.get().unwrap(), plugin_uuid.replace('.', "_"), tauri::WindowUrl::External(url.parse()?)) .visible(false) .build()?; @@ -172,7 +172,7 @@ pub async fn initialise_plugin(path: &path::PathBuf) -> anyhow::Result<()> { INSTANCES.lock().await.insert(plugin_uuid.to_owned(), PluginInstance::Webview); } else if use_wine { if Command::new("wine").stdout(Stdio::null()).stderr(Stdio::null()).spawn().is_err() { - return Err(anyhow!("Failed to detect an installation of Wine to run plugin {}", plugin_uuid)); + return Err(anyhow!("failed to detect an installation of Wine")); } // Start Wine with the appropriate arguments. @@ -180,13 +180,13 @@ pub async fn initialise_plugin(path: &path::PathBuf) -> anyhow::Result<()> { .current_dir(path) .args([ code_path, - String::from("-port"), - 57116.to_string(), - String::from("-pluginUUID"), + "-port".to_owned(), + "57116".to_owned(), + "-pluginUUID".to_owned(), plugin_uuid.to_owned(), - String::from("-registerEvent"), - String::from("registerPlugin"), - String::from("-info"), + "-registerEvent".to_owned(), + "registerPlugin".to_owned(), + "-info".to_owned(), serde_json::to_string(&info)?, ]) .stdout(Stdio::null()) @@ -200,13 +200,13 @@ pub async fn initialise_plugin(path: &path::PathBuf) -> anyhow::Result<()> { let child = Command::new(path.join(code_path)) .current_dir(path) .args([ - String::from("-port"), - 57116.to_string(), - String::from("-pluginUUID"), + "-port".to_owned(), + "57116".to_owned(), + "-pluginUUID".to_owned(), plugin_uuid.to_owned(), - String::from("-registerEvent"), - String::from("registerPlugin"), - String::from("-info"), + "-registerEvent".to_owned(), + "registerPlugin".to_owned(), + "-info".to_owned(), serde_json::to_string(&info)?, ]) .stdout(Stdio::null()) @@ -217,13 +217,13 @@ pub async fn initialise_plugin(path: &path::PathBuf) -> anyhow::Result<()> { let child = Command::new(path.join(code_path)) .current_dir(path) .args([ - String::from("-port"), - 57116.to_string(), - String::from("-pluginUUID"), + "-port".to_owned(), + "57116".to_owned(), + "-pluginUUID".to_owned(), plugin_uuid.to_owned(), - String::from("-registerEvent"), - String::from("registerPlugin"), - String::from("-info"), + "-registerEvent".to_owned(), + "registerPlugin".to_owned(), + "-info".to_owned(), serde_json::to_string(&info)?, ]) .stdout(Stdio::null()) diff --git a/src-tauri/src/shared.rs b/src-tauri/src/shared.rs index d7b1dd7..71b300f 100644 --- a/src-tauri/src/shared.rs +++ b/src-tauri/src/shared.rs @@ -19,50 +19,48 @@ pub fn convert_icon(path: String) -> String { } /// A state of an action. -#[serde_inline_default] #[derive(Clone, Serialize, Deserialize)] +#[serde(default)] pub struct ActionState { - #[serde_inline_default(String::from("actionDefaultImage"))] #[serde(alias = "Image")] pub image: String, - - #[serde_inline_default(String::from(""))] #[serde(alias = "Name")] pub name: String, - - #[serde_inline_default(String::from(""))] #[serde(alias = "Title")] pub text: String, - - #[serde_inline_default(true)] #[serde(alias = "ShowTitle")] pub show: bool, - - #[serde_inline_default(String::from("#f2f2f2"))] #[serde(alias = "TitleColor")] pub colour: String, - - #[serde_inline_default(String::from("middle"))] #[serde(alias = "TitleAlignment")] pub alignment: String, - - #[serde_inline_default(String::from("Liberation Sans"))] #[serde(alias = "FontFamily")] pub family: String, - - #[serde_inline_default(String::from("Regular"))] #[serde(alias = "FontStyle")] pub style: String, - - #[serde_inline_default(String::from("16"))] #[serde(alias = "FontSize")] pub size: String, - - #[serde_inline_default(false)] #[serde(alias = "FontUnderline")] pub underline: bool, } +impl Default for ActionState { + fn default() -> Self { + Self { + image: "actionDefaultImage".to_owned(), + name: String::new(), + text: String::new(), + show: true, + colour: "#FFFFFF".to_owned(), + alignment: "middle".to_owned(), + family: "Liberation Sans".to_owned(), + style: "Regular".to_owned(), + size: "16".to_owned(), + underline: false, + } + } +} + /// An action, deserialised from the plugin manifest. #[serde_inline_default] #[derive(Clone, Serialize, Deserialize)] @@ -73,14 +71,14 @@ pub struct Action { #[serde(alias = "UUID")] pub uuid: String, - #[serde_inline_default(String::from(""))] + #[serde_inline_default(String::new())] pub plugin: String, - #[serde_inline_default(String::from(""))] + #[serde_inline_default(String::new())] #[serde(alias = "Tooltip")] pub tooltip: String, - #[serde_inline_default(String::from(""))] + #[serde_inline_default(String::new())] #[serde(alias = "Icon")] pub icon: String, @@ -100,11 +98,11 @@ pub struct Action { #[serde(alias = "UserTitleEnabled")] pub user_title_enabled: bool, - #[serde_inline_default(String::from(""))] + #[serde_inline_default(String::new())] #[serde(alias = "PropertyInspectorPath")] pub property_inspector: String, - #[serde_inline_default(vec![String::from("Keypad")])] + #[serde_inline_default(vec!["Keypad".to_owned()])] #[serde(alias = "Controllers")] pub controllers: Vec, diff --git a/src-tauri/src/store/profiles.rs b/src-tauri/src/store/profiles.rs index 2870821..f8f3918 100644 --- a/src-tauri/src/store/profiles.rs +++ b/src-tauri/src/store/profiles.rs @@ -3,7 +3,6 @@ use crate::shared::Profile; use std::collections::HashMap; use std::fs; -use std::iter::repeat_with; use std::path::PathBuf; use serde::{Deserialize, Serialize}; @@ -40,8 +39,8 @@ impl ProfileStores { } else { let default = Profile { id: id.to_owned(), - keys: repeat_with(Vec::new).take((device.rows * device.columns).into()).collect(), - sliders: repeat_with(Vec::new).take(device.sliders.into()).collect(), + keys: vec![vec![]; (device.rows * device.columns) as usize], + sliders: vec![vec![]; device.sliders as usize], }; let store = Store::new(path, app.path_resolver().app_config_dir().unwrap(), default).context(format!("Failed to create store for profile {}", path))?; diff --git a/src/components/PluginManager.svelte b/src/components/PluginManager.svelte index bbbeb59..e31890c 100644 --- a/src/components/PluginManager.svelte +++ b/src/components/PluginManager.svelte @@ -12,8 +12,8 @@ import { invoke } from "@tauri-apps/api"; - export let actionList: ActionList; - export let profileSelector: ProfileSelector; + export let actionList: () => ActionList; + export let profileSelector: () => ProfileSelector; let showPopup: boolean; @@ -22,10 +22,10 @@ try { await invoke("install_plugin", { id, url }); alert(`Successfully installed "${name}".`); - actionList.reload(); + actionList().reload(); installed = await invoke("list_plugins"); } catch (error: any) { - alert(`Failed to install ${name}: ${error}`); + alert(`Failed to install ${name}: ${error.description ?? error}`); } } @@ -78,11 +78,11 @@ try { await invoke("remove_plugin", { id: plugin.id }); alert(`Successfully removed "${plugin.name}".`); - actionList.reload(); - profileSelector.reload(); + actionList().reload(); + profileSelector().reload(); installed = await invoke("list_plugins"); } catch (error: any) { - alert(`Failed to remove ${plugin.name}: ${error}`); + alert(`Failed to remove ${plugin.name}: ${error.description ?? error}`); } } diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 0d6c8d9..a827ba7 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -52,7 +52,7 @@
- + actionList} profileSelector={() => profileSelector} />