Skip to content

Commit

Permalink
refactor(back): make utils more error safe
Browse files Browse the repository at this point in the history
  • Loading branch information
kareemmahlees committed Dec 27, 2023
1 parent 28635f5 commit 66e4d0f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 59 deletions.
48 changes: 21 additions & 27 deletions src-tauri/src/connection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{
drivers::{mysql, postgres, sqlite},
utils::{read_from_connections_file, write_into_connections_file, Drivers},
utils::{
get_connections_file_path, read_from_connections_file, write_into_connections_file, Drivers,
},
DbInstance,
};
use sqlx::{AnyConnection, Connection};
Expand Down Expand Up @@ -28,12 +30,8 @@ pub fn create_connection_record(
conn_name: String,
driver: Drivers,
) -> Result<(), String> {
write_into_connections_file(
app.path_resolver().app_config_dir(),
driver,
conn_string,
conn_name,
);
let mut connections_file_path = get_connections_file_path(&app)?;
write_into_connections_file(&mut connections_file_path, driver, conn_string, conn_name)?;
Ok(())
}

Expand All @@ -54,36 +52,32 @@ pub async fn establish_connection(

#[tauri::command]
pub fn connections_exist(app: tauri::AppHandle) -> Result<bool, String> {
let (_, connections) = read_from_connections_file(app.path_resolver().app_config_dir());
match connections {
Ok(connections) => {
if !connections.as_object().unwrap().is_empty() {
Ok(true)
} else {
Ok(false)
}
}
Err(err) => Err(err.to_string()),
let connections_file_path = get_connections_file_path(&app)?;
let connections = read_from_connections_file(&connections_file_path)?;
if !connections.as_object().unwrap().is_empty() {
Ok(true)
} else {
Ok(false)
}
}

#[tauri::command]
pub fn get_connections(app: tauri::AppHandle) -> Result<serde_json::Value, String> {
let (_, connections) = read_from_connections_file(app.path_resolver().app_config_dir());
match connections {
Ok(conns) => Ok(conns),
Err(err) => Err(err.to_string()),
}
let connections_file_path = get_connections_file_path(&app)?;
let connections = read_from_connections_file(&connections_file_path)?;
Ok(connections)
}

#[tauri::command]
pub fn get_connection_details(
app: tauri::AppHandle,
conn_id: String,
) -> Result<serde_json::Value, String> {
let (_, connections) = read_from_connections_file(app.path_resolver().app_config_dir());
match connections {
Ok(conns) => Ok(conns.get(conn_id).unwrap().to_owned()),
Err(err) => Err(err.to_string()),
}
let connections_file_path = get_connections_file_path(&app)?;
let connections = read_from_connections_file(&connections_file_path)?;
let connection_details = connections
.get(conn_id)
.ok_or("Couldn't find the specified connection".to_string())?
.to_owned();
Ok(connection_details)
}
70 changes: 38 additions & 32 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::io::{BufReader, BufWriter, Write};
use std::path::PathBuf;
use std::{
collections::HashMap,
fs::{create_dir_all, File, OpenOptions},
};
use std::{collections::HashMap, fs::OpenOptions};
use uuid::Uuid;

#[derive(Serialize, Deserialize, Debug)]
Expand All @@ -25,62 +22,71 @@ pub struct ConnConfig {
conn_name: String,
}

pub fn get_connections_file_path(app: &tauri::AppHandle) -> Result<PathBuf, String> {
let mut config_dir = app
.path_resolver()
.app_config_dir()
.ok_or("Couldn't read config dir path".to_string())?;
config_dir.push("connections.json");
Ok(config_dir)
}

pub fn write_into_connections_file(
config_dir: Option<PathBuf>,
connections_file_path: &mut PathBuf,
driver: Drivers,
conn_string: String,
conn_name: String,
) {
let (config_file_path, content) = read_from_connections_file(config_dir);
) -> Result<(), String> {
let mut contents = read_from_connections_file(connections_file_path)
.map_err(|_| "Couldn't read contents of connections file")?;

let connection = ConnConfig {
driver,
conn_string,
conn_name,
};

match content.unwrap().as_object_mut() {
match contents.as_object_mut() {
Some(v) => {
let file = OpenOptions::new()
.write(true)
.open(config_file_path)
.unwrap();
.open(connections_file_path)
.map_err(|e| e.to_string())?;
let mut writer = BufWriter::new(file);

let id = Uuid::new_v4().to_string();

v.insert(id, serde_json::to_value(connection).unwrap());
serde_json::to_writer(&mut writer, &v).unwrap();
serde_json::to_writer(&mut writer, &v)
.map_err(|_| "Failed to write connection record".to_string())?;
writer.flush().unwrap();
Ok(())
}
None => todo!(),
};
None => Err("Invalid JSON file format".to_string()),
}
}

pub fn read_from_connections_file(
config_dir: Option<PathBuf>,
) -> (PathBuf, serde_json::Result<serde_json::Value>) {
match config_dir {
Some(mut path) => {
if !path.exists() {
create_dir_all(&path).unwrap();
}
path.push("connections.json");
let mut file: File;
if !path.exists() {
file = File::create(&path).unwrap();
write!(file, "{{}}").unwrap()
}
file = OpenOptions::new().read(true).open(&path).unwrap();
let reader = BufReader::new(file);
connections_file_path: &PathBuf,
) -> Result<serde_json::Value, String> {
let mut file = OpenOptions::new()
.read(true)
.create(true)
.write(true)
.open(connections_file_path)
.map_err(|e| e.to_string())?;

let content: serde_json::Result<serde_json::Value> = serde_json::from_reader(reader);
(path, content)
}
None => todo!(),
if file.metadata().unwrap().len() == 0 {
write!(file, "{{}}")
.map_err(|_| "Couldn't write initial content into connections file".to_string())?;
}

let reader = BufReader::new(file);
let content: serde_json::Result<serde_json::Value> = serde_json::from_reader(reader);
content.map_err(|e| e.to_string())
}

/// shared between drivers::$::table.rs
pub fn create_column_definition_map(
data_type: JsonValue,
is_nullable: JsonValue,
Expand Down

0 comments on commit 66e4d0f

Please sign in to comment.