From 8c0d03aed0bba41fe53528384ed75c3d017c2395 Mon Sep 17 00:00:00 2001 From: xy Date: Thu, 9 May 2024 19:09:55 +0900 Subject: [PATCH] feat: Refactor code to improve error handling and readability --- src-tauri/src/commands/opg.rs | 2 +- src-tauri/src/commands/window.rs | 6 +++--- src-tauri/src/main.rs | 6 ++---- src-tauri/src/nostr/keys.rs | 14 +++++++------- src-tauri/src/nostr/metadata.rs | 6 +++--- src-tauri/src/traffic_light.rs | 11 +++++------ 6 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src-tauri/src/commands/opg.rs b/src-tauri/src/commands/opg.rs index 8eaa7371..050960db 100644 --- a/src-tauri/src/commands/opg.rs +++ b/src-tauri/src/commands/opg.rs @@ -9,7 +9,7 @@ pub fn fetch_opg(url: String) -> Result { options.timeout = Duration::from_secs(10); if let Ok(data) = Webpage::from_url(&url, options) { - Ok(data.html.opengraph.into()) + Ok(data.html.opengraph) } else { Err("Get open graph failed".into()) } diff --git a/src-tauri/src/commands/window.rs b/src-tauri/src/commands/window.rs index 6167a5e2..f9dc5ebf 100644 --- a/src-tauri/src/commands/window.rs +++ b/src-tauri/src/commands/window.rs @@ -47,7 +47,7 @@ pub fn create_column( pub fn close_column(label: &str, app_handle: tauri::AppHandle) -> Result { match app_handle.get_webview(label) { Some(webview) => { - if let Ok(_) = webview.close() { + if webview.close().is_ok() { Ok(true) } else { Ok(false) @@ -90,7 +90,7 @@ pub fn reposition_column( ) -> Result<(), String> { match app_handle.get_webview(label) { Some(webview) => { - if let Ok(_) = webview.set_position(LogicalPosition::new(x, y)) { + if webview.set_position(LogicalPosition::new(x, y)).is_ok() { Ok(()) } else { Err("Reposition column failed".into()) @@ -109,7 +109,7 @@ pub fn resize_column( ) -> Result<(), String> { match app_handle.get_webview(label) { Some(webview) => { - if let Ok(_) = webview.set_size(LogicalSize::new(width, height)) { + if webview.set_size(LogicalSize::new(width, height)).is_ok() { Ok(()) } else { Err("Resize column failed".into()) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 7537439a..f9c73866 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -34,7 +34,7 @@ fn main() { // Setup app tray let handle = app.handle().clone(); - let _ = tray::create_tray(app.handle()).unwrap(); + tray::create_tray(app.handle()).unwrap(); // Create data folder if not exist let home_dir = app.path().home_dir().unwrap(); @@ -65,9 +65,7 @@ fn main() { client.connect().await; // Update global state - handle.manage(Nostr { - client: client.into(), - }) + handle.manage(Nostr { client }) }); Ok(()) diff --git a/src-tauri/src/nostr/keys.rs b/src-tauri/src/nostr/keys.rs index 7d460542..65981418 100644 --- a/src-tauri/src/nostr/keys.rs +++ b/src-tauri/src/nostr/keys.rs @@ -22,7 +22,7 @@ pub fn create_keys() -> Result { nsec: secret_key.to_bech32().expect("nsec failed"), }; - Ok(result.into()) + Ok(result) } #[tauri::command] @@ -70,7 +70,7 @@ pub async fn save_key( Ok(npub) } - Err(msg) => Err(msg.into()), + Err(msg) => Err(msg), } } @@ -111,7 +111,7 @@ pub async fn nostr_connect( pub async fn verify_signer(state: State<'_, Nostr>) -> Result { let client = &state.client; - if let Ok(_) = client.signer().await { + if (client.signer().await).is_ok() { Ok(true) } else { Ok(false) @@ -188,16 +188,16 @@ pub async fn load_selected_account(npub: &str, state: State<'_, Nostr>) -> Resul { Ok(events) => { if let Some(event) = events.first() { - let relay_list = nip65::extract_relay_list(&event); + let relay_list = nip65::extract_relay_list(event); for item in relay_list.into_iter() { - println!("connecting to relay: {}", item.0.to_string()); + println!("connecting to relay: {}", item.0); // Add relay to pool let _ = client .add_relay(item.0.to_string()) .await .unwrap_or_default(); // Connect relay - let _ = client + client .connect_relay(item.0.to_string()) .await .unwrap_or_default(); @@ -242,7 +242,7 @@ pub async fn verify_nip05(key: &str, nip05: &str) -> Result { let public_key = PublicKey::from_str(key).unwrap(); let status = nip05::verify(public_key, nip05, None).await; - if let Ok(_) = status { + if status.is_ok() { Ok(true) } else { Ok(false) diff --git a/src-tauri/src/nostr/metadata.rs b/src-tauri/src/nostr/metadata.rs index 94bc7168..a8a38f04 100644 --- a/src-tauri/src/nostr/metadata.rs +++ b/src-tauri/src/nostr/metadata.rs @@ -353,7 +353,7 @@ pub async fn get_nstore(key: &str, state: State<'_, Nostr>) -> Result) -> Result { let client = &state.client; - if let Ok(nwc_uri) = NostrWalletConnectURI::from_str(&uri) { + if let Ok(nwc_uri) = NostrWalletConnectURI::from_str(uri) { if let Ok(nwc) = NWC::new(nwc_uri).await { let keyring = Entry::new("Lume Secret Storage", "NWC").unwrap(); let _ = keyring.set_password(uri); @@ -431,7 +431,7 @@ pub async fn zap_profile( if let Some(recipient) = public_key { let details = ZapDetails::new(ZapType::Public).message(message); - if let Ok(_) = client.zap(recipient, amount, Some(details)).await { + if (client.zap(recipient, amount, Some(details)).await).is_ok() { Ok(true) } else { Err("Zap profile failed".into()) @@ -464,7 +464,7 @@ pub async fn zap_event( if let Some(recipient) = event_id { let details = ZapDetails::new(ZapType::Public).message(message); - if let Ok(_) = client.zap(recipient, amount, Some(details)).await { + if (client.zap(recipient, amount, Some(details)).await).is_ok() { Ok(true) } else { Err("Zap event failed".into()) diff --git a/src-tauri/src/traffic_light.rs b/src-tauri/src/traffic_light.rs index 9bad8195..30d40d02 100644 --- a/src-tauri/src/traffic_light.rs +++ b/src-tauri/src/traffic_light.rs @@ -19,7 +19,6 @@ pub fn init() -> TauriPlugin { .on_window_ready(|window| { #[cfg(target_os = "macos")] setup_traffic_light_positioner(window); - return; }) .build() } @@ -107,7 +106,7 @@ pub fn setup_traffic_light_positioner(window: Window) { } extern "C" fn on_window_did_resize(this: &Object, _cmd: Sel, notification: id) { unsafe { - with_window_state(&*this, |state: &mut WindowState| { + with_window_state(this, |state: &mut WindowState| { let id = state .window .ns_window() @@ -204,7 +203,7 @@ pub fn setup_traffic_light_positioner(window: Window) { notification: id, ) { unsafe { - with_window_state(&*this, |state: &mut WindowState| { + with_window_state(this, |state: &mut WindowState| { state .window .emit("did-enter-fullscreen", ()) @@ -221,7 +220,7 @@ pub fn setup_traffic_light_positioner(window: Window) { notification: id, ) { unsafe { - with_window_state(&*this, |state: &mut WindowState| { + with_window_state(this, |state: &mut WindowState| { state .window .emit("will-enter-fullscreen", ()) @@ -238,7 +237,7 @@ pub fn setup_traffic_light_positioner(window: Window) { notification: id, ) { unsafe { - with_window_state(&*this, |state: &mut WindowState| { + with_window_state(this, |state: &mut WindowState| { state .window .emit("did-exit-fullscreen", ()) @@ -262,7 +261,7 @@ pub fn setup_traffic_light_positioner(window: Window) { notification: id, ) { unsafe { - with_window_state(&*this, |state: &mut WindowState| { + with_window_state(this, |state: &mut WindowState| { state .window .emit("will-exit-fullscreen", ())