From 5b53d2989d233f72ad95bf47fbefb7cf6870e9bc Mon Sep 17 00:00:00 2001 From: Fractal-Tess Date: Fri, 19 May 2023 03:58:42 +0300 Subject: [PATCH] refactor --- src-tauri/src/commands.rs | 6 +++--- src-tauri/src/main.rs | 8 ++++---- src-tauri/src/state.rs | 20 +++++++++++--------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index e34bbd3..f7e4f3b 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1,5 +1,5 @@ +#![allow(clippy::needless_pass_by_value)] // This module shows examples of how to use IPC command handlers that can be invoked from the frontend. - use sha2::{Digest, Sha256}; use specta::collect_types; use tauri::{Builder, State, Wry}; @@ -8,7 +8,7 @@ use tauri_specta::ts; use crate::state::Store; // Exports a function for the tauri app instance to use and register all commands defined as frontend IPC command handlers. -pub fn register_commands(builder: Builder) -> Builder { +pub fn register_command_handlers(builder: Builder) -> Builder { // Specta generating typed binding interfaces #[cfg(debug_assertions)] ts::export( @@ -39,7 +39,7 @@ fn hash256sum(hash_input: String) -> String { let mut hasher = Sha256::new(); hasher.update(hash_input.as_bytes()); let result = hasher.finalize(); - format!("{:X}", result) + format!("{result:X}") } // Example command using managed state diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 568f9a4..1d84760 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -3,7 +3,7 @@ windows_subsystem = "windows" )] -use commands::register_commands; +use commands::register_command_handlers; use state::register_managed_state; use tauri::{Builder as TauriBuilder, RunEvent}; @@ -17,7 +17,7 @@ fn main() { let app = TauriBuilder::default().plugin(tauri_plugin_window_state::Builder::default().build()); // Register app commands - let app = register_commands(app); + let app = register_command_handlers(app); // Register app managed state let app = register_managed_state(app); @@ -26,8 +26,8 @@ fn main() { app.build(tauri::generate_context!()) .expect("error while running tauri application") .run(|_, e| { - if let RunEvent::Ready = e { + if matches!(e, RunEvent::Ready) { println!("Window is ready"); } - }) + }); } diff --git a/src-tauri/src/state.rs b/src-tauri/src/state.rs index 91f6eae..e18ee7c 100644 --- a/src-tauri/src/state.rs +++ b/src-tauri/src/state.rs @@ -1,9 +1,18 @@ // This module shows examples of how to use managed custom state. -use std::{collections::HashMap, sync::Mutex}; +use std::collections::HashMap; +use std::string::ToString; +use std::sync::Mutex; use tauri::{Builder, Wry}; +// Exports a function for the tauri app instance to use and register all commands defined as frontend IPC command handlers. +pub fn register_managed_state(builder: Builder) -> Builder { + let store = Store::default(); + + builder.manage(store) +} + #[derive(Default)] pub struct Store { store: Mutex>, @@ -20,13 +29,6 @@ impl Store { .lock() .expect("cannot lock store") .get(key) - .map(|val| val.to_string()) + .map(ToString::to_string) } } - -// Exports a function for the tauri app instance to use and register all commands defined as frontend IPC command handlers. -pub fn register_managed_state(builder: Builder) -> Builder { - let store = Store::default(); - - builder.manage(store) -}