From ee4ee00964159c8b01e6c3a5825cd1a2d78b2e11 Mon Sep 17 00:00:00 2001 From: Damian Wessels Date: Tue, 13 May 2025 22:27:07 +0200 Subject: [PATCH] Vergessene Dateien --- .gitignore | 1 + src/config.rs | 27 ++++++++++++++++++++ src/database.rs | 47 +++++++++++++++++++++++++++++++++++ src/state.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ src/style.rs | 19 ++++++++++++++ 5 files changed, 160 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/src/config.rs b/src/config.rs index e69de29..9ff66be 100644 --- a/src/config.rs +++ b/src/config.rs @@ -0,0 +1,27 @@ +use anyhow::{anyhow, Result }; +use directories::ProjectDirs; + +pub struct Config { + pub database_file: String, + pub default_sheet: String, +} + +impl Config { + pub fn build() -> Result { + let proj_dirs = ProjectDirs::from("de","schacht-analyse","timetracker") + .ok_or(anyhow!("Couldn't get project directories"))?; + + let data_dir = proj_dirs.data_dir(); + let mut db_file = data_dir.to_path_buf(); + + db_file.push("database.db"); + + if let Some(db_file_str) = db_file.to_str() { + return Ok(Config { + database_file: db_file_str.to_string(), + default_sheet: "default".to_string(), + }); + } + Err(anyhow!("Couldn't get database file")) + } +} \ No newline at end of file diff --git a/src/database.rs b/src/database.rs index e69de29..3d6eb58 100644 --- a/src/database.rs +++ b/src/database.rs @@ -0,0 +1,47 @@ +use std::path::PathBuf; +use anyhow::{anyhow, Context}; +use anyhow::Result; +use std::str::FromStr; +use rusqlite::Connection; + +use crate::config::Config; + +pub fn connect_to_db(config: &Config) -> Result { + if let Ok(conn) = Connection::open(&config.database_file) { + return Ok(conn); + } + + Err(anyhow!( + "Cannot create connection to database" + )) +} + +pub fn create_tables(db: &Connection) -> Result<()> { + let query = " + CREATE TABLE IF NOT EXISTS entries ( + id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + note VARCHAR(255) NOT NULL, + start TIMESTAMP NOT NULL, + end TIMESTAMP NOT NULL, + sheet VARCHAR(255) NOT NULL + ) + "; + db.execute(query, ()) + .context("Failed to create entries table")?; + + Ok(()) +} + +pub fn ensure_db_exists(config: &Config) -> Result<()> { + let mut db_path = PathBuf::from_str(&config.database_file).unwrap(); + db_path.pop(); + + if!db_path.exists() { + std::fs::create_dir_all(&db_path).context(format!( + "Cannot create the data folder for Timetracker. The expected path was {:?}", + &db_path + ))?; + + } + Ok(()) +} \ No newline at end of file diff --git a/src/state.rs b/src/state.rs index e69de29..173e17b 100644 --- a/src/state.rs +++ b/src/state.rs @@ -0,0 +1,66 @@ +use rusqlite::Connection; +use anyhow::{anyhow, Context, Result }; +use directories::ProjectDirs; +use std::fs; + +use crate::config::Config; +use crate::database::connect_to_db; + +pub struct State { + pub current_sheet: String, + pub last_sheet: String, + pub database: Connection, +} + +impl State { + pub fn build(config: &Config) -> Result { + let db = connect_to_db(config)?; + let proj_dirs = ProjectDirs::from("de", "schacht-analyse", "timetracker") + .ok_or(anyhow!("Could not determine project directories"))?; + + let mut state = State { + current_sheet : "default".to_string(), + last_sheet: "default".to_string(), + database: db, + }; + + let data_dir = proj_dirs.data_local_dir(); + let mut data_file = data_dir.to_path_buf(); + data_file.push("data.txt"); + + let content_res = fs::read_to_string(&data_file); + + match content_res { + Ok(content) => { + let mut lines = content.lines(); + if let Some(s) = lines.next() { + state.current_sheet = s.to_string(); + } + if let Some(s) = lines.next() { + state.last_sheet = s.to_string(); + } + } + Err(_) => { + state + .update_file() + .context("Cannot write the default state file.")?; + } + } + Ok(state) + } + + fn update_file(&self) -> Result<()> { + let proj_dirs = ProjectDirs::from("de", "schacht-analyse", "timetracker") + .ok_or(anyhow!("Could not determine project directories"))?; + + let data_dir = proj_dirs.data_local_dir(); + let mut data_file = data_dir.to_path_buf(); + data_file.push("data.txt"); + + fs::write(&data_file, + format!("{}\n{}",self.current_sheet,self.last_sheet),)?; + Ok(()) + + } + +} \ No newline at end of file diff --git a/src/style.rs b/src/style.rs index e69de29..8431e22 100644 --- a/src/style.rs +++ b/src/style.rs @@ -0,0 +1,19 @@ +use colored::{ColoredString, Colorize}; + +pub enum Styles { + Error, + Title, + Message, + Primary, + Secondary, +} + +pub fn style_string(label: &str, style: Styles) -> ColoredString { + match style { + Styles::Error => label.red().bold(), + Styles::Title => label.bold(), + Styles::Message => label.bold(), + Styles::Primary => label.green().bold(), + Styles::Secondary => label.cyan(), + } +} \ No newline at end of file