From 703496a49315f7b67442abf0e0df3c41d3605d3b Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Fri, 15 Aug 2025 16:02:40 +0000 Subject: task-012 (#2) Co-authored-by: Joe Carstairs Reviewed-on: https://git.joeac.net/joeac/schist/pulls/2 Co-authored-by: Joe Carstairs Co-committed-by: Joe Carstairs --- schist_desktop_gui/src/connection.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 schist_desktop_gui/src/connection.rs (limited to 'schist_desktop_gui/src/connection.rs') diff --git a/schist_desktop_gui/src/connection.rs b/schist_desktop_gui/src/connection.rs new file mode 100644 index 0000000..5a88083 --- /dev/null +++ b/schist_desktop_gui/src/connection.rs @@ -0,0 +1,20 @@ +use std::{fs::create_dir_all, path::Path}; + +use anyhow::Context; +use diesel::{Connection, SqliteConnection}; + +const SCHIST_DATA_DIR: &'static str = "schist"; +const SCHIST_DATABASE_FILENAME: &'static str = "schist.sqlite"; + +pub fn establish_connection() -> anyhow::Result { + let data_dir = dirs::data_dir().context("Failed to get local app data directory")?; + let schist_local_dir = data_dir.join(Path::new(SCHIST_DATA_DIR)); + create_dir_all(&schist_local_dir) + .context(format!("Failed to create local files directory at {}", schist_local_dir.display()))?; + let db_path = schist_local_dir.join(Path::new(SCHIST_DATABASE_FILENAME)); + let db_path = db_path + .to_str() + .context("Failed to construct database path")?; + Ok(SqliteConnection::establish(db_path) + .with_context(|| format!("Failed to connect to database at {}", db_path))?) +} -- cgit v1.2.3