Skip to content

Commit

Permalink
Improve error messages, logging, and code consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
ninjadev64 committed Jun 2, 2024
1 parent 07d7b97 commit b82bc13
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 64 deletions.
4 changes: 2 additions & 2 deletions src-tauri/src/devices/prontokey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/events/inbound/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub async fn open_url(event: PayloadEvent<OpenUrlEvent>) -> Result<(), anyhow::E
}

pub async fn log_message(event: PayloadEvent<LogMessageEvent>) -> Result<(), anyhow::Error> {
log::info!("{}", event.payload.message);
log::info!("{}", event.payload.message.trim());
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/events/outbound/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"])))
Expand Down Expand Up @@ -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") {
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/plugins/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
44 changes: 22 additions & 22 deletions src-tauri/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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()?;
Expand All @@ -172,21 +172,21 @@ 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.
let child = Command::new("wine")
.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())
Expand All @@ -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())
Expand All @@ -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())
Expand Down
48 changes: 23 additions & 25 deletions src-tauri/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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,

Expand All @@ -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<String>,

Expand Down
5 changes: 2 additions & 3 deletions src-tauri/src/store/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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))?;
Expand Down
14 changes: 7 additions & 7 deletions src/components/PluginManager.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}`);
}
}
Expand Down Expand Up @@ -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}`);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<ActionList bind:this={actionList} />
<hr class="mt-2 border dark:border-neutral-700" />
<div class="flex flex-row">
<PluginManager bind:actionList bind:profileSelector />
<PluginManager actionList={() => actionList} profileSelector={() => profileSelector} />
<SettingsView />
</div>
</div>

0 comments on commit b82bc13

Please sign in to comment.