Merge pull request #186 from kasugamirai/chore

feat: Refactor code to improve error handling and readability
This commit is contained in:
雨宮蓮 2024-05-10 07:13:32 +07:00 committed by GitHub
commit b46a5cf68f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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);
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())
}

View File

@ -47,7 +47,7 @@ pub fn create_column(
pub fn close_column(label: &str, app_handle: tauri::AppHandle) -> Result<bool, ()> {
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())

View File

@ -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(())

View File

@ -22,7 +22,7 @@ pub fn create_keys() -> Result<CreateKeysResponse, ()> {
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<bool, ()> {
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<bool, ()> {
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)

View File

@ -392,7 +392,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> {
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);
@ -470,7 +470,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())
@ -503,7 +503,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())

View File

@ -19,7 +19,6 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
.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<R: Runtime>(window: Window<R>) {
}
extern "C" fn on_window_did_resize<R: Runtime>(this: &Object, _cmd: Sel, notification: id) {
unsafe {
with_window_state(&*this, |state: &mut WindowState<R>| {
with_window_state(this, |state: &mut WindowState<R>| {
let id = state
.window
.ns_window()
@ -204,7 +203,7 @@ pub fn setup_traffic_light_positioner<R: Runtime>(window: Window<R>) {
notification: id,
) {
unsafe {
with_window_state(&*this, |state: &mut WindowState<R>| {
with_window_state(this, |state: &mut WindowState<R>| {
state
.window
.emit("did-enter-fullscreen", ())
@ -221,7 +220,7 @@ pub fn setup_traffic_light_positioner<R: Runtime>(window: Window<R>) {
notification: id,
) {
unsafe {
with_window_state(&*this, |state: &mut WindowState<R>| {
with_window_state(this, |state: &mut WindowState<R>| {
state
.window
.emit("will-enter-fullscreen", ())
@ -238,7 +237,7 @@ pub fn setup_traffic_light_positioner<R: Runtime>(window: Window<R>) {
notification: id,
) {
unsafe {
with_window_state(&*this, |state: &mut WindowState<R>| {
with_window_state(this, |state: &mut WindowState<R>| {
state
.window
.emit("did-exit-fullscreen", ())
@ -262,7 +261,7 @@ pub fn setup_traffic_light_positioner<R: Runtime>(window: Window<R>) {
notification: id,
) {
unsafe {
with_window_state(&*this, |state: &mut WindowState<R>| {
with_window_state(this, |state: &mut WindowState<R>| {
state
.window
.emit("will-exit-fullscreen", ())