Used `DatabaseLike` trait instead of actual `Database` in gui

pull/8/head
Benedikt Terhechte 2 years ago
parent 93cbcea02a
commit d1d443f036

1
Cargo.lock generated

@ -1658,7 +1658,6 @@ dependencies = [
"objc",
"once_cell",
"ps-core",
"ps-database",
"ps-importer",
"rand",
"shellexpand",

@ -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::<Database>();
}

@ -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<Path>) -> Result<Self>
where
Self: Sized;
fn config(path: impl AsRef<Path>) -> Result<Config>
where
Self: Sized;
fn total_mails(&self) -> Result<usize>;

@ -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;
}

@ -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<Path>) -> Result<Config> {
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<usize> {
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<P: AsRef<Path>>(path: P) -> Result<Config> {
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![])?;

@ -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"

@ -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<Database: DatabaseLike> {
state: StateUI,
platform_custom_setup: bool,
textures: Option<Textures>,
_database: PhantomData<Database>,
}
impl PostsackApp {
impl<Database: DatabaseLike> PostsackApp<Database> {
pub fn new() -> Self {
let state = StateUI::new();
PostsackApp {
state,
platform_custom_setup: false,
textures: None,
_database: PhantomData,
}
}
}
impl App for PostsackApp {
impl<Database: DatabaseLike> App for PostsackApp<Database> {
fn name(&self) -> &str {
"Postsack"
}
@ -51,7 +56,7 @@ impl App for PostsackApp {
}
}
self.state.update(ctx, &self.textures);
self.state.update::<Database>(ctx, &self.textures);
// Resize the native window to be just the size we need it to be:
frame.set_window_size(ctx.used_size());

@ -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<Self> {
pub fn new<Database: DatabaseLike>(config: Config, total: usize) -> Result<Self> {
let mut engine = Engine::new::<Database>(&config)?;
engine.start()?;
Ok(Self {

@ -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<Textures>) {
pub fn update<Database: DatabaseLike>(
&mut self,
ctx: &egui::CtxRef,
textures: &Option<Textures>,
) {
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>(
database_path,
emails_folder_path,
sender_emails,
format,
)
}
StateUIAction::OpenDatabase { database_path } => {
*self = self.open_database(database_path)
*self = self.open_database::<Database>(database_path)
}
StateUIAction::ImportDone { config, total } => {
*self = match main::MainUI::new(config.clone(), total) {
*self = match main::MainUI::new::<Database>(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<Database: DatabaseLike>(
&self,
database_path: Option<PathBuf>,
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<Database: DatabaseLike>(&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::<Database>(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<Database: DatabaseLike>(
&self,
config: Config,
database: Database,
) -> StateUI {
let importer = match import::ImporterUI::new(config.clone(), database) {
Ok(n) => n,
Err(e) => {

@ -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<Database: DatabaseLike>() {
let options = eframe::NativeOptions::default();
eframe::run_native(Box::new(app::PostsackApp::new()), options);
eframe::run_native(Box::new(app::PostsackApp::<Database>::new()), options);
}

Loading…
Cancel
Save