code restructure

This commit is contained in:
djkato 2023-04-16 20:51:50 +02:00
parent f028064630
commit eb68d42748
9 changed files with 205 additions and 169 deletions

7
Cargo.lock generated
View file

@ -719,6 +719,7 @@ dependencies = [
"egui",
"egui_extras",
"grid",
"if_chain",
"regex",
"rfd",
"sqlx",
@ -1459,6 +1460,12 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "if_chain"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb56e1aa765b4b4f3aadfab769793b7087bb03a4ea4920644a6d238e2df5b9ed"
[[package]]
name = "indexmap"
version = "1.9.2"

View file

@ -21,3 +21,4 @@ rfd = "0.11.1"
grid = "0.9.0"
regex = "1.7.1"
chrono = "0.4.24"
if_chain = "1.0.2"

View file

@ -19,7 +19,7 @@ volumes:
# To connect to mysql, do:
# sudo docker exec -it csql-Opencart_dummy_DB-1 /bin/bash
# mysql -p
# mysql -pmauFJcuf5dhRMQrjj
# [password]
# use quotes;
# select * from oc_product;

View file

@ -1,6 +1,7 @@
use super::csv_handler::{DataEntry, ImportedData};
use super::database_handler::{DBLoginData, QueryResult, Table, Tables};
use super::parser::parse;
use if_chain::if_chain;
use sqlx::MySqlConnection;
use std::sync::Arc;
use tokio::sync::mpsc::{Receiver, Sender};
@ -183,24 +184,16 @@ pub fn try_match_headers_to_fields(
if !csv_data.are_headers || csv_data.data.is_empty() {
return;
}
let final_table_index: usize;
if let Some(i) = table_index {
final_table_index = i;
} else if let Some(cw_table_i) = tables.current_working_table {
final_table_index = cw_table_i;
let new_i: usize;
if let Some(table_index) = table_index.or(tables.current_working_table) {
new_i = table_index;
} else {
return;
}
let table_index = final_table_index;
let db_fields = tables
.tables
.get_mut(table_index)
.unwrap()
.fields
.as_mut()
.unwrap();
let table_index = new_i;
println!("Trying to automatch table \"{}\"...", table_index);
let mut has_matched_some = false;
if let Some(db_fields) = tables.tables.get_mut(table_index).unwrap().fields.as_mut() {
for field in db_fields {
for (i, header) in csv_data.data.iter_row(0).into_iter().enumerate() {
if &field.description.field == &header.data {
@ -209,31 +202,30 @@ pub fn try_match_headers_to_fields(
}
}
}
if has_matched_some == false
|| tables.current_working_table.is_none()
|| tables.tables.is_empty()
{
}
if !has_matched_some {
return;
}
println!("Autommapped some!");
/* If some got automapped, try to parse them all */
if let Some(table_i) = tables.current_working_table {
if let Some(table) = tables.tables.get_mut(table_i) {
if let Some(fields) = table.fields.as_mut() {
for (col_index, field) in fields.iter().enumerate() {
if_chain! {
if let Some(table) = tables.tables.get_mut(table_index);
if let Some(fields) = table.fields.as_mut();
then {
for (field_index, field) in fields.iter().enumerate() {
if let Some(col_index) = field.mapped_to_col {
println!(
" > automapping field \"{}\" to col \"{:?}\"",
&field.description.field, field.mapped_to_col
);
if let Some(col_index) = field.mapped_to_col {
for cell in csv_data.data.iter_mut() {
for cell in csv_data.data.iter_col_mut(col_index) {
cell.curr_field_description = Some(field.description.clone());
parse(cell);
}
}
if is_whole_col_parsed(csv_data, col_index) {
println!("col \"{}\" is parsed whole!", &col_index);
csv_data.parsed_cols.push(col_index);
if is_whole_col_parsed(csv_data, field_index) {
println!("col \"{}\" is parsed whole!", &field_index);
csv_data.parsed_cols.push(field_index);
}
if is_whole_table_parsed(&csv_data) {
csv_data.is_parsed = true;
@ -242,7 +234,6 @@ pub fn try_match_headers_to_fields(
}
}
}
}
#[derive(Debug)]
pub enum Communication {

View file

@ -158,7 +158,7 @@ fn parse_decimal(cell: &mut DataEntry, args: Option<String>) {
if let Some(num_args) = process_args(args) {
let num_parse_res: Result<f32, ParseFloatError> = cell.data.parse();
if let Ok(_) = num_parse_res {
let dot_i = cell.data.find(".").unwrap();
if let Some(dot_i) = cell.data.find(".") {
// -1 cuz the dot is there
let count_to_dot = cell.data.chars().count() as u32;
let count_from_dot = cell.data.chars().skip(dot_i + 1).count() as u32;
@ -167,7 +167,10 @@ fn parse_decimal(cell: &mut DataEntry, args: Option<String>) {
} else {
cell.is_parsed = Some(Err(Arc::from(<&str as Into<
Box<dyn Error + Send + Sync>,
>>::into("too many numbers"))))
>>::into(
"too many numbers"
))))
}
}
} else {
cell.is_parsed = Some(Err(Arc::from(<&str as Into<

View file

@ -1,5 +1,5 @@
#![cfg_attr(not(debug_assertions), deny(dead_code), deny(unused_variables))]
#![cfg_attr(debug_assertions, allow(dead_code), allow(unused_variables))]
//#![cfg_attr(not(debug_assertions), deny(dead_code), deny(unused_variables))]
//#![cfg_attr(debug_assertions, allow(dead_code), allow(unused_variables))]
mod backend;
mod ui;
use backend::backend_manager::BackendManger;

View file

@ -41,6 +41,7 @@ impl CSQLWindow for DBTransactionWindow {
ui: &mut Ui,
frame: &mut eframe::Frame,
) -> Option<Result<ExitStatus, Box<dyn std::error::Error>>> {
println!("Opening Transaction window...");
egui::Window::new("Database Transactions")
.id(egui::Id::new("Database Transactions"))
.resizable(false)

View file

@ -4,6 +4,7 @@ use crate::backend::csv_handler::ImportedData;
use crate::backend::database_handler::{TableField, Tables};
use egui::{ComboBox, Context, Ui};
use egui_extras::{Column, TableBuilder};
use if_chain::if_chain;
use std::error::Error;
use std::sync::Arc;
use tokio::sync::mpsc::Sender;
@ -43,7 +44,7 @@ impl CSQLWindow for SpreadSheetWindow {
frame: &mut eframe::Frame,
) -> Option<Result<ExitStatus, Box<dyn std::error::Error>>> {
self.ui(ui, ctx);
if let Some(return_status) = self.return_status {
if let Some(return_status) = self.return_status.clone() {
self.return_status = None;
return Some(Ok(return_status));
}
@ -109,77 +110,44 @@ impl SpreadSheetWindow {
}
});
SpreadSheetWindow::preview_files_being_dropped(ctx);
/*All the dzum checks before making the window... */
if self.current_table.is_some() {
if_chain! {
if let Some(current_table) = self.current_table;
if let Ok(csv_data) = &mut self.csv_data_handle.try_lock();
if let Ok(db_table_data) = &mut self.db_table_data_handle.try_lock();
if !csv_data.data.is_empty();
if let Some(curr_db_table) = db_table_data.tables.get_mut(current_table);
then {
if let Some(curr_db_table_fields) = curr_db_table.fields.as_mut() {
ui.group(|ui| {
egui::ScrollArea::horizontal().show(ui, |ui| {
self.table_builder(ui)
.unwrap_or_else(|e| println!("Table un-intd, {:?}", e));
SpreadSheetWindow::table_builder(
&mut self.sender,
ui,
csv_data,
curr_db_table_fields,
)
});
});
}
}
fn table_options(&mut self, ui: &mut Ui) {
/* Create table select option, only enable if the tables are discovered yet*/
if let Ok(db_table_data) = &mut self.db_table_data_handle.try_lock() {
ui.add_enabled_ui(db_table_data.tables.get(0).is_some(), |ui| {
let mut select_table = ComboBox::from_label("Select Table").width(100.0);
if let Some(table_index) = self.current_table {
if let Some(current_table) = &db_table_data.tables.get(table_index) {
select_table = select_table.selected_text(current_table.name.clone());
}
}
/* autoselect oc_product in debug mode */
if cfg!(debug_assertions) && self.current_table.is_none() {
self.current_table = Some(88);
if let Some(table_88) = &db_table_data.tables.get(88) {
select_table = select_table.selected_text(table_88.name.clone());
}
}
ui.vertical(|ui| {
select_table.show_ui(ui, |ui| {
for (table_i, table) in db_table_data.tables.iter().enumerate() {
ui.selectable_value(
&mut self.current_table,
Some(table_i.clone()),
&table.name,
);
}
});
});
});
}
}
fn table_builder(&mut self, ui: &mut Ui) -> Result<(), Box<dyn std::error::Error>> {
if let Ok(csv_data) = &mut self.csv_data_handle.try_lock() {
if let Ok(db_table_data) = &mut self.db_table_data_handle.try_lock() {
//ref to all fields in curr db table
let mut curr_db_table_fields: &mut Vec<TableField> = match db_table_data
.tables
.get_mut(self.current_table.unwrap())
.ok_or("no current table")?
.fields
.as_mut()
.ok_or("no fields yet")
{
Ok(val) => val,
Err(e) => {
} else {
if !self.is_table_description_request_sent {
self.sender.try_send(Communication::GetTableDescription(
self.current_table.unwrap(),
))?;
self.is_table_description_request_sent = true;
if let Ok(mesg) = self.sender.try_send(
Communication::GetTableDescription(self.current_table.unwrap()),
) {}
}
return Err(Box::from(
<String as Into<Box<dyn std::error::Error>>>::into(e.to_string()),
));
}
};
if csv_data.data.is_empty() {
return Err(<String as Into<Box<dyn std::error::Error>>>::into(
"no rows yet".to_string(),
));
} else {
ui.centered_and_justified(|ui| ui.heading("Import or Drag and Drop a file..."));
}
}
}
fn table_builder(
sender: &mut Sender<Communication>,
ui: &mut Ui,
csv_data: &mut MutexGuard<ImportedData>,
curr_db_table_fields: &mut Vec<TableField>,
) {
let table = TableBuilder::new(ui)
.striped(true)
.cell_layout(egui::Layout::left_to_right(egui::Align::Center))
@ -194,36 +162,53 @@ impl SpreadSheetWindow {
.min_scrolled_height(0.0)
.header(20., |mut header| {
/* First Col for row mutators */
header.col(|ui|{
});
SpreadSheetWindow::add_header_cols(&mut header, csv_data,&mut curr_db_table_fields, &mut self.sender);
header.col(|ui| {});
SpreadSheetWindow::add_header_cols(
&mut header,
csv_data,
curr_db_table_fields,
sender,
);
})
.body(|body| {
body.rows(15., csv_data.data.rows() - csv_data.are_headers as usize, |row_index, mut row| {
body.rows(
15.,
csv_data.data.rows() - csv_data.are_headers as usize,
|row_index, mut row| {
let headers_i_offset = csv_data.are_headers as usize;
row.col(|ui| {
if ui.button("x").clicked(){}
});
for curr_cell in
csv_data.data.iter_row_mut(row_index + headers_i_offset)
if ui.button("x").clicked() {
if let Ok(_) = sender.try_send(Communication::RemoveRow(row_index))
{
};
}
});
for curr_cell in csv_data.data.iter_row_mut(row_index + headers_i_offset) {
/* If cell is bound to a field, color it's bg according to is_parsed */
row.col(|ui| {
let mut err: Option<Arc<dyn Error>> = None;
if curr_cell.curr_field_description.is_some() {
match &curr_cell.is_parsed {
Some(parse) => match parse {
Ok(_) => ui.style_mut().visuals.extreme_bg_color = egui::Color32::DARK_GREEN,
Ok(_) => {
ui.style_mut().visuals.extreme_bg_color =
egui::Color32::DARK_GREEN
}
Err(arc) => {
err = Some(arc.clone());
ui.style_mut().visuals.extreme_bg_color = egui::Color32::DARK_RED;
ui.style_mut().visuals.extreme_bg_color =
egui::Color32::DARK_RED;
}
},
None => {
ui.style_mut().visuals.extreme_bg_color =
egui::Color32::DARK_GRAY
}
}
None => ui.style_mut().visuals.extreme_bg_color = egui::Color32::DARK_GRAY,
}
}
if err.is_some(){
ui.text_edit_singleline(&mut curr_cell.data).on_hover_text(format!("{}", &err.unwrap()));
if let Some(err) = err {
ui.text_edit_singleline(&mut curr_cell.data)
.on_hover_text(format!("{}", err));
} else {
ui.text_edit_singleline(&mut curr_cell.data);
}
@ -233,13 +218,51 @@ impl SpreadSheetWindow {
}
});
}
},
);
});
}
fn table_options(&mut self, ui: &mut Ui) {
/* Create table select option, only enable if the tables are discovered yet*/
if let Ok(db_table_data) = &mut self.db_table_data_handle.try_lock() {
ui.add_enabled_ui(db_table_data.tables.get(0).is_some(), |ui| {
let mut select_table = ComboBox::from_label("Select Table").width(100.0);
if let Some(table_index) = self.current_table {
if let Some(current_table) = &db_table_data.tables.get(table_index) {
select_table = select_table.selected_text(current_table.name.clone());
}
}
/* autoselect oc_product in debug mode */
if cfg!(debug_assertions) && self.current_table.is_none() {
self.current_table = Some(88);
db_table_data.current_working_table = Some(88);
if let Some(table_88) = &db_table_data.tables.get(88) {
select_table = select_table.selected_text(table_88.name.clone());
}
}
ui.vertical(|ui| {
select_table.show_ui(ui, |ui| {
let mut current_working_table_i = None;
for (table_i, table) in db_table_data.tables.iter_mut().enumerate() {
if ui
.selectable_value(
&mut self.current_table,
Some(table_i.clone()),
&table.name,
)
.clicked()
{
current_working_table_i = Some(table_i);
};
}
if let Some(current_working_table_i) = current_working_table_i {
db_table_data.current_working_table = Some(current_working_table_i);
}
});
});
});
}
} else {
ui.centered_and_justified(|ui| ui.heading("Drag and drop or Open a file..."));
}
Ok(())
}
}
@ -253,7 +276,9 @@ impl SpreadSheetWindow {
for i in 0..csv_data.data.cols() {
header.col(|ui| {
ui.vertical_centered_justified(|ui| {
if ui.button("x").clicked() {}
if ui.button("x").clicked() {
if let Ok(_) = sender.try_send(Communication::RemoveCol(i)) {}
}
if csv_data.parsed_cols.contains(&i) {
/* If the whole col is parsed, change header bg to green, else normal */
ui.style_mut().visuals.override_text_color = Some(egui::Color32::GREEN);
@ -340,13 +365,13 @@ impl SpreadSheetWindow {
} else {
return;
}
}
self.sender
.try_send(Communication::LoadImportFilePath(
final_path.display().to_string(),
))
.unwrap_or_else(|err| println!("failed to send loadimportpath, {}", err));
}
}
pub fn open_dropped_file(&mut self, path: &egui::InputState) {
let path = path

View file

@ -1,15 +1,15 @@
use super::db_transaction_window::DBTransactionWindow;
use crate::backend::backend_manager::Communication;
use crate::backend::csv_handler::ImportedData;
use crate::backend::database_handler::Tables;
use crate::ui::db_login_window::DBLoginWindow;
use crate::ui::table_window::SpreadSheetWindow;
use eframe::{run_native, App, NativeOptions};
use if_chain::*;
use std::sync::Arc;
use tokio::sync::mpsc::{channel, Sender};
use tokio::sync::Mutex;
use super::db_transaction_window::DBTransactionWindow;
pub fn create_ui(
sender: Sender<Communication>,
csv_data_handle: Arc<Mutex<ImportedData>>,
@ -60,8 +60,11 @@ impl App for CSQL {
if let Some(result) = self.spreadsheet_window.refresh(ctx, ui, frame) {
match result {
Ok(status) => match status {
ExitStatus::StartLoginWindow => self.should_open_transaction_window = true,
ExitStatus::StartTransactionWindow => self.should_open_login_window = true,
ExitStatus::StartLoginWindow => self.should_open_login_window = true,
ExitStatus::StartTransactionWindow => {
self.should_open_transaction_window = true;
println!("should_open_transaction_window");
}
_ => {}
},
_ => (),
@ -83,7 +86,9 @@ impl App for CSQL {
self.db_login_window = Some(DBLoginWindow::default(self.sender.clone()));
}
}
if self.should_open_transaction_window {
println!("inside if.shoud...");
if let Some(db_transaction_window) = self.db_transaction_window.as_mut() {
if let Some(result) = db_transaction_window.refresh(ctx, ui, frame) {
match result {
@ -95,14 +100,17 @@ impl App for CSQL {
}
}
} else {
println!("No transaction window, creating...");
if let Ok(db_table_data) = self.db_table_data_handle.try_lock() {
println!("got a db_lock");
if let Some(cw_table_i) = db_table_data.current_working_table {
println!("Found a current_working _table");
self.db_transaction_window = Some(DBTransactionWindow::default(
self.sender.clone(),
cw_table_i,
))
}
}
};
}
}
});