feat: Refactor code to improve error handling and readability

This commit is contained in:
xy 2024-05-09 19:09:55 +09:00
parent c8e1b8b8bd
commit 8c0d03aed0
6 changed files with 21 additions and 24 deletions

View File

@ -9,7 +9,7 @@ pub fn fetch_opg(url: String) -> Result<Opengraph, String> {
options.timeout = Duration::from_secs(10); options.timeout = Duration::from_secs(10);
if let Ok(data) = Webpage::from_url(&url, options) { if let Ok(data) = Webpage::from_url(&url, options) {
Ok(data.html.opengraph.into()) Ok(data.html.opengraph)
} else { } else {
Err("Get open graph failed".into()) Err("Get open graph failed".into())
} }

View File

@ -47,7 +47,7 @@ pub fn create_column(
pub fn close_column(label: &str, app_handle: tauri::AppHandle) -> Result<bool, ()> { pub fn close_column(label: &str, app_handle: tauri::AppHandle) -> Result<bool, ()> {
match app_handle.get_webview(label) { match app_handle.get_webview(label) {
Some(webview) => { Some(webview) => {
if let Ok(_) = webview.close() { if webview.close().is_ok() {
Ok(true) Ok(true)
} else { } else {
Ok(false) Ok(false)
@ -90,7 +90,7 @@ pub fn reposition_column(
) -> Result<(), String> { ) -> Result<(), String> {
match app_handle.get_webview(label) { match app_handle.get_webview(label) {
Some(webview) => { Some(webview) => {
if let Ok(_) = webview.set_position(LogicalPosition::new(x, y)) { if webview.set_position(LogicalPosition::new(x, y)).is_ok() {
Ok(()) Ok(())
} else { } else {
Err("Reposition column failed".into()) Err("Reposition column failed".into())
@ -109,7 +109,7 @@ pub fn resize_column(
) -> Result<(), String> { ) -> Result<(), String> {
match app_handle.get_webview(label) { match app_handle.get_webview(label) {
Some(webview) => { Some(webview) => {
if let Ok(_) = webview.set_size(LogicalSize::new(width, height)) { if webview.set_size(LogicalSize::new(width, height)).is_ok() {
Ok(()) Ok(())
} else { } else {
Err("Resize column failed".into()) Err("Resize column failed".into())

View File

@ -34,7 +34,7 @@ fn main() {
// Setup app tray // Setup app tray
let handle = app.handle().clone(); let handle = app.handle().clone();
let _ = tray::create_tray(app.handle()).unwrap(); tray::create_tray(app.handle()).unwrap();
// Create data folder if not exist // Create data folder if not exist
let home_dir = app.path().home_dir().unwrap(); let home_dir = app.path().home_dir().unwrap();
@ -65,9 +65,7 @@ fn main() {
client.connect().await; client.connect().await;
// Update global state // Update global state
handle.manage(Nostr { handle.manage(Nostr { client })
client: client.into(),
})
}); });
Ok(()) Ok(())

View File

@ -22,7 +22,7 @@ pub fn create_keys() -> Result<CreateKeysResponse, ()> {
nsec: secret_key.to_bech32().expect("nsec failed"), nsec: secret_key.to_bech32().expect("nsec failed"),
}; };
Ok(result.into()) Ok(result)
} }
#[tauri::command] #[tauri::command]
@ -70,7 +70,7 @@ pub async fn save_key(
Ok(npub) 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<bool, ()> { pub async fn verify_signer(state: State<'_, Nostr>) -> Result<bool, ()> {
let client = &state.client; let client = &state.client;
if let Ok(_) = client.signer().await { if (client.signer().await).is_ok() {
Ok(true) Ok(true)
} else { } else {
Ok(false) Ok(false)
@ -188,16 +188,16 @@ pub async fn load_selected_account(npub: &str, state: State<'_, Nostr>) -> Resul
{ {
Ok(events) => { Ok(events) => {
if let Some(event) = events.first() { 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() { 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 // Add relay to pool
let _ = client let _ = client
.add_relay(item.0.to_string()) .add_relay(item.0.to_string())
.await .await
.unwrap_or_default(); .unwrap_or_default();
// Connect relay // Connect relay
let _ = client client
.connect_relay(item.0.to_string()) .connect_relay(item.0.to_string())
.await .await
.unwrap_or_default(); .unwrap_or_default();
@ -242,7 +242,7 @@ pub async fn verify_nip05(key: &str, nip05: &str) -> Result<bool, ()> {
let public_key = PublicKey::from_str(key).unwrap(); let public_key = PublicKey::from_str(key).unwrap();
let status = nip05::verify(public_key, nip05, None).await; let status = nip05::verify(public_key, nip05, None).await;
if let Ok(_) = status { if status.is_ok() {
Ok(true) Ok(true)
} else { } else {
Ok(false) Ok(false)

View File

@ -353,7 +353,7 @@ pub async fn get_nstore(key: &str, state: State<'_, Nostr>) -> Result<String, St
pub async fn set_nwc(uri: &str, state: State<'_, Nostr>) -> Result<bool, String> { pub async fn set_nwc(uri: &str, state: State<'_, Nostr>) -> Result<bool, String> {
let client = &state.client; 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 { if let Ok(nwc) = NWC::new(nwc_uri).await {
let keyring = Entry::new("Lume Secret Storage", "NWC").unwrap(); let keyring = Entry::new("Lume Secret Storage", "NWC").unwrap();
let _ = keyring.set_password(uri); let _ = keyring.set_password(uri);
@ -431,7 +431,7 @@ pub async fn zap_profile(
if let Some(recipient) = public_key { if let Some(recipient) = public_key {
let details = ZapDetails::new(ZapType::Public).message(message); 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) Ok(true)
} else { } else {
Err("Zap profile failed".into()) Err("Zap profile failed".into())
@ -464,7 +464,7 @@ pub async fn zap_event(
if let Some(recipient) = event_id { if let Some(recipient) = event_id {
let details = ZapDetails::new(ZapType::Public).message(message); 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) Ok(true)
} else { } else {
Err("Zap event failed".into()) Err("Zap event failed".into())

View File

@ -19,7 +19,6 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
.on_window_ready(|window| { .on_window_ready(|window| {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
setup_traffic_light_positioner(window); setup_traffic_light_positioner(window);
return;
}) })
.build() .build()
} }
@ -107,7 +106,7 @@ pub fn setup_traffic_light_positioner<R: Runtime>(window: Window<R>) {
} }
extern "C" fn on_window_did_resize<R: Runtime>(this: &Object, _cmd: Sel, notification: id) { extern "C" fn on_window_did_resize<R: Runtime>(this: &Object, _cmd: Sel, notification: id) {
unsafe { unsafe {
with_window_state(&*this, |state: &mut WindowState<R>| { with_window_state(this, |state: &mut WindowState<R>| {
let id = state let id = state
.window .window
.ns_window() .ns_window()
@ -204,7 +203,7 @@ pub fn setup_traffic_light_positioner<R: Runtime>(window: Window<R>) {
notification: id, notification: id,
) { ) {
unsafe { unsafe {
with_window_state(&*this, |state: &mut WindowState<R>| { with_window_state(this, |state: &mut WindowState<R>| {
state state
.window .window
.emit("did-enter-fullscreen", ()) .emit("did-enter-fullscreen", ())
@ -221,7 +220,7 @@ pub fn setup_traffic_light_positioner<R: Runtime>(window: Window<R>) {
notification: id, notification: id,
) { ) {
unsafe { unsafe {
with_window_state(&*this, |state: &mut WindowState<R>| { with_window_state(this, |state: &mut WindowState<R>| {
state state
.window .window
.emit("will-enter-fullscreen", ()) .emit("will-enter-fullscreen", ())
@ -238,7 +237,7 @@ pub fn setup_traffic_light_positioner<R: Runtime>(window: Window<R>) {
notification: id, notification: id,
) { ) {
unsafe { unsafe {
with_window_state(&*this, |state: &mut WindowState<R>| { with_window_state(this, |state: &mut WindowState<R>| {
state state
.window .window
.emit("did-exit-fullscreen", ()) .emit("did-exit-fullscreen", ())
@ -262,7 +261,7 @@ pub fn setup_traffic_light_positioner<R: Runtime>(window: Window<R>) {
notification: id, notification: id,
) { ) {
unsafe { unsafe {
with_window_state(&*this, |state: &mut WindowState<R>| { with_window_state(this, |state: &mut WindowState<R>| {
state state
.window .window
.emit("will-exit-fullscreen", ()) .emit("will-exit-fullscreen", ())