Skip to content

Commit

Permalink
chore: cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
kalvkusk committed May 23, 2024
1 parent 0a4553b commit ec52107
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 82 deletions.
2 changes: 1 addition & 1 deletion src-tauri/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
94 changes: 55 additions & 39 deletions src-tauri/src/commands/user_preferences.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
accounts_list_sort_order: Option<String>,
accounts_list_sort_column: Option<String>,
accounts_list_sort_order: Option<String>,
}

// Utility function to retrieve the full path to the preferences file
fn get_preferences_path() -> Result<PathBuf, CarpeError> {
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<UserPreferences, CarpeError> {
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)))
}
84 changes: 43 additions & 41 deletions src-tauri/src/commands/wallets.rs
Original file line number Diff line number Diff line change
@@ -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},
};
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -145,48 +145,48 @@ pub fn get_all_accounts() -> Result<Vec<CarpeProfile>, CarpeError> {
/// read all accounts from profile plus notes
#[tauri::command]
pub fn get_all_accounts_with_notes() -> Result<Vec<CarpeProfile>, 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<Vec<Note>, 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<CarpeProfile>) -> 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(())
}
Expand All @@ -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(&notes)
.map_err(|_e| CarpeError::misc("Failed to serialize notes"))?;
let notes_json =
serde_json::to_string(&notes).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<CarpeProfile, CarpeError> {
Expand Down Expand Up @@ -315,8 +317,8 @@ pub async fn switch_profile(account: AccountAddress) -> Result<CarpeProfile, Car
let p = app_cfg.get_profile(Some(account.to_string()))?;
app_cfg.workspace.set_default(p.nickname.clone());
app_cfg.save_file()?;
// TODO: gross, fix upstream `app_cfg.rs` to prevent the borrow issues here

// TODO: gross, fix upstream `app_cfg.rs` to prevent the borrow issues here
let profile = app_cfg.get_profile(Some(account.to_string()))?;

// Assign account note
Expand Down
1 change: 0 additions & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ async fn main() {
commands::preferences::set_preferences_locale,
commands::preferences::get_miner_txs_cost,
commands::preferences::set_miner_txs_cost,

//////// User Preferences ////////
commands::user_preferences::get_user_preferences,
commands::user_preferences::set_accounts_list_preference,
Expand Down

0 comments on commit ec52107

Please sign in to comment.