From e8680cb0528d01e14d25c0f89df477e985b20c1f Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 2 Oct 2024 18:46:08 +0300 Subject: [PATCH] move profile filesystem dependencies init on the main level --- src/app.rs | 30 ++++++------------------------ src/app/database.rs | 9 +++++---- src/main.rs | 26 ++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/app.rs b/src/app.rs index 5e5fae48..c312fdda 100644 --- a/src/app.rs +++ b/src/app.rs @@ -7,12 +7,13 @@ use browser::Browser; use database::Database; use gtk::{ - glib::{user_config_dir, ExitCode}, + glib::ExitCode, prelude::{ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt}, Application, }; +use sqlite::Connection; -use std::{fs::create_dir_all, sync::Arc}; +use std::sync::Arc; const APPLICATION_ID: &str = "io.github.yggverse.Yoda"; @@ -26,28 +27,9 @@ pub struct App { impl App { // Construct - pub fn new() -> Self { - // Init profile directory path - let mut profile_path = user_config_dir(); - - profile_path.push(APPLICATION_ID); - - if let Err(e) = create_dir_all(&profile_path) { - panic!("Failed to create profile directory: {e}") - } - - // Init profile database path - let mut database_path = profile_path.clone(); - - database_path.push("database.sqlite3"); - - let connection = match sqlite::open(database_path) { - Ok(connection) => Arc::new(connection), - Err(e) => panic!("Failed to connect profile database: {e}"), - }; - - // Init database model - let database = Arc::new(Database::init(connection)); + pub fn new(profile_database_connection: Arc) -> Self { + // Init app database model + let database = Arc::new(Database::init(profile_database_connection)); // Init actions let action_debug = Action::new("win", true); diff --git a/src/app/database.rs b/src/app/database.rs index 66131dd1..366fa240 100644 --- a/src/app/database.rs +++ b/src/app/database.rs @@ -1,3 +1,4 @@ +use sqlite::Connection; use std::sync::Arc; pub struct Database { @@ -6,18 +7,18 @@ pub struct Database { impl Database { // Construct new application DB - pub fn init(connection: Arc) -> Database { + pub fn init(connection: Arc) -> Database { // Init app table - if let Err(e) = connection.execute( + if let Err(error) = connection.execute( r" CREATE TABLE IF NOT EXISTS `app` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - `time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP, + `time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP ) ", ) { - panic!("{e}"); + panic!("{error}"); } // Return struct diff --git a/src/main.rs b/src/main.rs index fc1492e6..5fa594ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,30 @@ mod app; use app::App; -use gtk::glib::ExitCode; +use gtk::glib::{user_config_dir, ExitCode}; +use std::{fs::create_dir_all, sync::Arc}; fn main() -> ExitCode { - App::new().run() + // Init profile path + let mut profile_path = user_config_dir(); + + profile_path.push(env!("CARGO_PKG_NAME")); + + if let Err(error) = create_dir_all(&profile_path) { + panic!("Failed to create profile directory: {error}") + } + + // Init profile database path + let mut profile_database_path = profile_path.clone(); + + profile_database_path.push("database.sqlite3"); + + // Init database connection + let profile_database_connection = match sqlite::open(profile_database_path) { + Ok(profile_database_connection) => Arc::new(profile_database_connection), + Err(error) => panic!("Failed to connect profile database: {error}"), + }; + + // Start application + App::new(profile_database_connection).run() }