From 463bd7e4358bb0fe876021098205fe666ebaa938 Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Sat, 20 Apr 2024 17:03:39 +0200 Subject: [PATCH] Improved error handling & added delete secret as well --- runtime/src/main.rs | 71 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/runtime/src/main.rs b/runtime/src/main.rs index 5c869eb..9f43ba1 100644 --- a/runtime/src/main.rs +++ b/runtime/src/main.rs @@ -2,22 +2,83 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] use keyring::Entry; +use serde::Serialize; fn main() { tauri::Builder::default() - .invoke_handler(tauri::generate_handler![store_secret, get_secret]) + .invoke_handler(tauri::generate_handler![store_secret, get_secret, delete_secret]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } #[tauri::command] -fn store_secret(destination: String, user_name: String, secret: String) { +fn store_secret(destination: String, user_name: String, secret: String) -> StoreSecretResponse { let entry = Entry::new(&format!("mindwork-ai-studio::{}", destination), user_name.as_str()).unwrap(); - entry.set_password(secret.as_str()).unwrap(); + let result = entry.set_password(secret.as_str()); + match result { + Ok(_) => StoreSecretResponse { + success: true, + issue: String::from(""), + }, + + Err(e) => StoreSecretResponse { + success: false, + issue: e.to_string(), + }, + } +} + +#[derive(Serialize)] +struct StoreSecretResponse { + success: bool, + issue: String, } #[tauri::command] -fn get_secret(destination: String, user_name: String) -> String { +fn get_secret(destination: String, user_name: String) -> RequestedSecret { let entry = Entry::new(&format!("mindwork-ai-studio::{}", destination), user_name.as_str()).unwrap(); - entry.get_password().unwrap() + let secret = entry.get_password(); + match secret { + Ok(s) => RequestedSecret { + success: true, + secret: s, + issue: String::from(""), + }, + + Err(e) => RequestedSecret { + success: false, + secret: String::from(""), + issue: e.to_string(), + }, + } +} + +#[derive(Serialize)] +struct RequestedSecret { + success: bool, + secret: String, + issue: String, +} + +#[tauri::command] +fn delete_secret(destination: String, user_name: String) -> DeleteSecretResponse { + let entry = Entry::new(&format!("mindwork-ai-studio::{}", destination), user_name.as_str()).unwrap(); + let result = entry.delete_password(); + match result { + Ok(_) => DeleteSecretResponse { + success: true, + issue: String::from(""), + }, + + Err(e) => DeleteSecretResponse { + success: false, + issue: e.to_string(), + }, + } +} + +#[derive(Serialize)] +struct DeleteSecretResponse { + success: bool, + issue: String, } \ No newline at end of file