diff --git a/Cargo.lock b/Cargo.lock index 7a7de7b..132dbaa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1658,7 +1658,6 @@ dependencies = [ "objc", "once_cell", "ps-core", - "ps-database", "ps-importer", "rand", "shellexpand", diff --git a/postsack/src/main.rs b/postsack/src/main.rs index 3d45b9e..e2309f8 100644 --- a/postsack/src/main.rs +++ b/postsack/src/main.rs @@ -1,6 +1,8 @@ +use ps_database::Database; + fn main() { #[cfg(debug_assertions)] ps_core::setup_tracing(); - ps_gui::run_ui(); + ps_gui::run_ui::(); } diff --git a/ps-core/src/database/database_like.rs b/ps-core/src/database/database_like.rs index 793bdab..ede09be 100644 --- a/ps-core/src/database/database_like.rs +++ b/ps-core/src/database/database_like.rs @@ -8,8 +8,11 @@ use crate::Config; use super::{db_message::DBMessage, query::Query, query_result::QueryResult}; -pub trait DatabaseLike: Clone + Send { +pub trait DatabaseLike: Clone + Send + 'static { fn new(path: impl AsRef) -> Result + where + Self: Sized; + fn config(path: impl AsRef) -> Result where Self: Sized; fn total_mails(&self) -> Result; diff --git a/ps-core/src/types/config.rs b/ps-core/src/types/config.rs index 804e8e1..7301b3c 100644 --- a/ps-core/src/types/config.rs +++ b/ps-core/src/types/config.rs @@ -194,7 +194,6 @@ impl Config { fn random_filename() -> String { use rand::Rng; let number: u32 = rand::thread_rng().gen(); - let folder = "postsack"; let filename = format!("{}.sqlite", number); return filename; } diff --git a/ps-database/src/db.rs b/ps-database/src/db.rs index 906a6e7..fe64dc4 100644 --- a/ps-database/src/db.rs +++ b/ps-database/src/db.rs @@ -48,6 +48,13 @@ impl DatabaseLike for Database { }) } + /// Open a database and try to retrieve a config from the information stored in there + fn config(path: impl AsRef) -> Result { + let database = Self::new(path.as_ref())?; + let fields = database.select_config_fields()?; + Config::from_fields(path.as_ref(), fields) + } + fn total_mails(&self) -> Result { let connection = match &self.connection { Some(n) => n, @@ -171,13 +178,6 @@ impl DatabaseLike for Database { } impl Database { - /// Open a database and try to retrieve a config from the information stored in there - pub fn config>(path: P) -> Result { - let database = Self::new(path.as_ref())?; - let fields = database.select_config_fields()?; - Config::from_fields(path.as_ref(), fields) - } - fn create_tables(connection: &Connection) -> Result<()> { connection.execute(TBL_EMAILS, params![])?; connection.execute(TBL_ERRORS, params![])?; diff --git a/ps-gui/Cargo.toml b/ps-gui/Cargo.toml index 609dd7b..555ee94 100644 --- a/ps-gui/Cargo.toml +++ b/ps-gui/Cargo.toml @@ -26,7 +26,6 @@ version = "0.2.7" [target."cfg(not(target_arch = \"wasm32\"))".dependencies] ps-importer = { path = "../ps-importer" } -ps-database = { path = "../ps-database" } shellexpand = "2.1.0" tinyfiledialogs = "3.0" diff --git a/ps-gui/src/app.rs b/ps-gui/src/app.rs index 93d399f..e85b435 100644 --- a/ps-gui/src/app.rs +++ b/ps-gui/src/app.rs @@ -1,3 +1,5 @@ +use std::marker::PhantomData; + use eframe::{ egui::{self}, epi::{self, App, Frame, Storage}, @@ -7,25 +9,28 @@ use super::app_state::StateUI; use super::platform::Theme; use super::textures::Textures; -pub struct PostsackApp { +use ps_core::DatabaseLike; + +pub struct PostsackApp { state: StateUI, platform_custom_setup: bool, - textures: Option, + _database: PhantomData, } -impl PostsackApp { +impl PostsackApp { pub fn new() -> Self { let state = StateUI::new(); PostsackApp { state, platform_custom_setup: false, textures: None, + _database: PhantomData, } } } -impl App for PostsackApp { +impl App for PostsackApp { fn name(&self) -> &str { "Postsack" } @@ -51,7 +56,7 @@ impl App for PostsackApp { } } - self.state.update(ctx, &self.textures); + self.state.update::(ctx, &self.textures); // Resize the native window to be just the size we need it to be: frame.set_window_size(ctx.used_size()); diff --git a/ps-gui/src/app_state/main.rs b/ps-gui/src/app_state/main.rs index 1608c05..a7eabf7 100644 --- a/ps-gui/src/app_state/main.rs +++ b/ps-gui/src/app_state/main.rs @@ -4,8 +4,7 @@ use eyre::{Report, Result}; use super::super::widgets::{FilterState, Spinner}; use super::Textures; use super::{StateUIAction, StateUIVariant}; -use ps_core::{model::Engine, Config}; -use ps_database::Database; +use ps_core::{model::Engine, Config, DatabaseLike}; #[derive(Default)] pub struct UIState { @@ -25,7 +24,7 @@ pub struct MainUI { } impl MainUI { - pub fn new(config: Config, total: usize) -> Result { + pub fn new(config: Config, total: usize) -> Result { let mut engine = Engine::new::(&config)?; engine.start()?; Ok(Self { diff --git a/ps-gui/src/app_state/mod.rs b/ps-gui/src/app_state/mod.rs index 8efc84e..02eb07e 100644 --- a/ps-gui/src/app_state/mod.rs +++ b/ps-gui/src/app_state/mod.rs @@ -14,8 +14,6 @@ pub use main::{MainUI, UIState}; pub use startup::StartupUI; use ps_core::{Config, DatabaseLike, FormatType}; -// FIXME: Abstract away with a trait? -use ps_database::Database; pub enum StateUIAction { CreateDatabase { @@ -60,7 +58,11 @@ impl StateUI { /// This proxies the `update` call to the individual calls in /// the `app_state` types - pub fn update(&mut self, ctx: &egui::CtxRef, textures: &Option) { + pub fn update( + &mut self, + ctx: &egui::CtxRef, + textures: &Option, + ) { let response = match self { StateUI::Startup(panel) => panel.update_panel(ctx, textures), StateUI::Import(panel) => panel.update_panel(ctx, textures), @@ -74,14 +76,18 @@ impl StateUI { sender_emails, format, } => { - *self = - self.create_database(database_path, emails_folder_path, sender_emails, format) + *self = self.create_database::( + database_path, + emails_folder_path, + sender_emails, + format, + ) } StateUIAction::OpenDatabase { database_path } => { - *self = self.open_database(database_path) + *self = self.open_database::(database_path) } StateUIAction::ImportDone { config, total } => { - *self = match main::MainUI::new(config.clone(), total) { + *self = match main::MainUI::new::(config.clone(), total) { Ok(n) => StateUI::Main(n), Err(e) => StateUI::Error(ErrorUI::new(e, Some(config))), }; @@ -102,7 +108,7 @@ impl StateUI { StateUI::Startup(startup::StartupUI::default()) } - pub fn create_database( + pub fn create_database( &self, database_path: Option, emails_folder_path: PathBuf, @@ -124,7 +130,7 @@ impl StateUI { self.importer_with_config(config, database) } - pub fn open_database(&mut self, database_path: PathBuf) -> StateUI { + pub fn open_database(&mut self, database_path: PathBuf) -> StateUI { let config = match Database::config(&database_path) { Ok(config) => config, Err(report) => return StateUI::Error(error::ErrorUI::new(report, None)), @@ -135,13 +141,17 @@ impl StateUI { Err(report) => return StateUI::Error(error::ErrorUI::new(report, None)), }; - match main::MainUI::new(config.clone(), total) { + match main::MainUI::new::(config.clone(), total) { Ok(n) => StateUI::Main(n), Err(e) => StateUI::Error(ErrorUI::new(e, Some(config))), } } - fn importer_with_config(&self, config: Config, database: Database) -> StateUI { + fn importer_with_config( + &self, + config: Config, + database: Database, + ) -> StateUI { let importer = match import::ImporterUI::new(config.clone(), database) { Ok(n) => n, Err(e) => { diff --git a/ps-gui/src/lib.rs b/ps-gui/src/lib.rs index 976bc3e..dc4ca28 100644 --- a/ps-gui/src/lib.rs +++ b/ps-gui/src/lib.rs @@ -7,9 +7,9 @@ mod segmentation_bar; mod textures; pub(crate) mod widgets; +use ps_core::DatabaseLike; -pub fn run_ui() { +pub fn run_ui() { let options = eframe::NativeOptions::default(); - eframe::run_native(Box::new(app::PostsackApp::new()), options); + eframe::run_native(Box::new(app::PostsackApp::::new()), options); } -