db path creation; command loop
This commit is contained in:
parent
7aadf98efe
commit
006bd2a4da
5 changed files with 64 additions and 16 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -225,7 +225,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "meowlog2"
|
name = "meowlog"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "meowlog2"
|
name = "meowlog"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use crate::{IngestionEntry, Unit};
|
use crate::{IngestionEntry, Unit, util::get_data_path};
|
||||||
use sqlite::{Connection, Result};
|
use sqlite::{Connection, Result};
|
||||||
|
|
||||||
pub fn init_db_conn() -> Result<Connection> {
|
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 {
|
match conn {
|
||||||
Ok(c) => Ok(c),
|
Ok(c) => Ok(c),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
33
src/main.rs
33
src/main.rs
|
@ -2,10 +2,12 @@ use chrono::{DateTime, Local};
|
||||||
use db::create_db;
|
use db::create_db;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::process::exit;
|
||||||
use strum::EnumIter;
|
use strum::EnumIter;
|
||||||
use strum::IntoEnumIterator;
|
use strum::IntoEnumIterator;
|
||||||
use strum_macros::Display;
|
use strum_macros::Display;
|
||||||
use util::gather_ingestion_data;
|
use util::gather_ingestion_data;
|
||||||
|
use util::initialize_app_directory;
|
||||||
|
|
||||||
mod util;
|
mod util;
|
||||||
use util::read_substances_from_file;
|
use util::read_substances_from_file;
|
||||||
|
@ -13,12 +15,26 @@ use util::read_substances_from_file;
|
||||||
mod db;
|
mod db;
|
||||||
use db::init_db_conn;
|
use db::init_db_conn;
|
||||||
|
|
||||||
#[derive(Debug, Display, Serialize, Deserialize, EnumIter)]
|
#[derive(Debug, Serialize, Deserialize, EnumIter)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
AddIngestion,
|
AddIngestion,
|
||||||
EditIngestion,
|
EditIngestion,
|
||||||
ListIngestions,
|
ListIngestions,
|
||||||
DeleteIngestion,
|
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)]
|
#[derive(Debug, Clone, Display, Serialize, Deserialize, EnumIter)]
|
||||||
|
@ -46,7 +62,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
println!("=======");
|
println!("=======");
|
||||||
|
|
||||||
// Load substances from JSON file
|
// Load substances from JSON file
|
||||||
let substances = match read_substances_from_file("drugs.json") {
|
let substances = match read_substances_from_file() {
|
||||||
Ok(s) => s,
|
Ok(s) => s,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("Error loading substances file: {}", e);
|
eprintln!("Error loading substances file: {}", e);
|
||||||
|
@ -54,15 +70,24 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let _ = initialize_app_directory()?;
|
||||||
let _ = create_db();
|
let _ = create_db();
|
||||||
|
|
||||||
println!("Successfully loaded {} substances", substances.len());
|
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 =
|
let command =
|
||||||
inquire::Select::new("What do you want to do?", Command::iter().collect()).prompt()?;
|
inquire::Select::new("What do you want to do?", Command::iter().collect()).prompt()?;
|
||||||
match command {
|
match command {
|
||||||
Command::AddIngestion => {
|
Command::AddIngestion => {
|
||||||
let ingestion = gather_ingestion_data(substances)?;
|
let ingestion = gather_ingestion_data(substances.clone())?;
|
||||||
|
|
||||||
// Display the recorded information
|
// Display the recorded information
|
||||||
println!("\nRecorded Information:");
|
println!("\nRecorded Information:");
|
||||||
|
@ -110,7 +135,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||||
util::delete_ingestion_entries(&init_db_conn()?)?;
|
util::delete_ingestion_entries(&init_db_conn()?)?;
|
||||||
}
|
}
|
||||||
Command::EditIngestion => util::edit_ingestion_entry(&init_db_conn()?)?,
|
Command::EditIngestion => util::edit_ingestion_entry(&init_db_conn()?)?,
|
||||||
|
Command::Quit => exit(0),
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
39
src/util.rs
39
src/util.rs
|
@ -2,10 +2,13 @@ use chrono::TimeZone;
|
||||||
use inquire::{CustomType, DateSelect, Select};
|
use inquire::{CustomType, DateSelect, Select};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::env;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
|
use std::fs;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::path::PathBuf;
|
||||||
use strum::IntoEnumIterator;
|
use strum::IntoEnumIterator;
|
||||||
|
|
||||||
use crate::{IngestionEntry, Unit, db::init_db_conn};
|
use crate::{IngestionEntry, Unit, db::init_db_conn};
|
||||||
|
@ -16,15 +19,35 @@ pub struct IngestionResponse {
|
||||||
pub data: Value,
|
pub data: Value,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_substances_from_file<P: AsRef<Path>>(
|
pub fn get_data_path() -> Result<PathBuf, Box<dyn Error>> {
|
||||||
path: P,
|
let home_dir = match env::var("HOME") {
|
||||||
) -> Result<HashMap<String, Value>, Box<dyn Error>> {
|
Ok(path) => PathBuf::from(path),
|
||||||
// Open the file in read-only mode
|
Err(_) => return Err("Could not determine home directory".into()),
|
||||||
let file = File::open(path)?;
|
};
|
||||||
let reader = BufReader::new(file);
|
|
||||||
|
// 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
|
// 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)
|
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
|
// Create a new entry with the original values as defaults
|
||||||
// Load substances from JSON file for selection
|
// 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") {
|
// let substances = match read_substances_from_file("drugs_example.json") {
|
||||||
// Ok(s) => s,
|
// Ok(s) => s,
|
||||||
// Err(_) => {
|
// Err(_) => {
|
||||||
|
|
Loading…
Add table
Reference in a new issue