From 6c26f8967bd00321cace2f371265656f644e94ed Mon Sep 17 00:00:00 2001 From: XIAO YU Date: Wed, 19 Jun 2024 16:36:59 +0900 Subject: [PATCH] chore: Refactor code for better performance and reliability (#212) --- src-tauri/src/main.rs | 14 +++++++------- src-tauri/src/nostr/event.rs | 6 +++--- src-tauri/src/nostr/keys.rs | 31 ++++++++++++++----------------- src-tauri/src/nostr/relay.rs | 6 +++--- src-tauri/src/nostr/utils.rs | 4 ++-- 5 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 8e1d07be..1d84005b 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -9,20 +9,20 @@ extern crate cocoa; #[macro_use] extern crate objc; +use std::sync::Mutex; +use std::time::Duration; use std::{ fs, io::{self, BufRead}, str::FromStr, }; -use std::sync::Mutex; -use std::time::Duration; use nostr_sdk::prelude::*; use serde::{Deserialize, Serialize}; use specta::Type; -use tauri::{Manager, path::BaseDirectory}; #[cfg(target_os = "macos")] use tauri::tray::{MouseButtonState, TrayIconEvent}; +use tauri::{path::BaseDirectory, Manager}; use tauri_nspanel::ManagerExt; use tauri_plugin_decorum::WebviewWindowExt; @@ -152,11 +152,11 @@ fn main() { // Create panel #[cfg(target_os = "macos")] - swizzle_to_menubar_panel(&app.handle()); + swizzle_to_menubar_panel(app.handle()); #[cfg(target_os = "macos")] - update_menubar_appearance(&app.handle()); + update_menubar_appearance(app.handle()); #[cfg(target_os = "macos")] - setup_menubar_panel_listeners(&app.handle()); + setup_menubar_panel_listeners(app.handle()); // Setup tray icon #[cfg(target_os = "macos")] @@ -173,7 +173,7 @@ fn main() { match panel.is_visible() { true => panel.order_out(None), false => { - position_menubar_panel(&app, 0.0); + position_menubar_panel(app, 0.0); panel.show(); } } diff --git a/src-tauri/src/nostr/event.rs b/src-tauri/src/nostr/event.rs index 3c85c8ca..da67c39a 100644 --- a/src-tauri/src/nostr/event.rs +++ b/src-tauri/src/nostr/event.rs @@ -6,8 +6,8 @@ use serde::Serialize; use specta::Type; use tauri::State; +use crate::nostr::utils::{create_event_tags, dedup_event, parse_event, Meta}; use crate::Nostr; -use crate::nostr::utils::{create_event_tags, dedup_event, Meta, parse_event}; #[derive(Debug, Serialize, Type)] pub struct RichEvent { @@ -99,10 +99,10 @@ pub async fn get_event_from( Ok(RichEvent { raw, parsed }) } else { - return Err("Cannot found this event with current relay list".into()); + Err("Cannot found this event with current relay list".into()) } } - Err(err) => return Err(err.to_string()), + Err(err) => Err(err.to_string()), } } else { // Add relay hint to relay pool diff --git a/src-tauri/src/nostr/keys.rs b/src-tauri/src/nostr/keys.rs index bbc2294a..20de3a26 100644 --- a/src-tauri/src/nostr/keys.rs +++ b/src-tauri/src/nostr/keys.rs @@ -64,20 +64,14 @@ pub async fn save_account( password: &str, state: State<'_, Nostr>, ) -> Result { - let secret_key: Result; - - if nsec.starts_with("ncryptsec") { + let secret_key = if nsec.starts_with("ncryptsec") { let encrypted_key = EncryptedSecretKey::from_bech32(nsec).unwrap(); - secret_key = match encrypted_key.to_secret_key(password) { - Ok(val) => Ok(val), - Err(err) => Err(err.to_string()), - }; + encrypted_key + .to_secret_key(password) + .map_err(|err| err.to_string()) } else { - secret_key = match SecretKey::from_bech32(nsec) { - Ok(val) => Ok(val), - Err(err) => Err(err.to_string()), - } - } + SecretKey::from_bech32(nsec).map_err(|err| err.to_string()) + }; match secret_key { Ok(val) => { @@ -280,11 +274,14 @@ pub async fn load_account( if subscription_id == notification_id { println!("new notification: {}", event.as_json()); - if let Err(_) = app.emit_to( - EventTarget::window("panel"), - "notification", - event.as_json(), - ) { + if app + .emit_to( + EventTarget::window("panel"), + "notification", + event.as_json(), + ) + .is_err() + { println!("Emit new notification failed.") } diff --git a/src-tauri/src/nostr/relay.rs b/src-tauri/src/nostr/relay.rs index ef827b8b..401e39af 100644 --- a/src-tauri/src/nostr/relay.rs +++ b/src-tauri/src/nostr/relay.rs @@ -117,12 +117,12 @@ pub fn get_bootstrap_relays(app: tauri::AppHandle) -> Result, ()> { .resolve("resources/relays.txt", BaseDirectory::Resource) .expect("Bootstrap relays not found."); - let file = std::fs::File::open(&relays_path).unwrap(); + let file = std::fs::File::open(relays_path).unwrap(); let lines = io::BufReader::new(file).lines(); let mut relays = Vec::new(); - for line in lines.flatten() { + for line in lines.map_while(Result::ok) { relays.push(line.to_string()) } @@ -139,7 +139,7 @@ pub fn save_bootstrap_relays(relays: &str, app: tauri::AppHandle) -> Result<(), let mut file = fs::OpenOptions::new() .write(true) - .open(&relays_path) + .open(relays_path) .unwrap(); match file.write_all(relays.as_bytes()) { diff --git a/src-tauri/src/nostr/utils.rs b/src-tauri/src/nostr/utils.rs index 862d1519..f62987af 100644 --- a/src-tauri/src/nostr/utils.rs +++ b/src-tauri/src/nostr/utils.rs @@ -2,8 +2,8 @@ use std::collections::HashSet; use std::str::FromStr; use linkify::LinkFinder; -use nostr_sdk::{Alphabet, Event, EventId, FromBech32, PublicKey, SingleLetterTag, Tag, TagKind}; use nostr_sdk::prelude::Nip19Event; +use nostr_sdk::{Alphabet, Event, EventId, FromBech32, PublicKey, SingleLetterTag, Tag, TagKind}; use reqwest::Client; use serde::Serialize; use specta::Type; @@ -176,7 +176,7 @@ pub fn create_event_tags(content: &str) -> Vec { .collect::>(); for mention in mentions { - let entity = mention.replace("nostr:", "").replace("@", ""); + let entity = mention.replace("nostr:", "").replace('@', ""); if !tag_set.contains(&entity) { if entity.starts_with("npub") {