diff --git a/src-tauri/src/commands/mod.rs b/src-tauri/src/commands/mod.rs index 8d1be68e..4655c52a 100644 --- a/src-tauri/src/commands/mod.rs +++ b/src-tauri/src/commands/mod.rs @@ -4,8 +4,8 @@ pub(crate) mod networks; pub(crate) mod preferences; pub(crate) mod query; pub(crate) mod tx; +pub(crate) mod user_preferences; pub(crate) mod wallets; pub(crate) mod web_logs; -pub(crate) mod user_preferences; //pub use app_version::*; diff --git a/src-tauri/src/commands/user_preferences.rs b/src-tauri/src/commands/user_preferences.rs index 1002e39a..eb35f2e9 100644 --- a/src-tauri/src/commands/user_preferences.rs +++ b/src-tauri/src/commands/user_preferences.rs @@ -1,61 +1,77 @@ use crate::carpe_error::CarpeError; +use crate::configs::default_config_path; use serde::{Deserialize, Serialize}; use std::fs::{File, OpenOptions}; use std::io::{Read, Write}; -use std::path::{PathBuf}; -use crate::configs::default_config_path; +use std::path::PathBuf; #[derive(Serialize, Deserialize)] pub struct UserPreferences { - accounts_list_sort_column: Option, - accounts_list_sort_order: Option, + accounts_list_sort_column: Option, + accounts_list_sort_order: Option, } // Utility function to retrieve the full path to the preferences file fn get_preferences_path() -> Result { - let app_dir_path = default_config_path(); // Assuming this returns a PathBuf or Path + let app_dir_path = default_config_path(); // Assuming this returns a PathBuf or Path - // Check if the path exists, if not, return an error - if !app_dir_path.exists() { - return Err(CarpeError::misc("App directory not found")); - } + // Check if the path exists, if not, return an error + if !app_dir_path.exists() { + return Err(CarpeError::misc("App directory not found")); + } - Ok(app_dir_path.join("user_preferences.json")) + Ok(app_dir_path.join("user_preferences.json")) } #[tauri::command(async)] pub async fn get_user_preferences() -> Result { - let file_path = get_preferences_path()?; - match File::open(&file_path) { - Ok(mut file) => { - let mut contents = String::new(); - if let Err(e) = file.read_to_string(&mut contents) { - return Err(CarpeError::misc(&format!("Failed to read from preferences file: {}", e))); - } - serde_json::from_str(&contents).map_err(|e| CarpeError::misc(&format!("Failed to parse preferences: {}", e))) - }, - Err(e) => Err(CarpeError::misc(&format!("Failed to open preferences file: {}", e))), + let file_path = get_preferences_path()?; + match File::open(&file_path) { + Ok(mut file) => { + let mut contents = String::new(); + if let Err(e) = file.read_to_string(&mut contents) { + return Err(CarpeError::misc(&format!( + "Failed to read from preferences file: {}", + e + ))); + } + serde_json::from_str(&contents) + .map_err(|e| CarpeError::misc(&format!("Failed to parse preferences: {}", e))) } + Err(e) => Err(CarpeError::misc(&format!( + "Failed to open preferences file: {}", + e + ))), + } } #[tauri::command(async)] -pub async fn set_accounts_list_preference(sort_column: String, sort_order: String) -> Result<(), CarpeError> { - let file_path = get_preferences_path()?; - let mut file = OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(&file_path) - .map_err(|e| CarpeError::misc(&format!("Failed to open preferences file for writing: {}", e)))?; - - let preferences = UserPreferences { - accounts_list_sort_column: Some(sort_column), - accounts_list_sort_order: Some(sort_order), - }; - - let serialized_data = serde_json::to_string_pretty(&preferences) - .map_err(|e| CarpeError::misc(&format!("Failed to serialize preferences: {}", e)))?; - - file.write_all(serialized_data.as_bytes()) - .map_err(|e| CarpeError::misc(&format!("Failed to write preferences file: {}", e))) +pub async fn set_accounts_list_preference( + sort_column: String, + sort_order: String, +) -> Result<(), CarpeError> { + let file_path = get_preferences_path()?; + let mut file = OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(&file_path) + .map_err(|e| { + CarpeError::misc(&format!( + "Failed to open preferences file for writing: {}", + e + )) + })?; + + let preferences = UserPreferences { + accounts_list_sort_column: Some(sort_column), + accounts_list_sort_order: Some(sort_order), + }; + + let serialized_data = serde_json::to_string_pretty(&preferences) + .map_err(|e| CarpeError::misc(&format!("Failed to serialize preferences: {}", e)))?; + + file + .write_all(serialized_data.as_bytes()) + .map_err(|e| CarpeError::misc(&format!("Failed to write preferences file: {}", e))) } diff --git a/src-tauri/src/commands/wallets.rs b/src-tauri/src/commands/wallets.rs index 629933d3..7b70cb86 100644 --- a/src-tauri/src/commands/wallets.rs +++ b/src-tauri/src/commands/wallets.rs @@ -1,7 +1,7 @@ use crate::{ carpe_error::CarpeError, commands::query, - configs::{self, default_legacy_account_path, get_cfg, get_client, default_config_path}, + configs::{self, default_config_path, default_legacy_account_path, get_cfg, get_client}, configs_profile, key_manager::{self, get_private_key, inject_private_key_to_cfg}, }; @@ -16,11 +16,11 @@ use libra_types::{ }; use libra_wallet::account_keys::{self, KeyChain}; use serde::{Deserialize, Serialize}; -use std::fs::{self, File}; -use std::io::{Write, prelude::*}; -use std::fs::OpenOptions; -use std::path::{PathBuf, Path}; use serde_json; +use std::fs::OpenOptions; +use std::fs::{self, File}; +use std::io::{prelude::*, Write}; +use std::path::{Path, PathBuf}; #[derive(serde::Deserialize, serde::Serialize, Debug)] pub struct NewKeygen { @@ -130,8 +130,8 @@ pub async fn init_from_private_key( #[derive(Serialize, Deserialize)] struct Note { - account: String, - note: String, + account: String, + note: String, } /// read all accounts from profile @@ -145,48 +145,48 @@ pub fn get_all_accounts() -> Result, CarpeError> { /// read all accounts from profile plus notes #[tauri::command] pub fn get_all_accounts_with_notes() -> Result, CarpeError> { - let mut accounts = get_all_accounts()?; - let _ = assign_notes_to_accounts(&mut accounts); - Ok(accounts) + let mut accounts = get_all_accounts()?; + let _ = assign_notes_to_accounts(&mut accounts); + Ok(accounts) } fn notes_file_path() -> PathBuf { - let app_dir_path = default_config_path(); // Assuming this returns a PathBuf or Path + let app_dir_path = default_config_path(); // Assuming this returns a PathBuf or Path return app_dir_path.join("account_notes.json"); } - fn read_notes() -> Result, CarpeError> { let file_path = notes_file_path(); // Check if the file exists before attempting to open it if !Path::new(&file_path).exists() { - return Ok(vec![]); // Return an empty vector if the file does not exist + return Ok(vec![]); // Return an empty vector if the file does not exist } let mut file = match File::open(&file_path) { - Ok(f) => f, - Err(_e) => return Err(CarpeError::misc("Failed to open notes file")), + Ok(f) => f, + Err(_e) => return Err(CarpeError::misc("Failed to open notes file")), }; let mut contents = String::new(); if let Err(_e) = file.read_to_string(&mut contents) { - return Err(CarpeError::misc("Failed to read from notes file")); + return Err(CarpeError::misc("Failed to read from notes file")); } match serde_json::from_str(&contents) { - Ok(notes) => Ok(notes), - Err(_e) => Err(CarpeError::misc("Failed to parse notes JSON")), + Ok(notes) => Ok(notes), + Err(_e) => Err(CarpeError::misc("Failed to parse notes JSON")), } } fn assign_notes_to_accounts(accounts: &mut Vec) -> Result<(), CarpeError> { let notes = read_notes()?; for account in accounts.iter_mut() { - let note_option = notes.iter() - .find(|note| note.account == account.account.to_string().to_uppercase()) - .map(|note| note.note.clone()); - account.note = note_option; + let note_option = notes + .iter() + .find(|note| note.account == account.account.to_string().to_uppercase()) + .map(|note| note.note.clone()); + account.note = note_option; } Ok(()) } @@ -199,36 +199,38 @@ pub fn associate_note_with_account(account: String, note: String) -> Result<(), // Check if the account already exists and update the note if it does let mut found = false; for account_note in notes.iter_mut() { - if account_note.account == address { - account_note.note = note.clone(); - found = true; - break; - } + if account_note.account == address { + account_note.note = note.clone(); + found = true; + break; + } } // If the account does not exist, add a new note if !found { - notes.push(Note { account: address, note }); + notes.push(Note { + account: address, + note, + }); } - let notes_json = serde_json::to_string(¬es) - .map_err(|_e| CarpeError::misc("Failed to serialize notes"))?; + let notes_json = + serde_json::to_string(¬es).map_err(|_e| CarpeError::misc("Failed to serialize notes"))?; let mut file = OpenOptions::new() - .write(true) - .truncate(true) - .create(true) - .open(&file_path) - .map_err(|_e| CarpeError::misc("Failed to open file for writing"))?; + .write(true) + .truncate(true) + .create(true) + .open(&file_path) + .map_err(|_e| CarpeError::misc("Failed to open file for writing"))?; - file.write_all(notes_json.as_bytes()) - .map_err(|_e| CarpeError::misc("Failed to write to file"))?; + file + .write_all(notes_json.as_bytes()) + .map_err(|_e| CarpeError::misc("Failed to write to file"))?; Ok(()) } - - /// read all accounts from profile #[tauri::command(async)] pub fn get_default_profile() -> Result { @@ -315,8 +317,8 @@ pub async fn switch_profile(account: AccountAddress) -> Result