diff --git a/Cargo.lock b/Cargo.lock index ab88943..ba1c61d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -225,7 +225,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] -name = "meowlog2" +name = "meowlog" version = "0.1.0" dependencies = [ "chrono", diff --git a/Cargo.toml b/Cargo.toml index 4254a84..64352c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "meowlog2" +name = "meowlog" version = "0.1.0" edition = "2024" diff --git a/src/db.rs b/src/db.rs index 373e2c9..c77b0d1 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,8 +1,8 @@ -use crate::{IngestionEntry, Unit}; +use crate::{IngestionEntry, Unit, util::get_data_path}; use sqlite::{Connection, Result}; pub fn init_db_conn() -> Result<Connection> { - let conn = sqlite::open("ingestions.db"); + let conn = sqlite::open(get_data_path().unwrap().join("ingestions.db")); match conn { Ok(c) => Ok(c), Err(e) => { diff --git a/src/main.rs b/src/main.rs index 43c30bd..7d4cea4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,10 +2,12 @@ use chrono::{DateTime, Local}; use db::create_db; use serde::{Deserialize, Serialize}; use std::error::Error; +use std::process::exit; use strum::EnumIter; use strum::IntoEnumIterator; use strum_macros::Display; use util::gather_ingestion_data; +use util::initialize_app_directory; mod util; use util::read_substances_from_file; @@ -13,12 +15,26 @@ use util::read_substances_from_file; mod db; use db::init_db_conn; -#[derive(Debug, Display, Serialize, Deserialize, EnumIter)] +#[derive(Debug, Serialize, Deserialize, EnumIter)] pub enum Command { AddIngestion, EditIngestion, ListIngestions, DeleteIngestion, + Quit, +} + +impl std::fmt::Display for Command { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let command_str = match self { + Command::AddIngestion => "Add", + Command::EditIngestion => "Edit", + Command::ListIngestions => "List", + Command::DeleteIngestion => "Delete", + Command::Quit => "Quit", + }; + write!(f, "{}", command_str) + } } #[derive(Debug, Clone, Display, Serialize, Deserialize, EnumIter)] @@ -46,7 +62,7 @@ fn main() -> Result<(), Box<dyn Error>> { println!("======="); // Load substances from JSON file - let substances = match read_substances_from_file("drugs.json") { + let substances = match read_substances_from_file() { Ok(s) => s, Err(e) => { eprintln!("Error loading substances file: {}", e); @@ -54,15 +70,24 @@ fn main() -> Result<(), Box<dyn Error>> { } }; + let _ = initialize_app_directory()?; let _ = create_db(); println!("Successfully loaded {} substances", substances.len()); + loop { + handle_commands(substances.clone())?; + } +} + +fn handle_commands( + substances: std::collections::HashMap<String, serde_json::Value>, +) -> Result<(), Box<dyn Error>> { let command = inquire::Select::new("What do you want to do?", Command::iter().collect()).prompt()?; match command { Command::AddIngestion => { - let ingestion = gather_ingestion_data(substances)?; + let ingestion = gather_ingestion_data(substances.clone())?; // Display the recorded information println!("\nRecorded Information:"); @@ -110,7 +135,7 @@ fn main() -> Result<(), Box<dyn Error>> { util::delete_ingestion_entries(&init_db_conn()?)?; } Command::EditIngestion => util::edit_ingestion_entry(&init_db_conn()?)?, + Command::Quit => exit(0), } - Ok(()) } diff --git a/src/util.rs b/src/util.rs index ac34ebc..378ec77 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,10 +2,13 @@ use chrono::TimeZone; use inquire::{CustomType, DateSelect, Select}; use serde_json::Value; use std::collections::HashMap; +use std::env; use std::error::Error; +use std::fs; use std::fs::File; use std::io::BufReader; use std::path::Path; +use std::path::PathBuf; use strum::IntoEnumIterator; use crate::{IngestionEntry, Unit, db::init_db_conn}; @@ -16,15 +19,35 @@ pub struct IngestionResponse { pub data: Value, } -pub fn read_substances_from_file<P: AsRef<Path>>( - path: P, -) -> Result<HashMap<String, Value>, Box<dyn Error>> { - // Open the file in read-only mode - let file = File::open(path)?; - let reader = BufReader::new(file); +pub fn get_data_path() -> Result<PathBuf, Box<dyn Error>> { + let home_dir = match env::var("HOME") { + Ok(path) => PathBuf::from(path), + Err(_) => return Err("Could not determine home directory".into()), + }; + + // Create path to ~/.local/share/meowlog + let app_dir = home_dir.join(".local").join("share").join("meowlog"); + + Ok(app_dir) +} +pub fn initialize_app_directory() -> Result<PathBuf, Box<dyn Error>> { + // Create path to ~/.local/share/meowlog + let app_dir = get_data_path()?; + + // Create directory if it doesn't exist + if !app_dir.exists() { + fs::create_dir_all(&app_dir)?; + println!("Created application directory at: {}", app_dir.display()); + } + + Ok(app_dir) +} + +pub fn read_substances_from_file() -> Result<HashMap<String, Value>, Box<dyn Error>> { + let json_content = include_str!("../drugs.json"); // Read the JSON contents into a HashMap - let substances: HashMap<String, Value> = serde_json::from_reader(reader)?; + let substances: HashMap<String, Value> = serde_json::from_str(json_content)?; Ok(substances) } @@ -281,7 +304,7 @@ pub fn edit_ingestion_entry(conn: &sqlite::Connection) -> Result<(), Box<dyn Err // Create a new entry with the original values as defaults // Load substances from JSON file for selection - let substances = read_substances_from_file("drugs.json")?; + let substances = read_substances_from_file()?; // let substances = match read_substances_from_file("drugs_example.json") { // Ok(s) => s, // Err(_) => {