From 51ad2bf67e0e8cc9544aef9ca3b7b3b47fb9f4fe Mon Sep 17 00:00:00 2001 From: xqtc Date: Fri, 11 Oct 2024 20:44:11 +0200 Subject: [PATCH] Cleanup ingestions --- Cargo.lock | 185 ----------------------------------------- Cargo.toml | 2 - src/ingestions.rs | 92 ++++++-------------- src/ingestions_util.rs | 87 +++++++++++++++++++ src/main.rs | 41 ++++----- 5 files changed, 129 insertions(+), 278 deletions(-) create mode 100644 src/ingestions_util.rs diff --git a/Cargo.lock b/Cargo.lock index 6ae7f80..f56b3c2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -102,12 +102,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "base64" -version = "0.21.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" - [[package]] name = "bincode" version = "1.3.3" @@ -128,9 +122,6 @@ name = "bitflags" version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" -dependencies = [ - "serde", -] [[package]] name = "bumpalo" @@ -150,8 +141,6 @@ version = "1.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e80e3b6a3ab07840e1cae9b0666a63970dc28e8ed5ffbcdacbfc760c281bfc1" dependencies = [ - "jobserver", - "libc", "shlex", ] @@ -302,15 +291,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "form_urlencoded" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" -dependencies = [ - "percent-encoding", -] - [[package]] name = "fuzzy-matcher" version = "0.3.7" @@ -346,21 +326,6 @@ version = "0.28.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" -[[package]] -name = "git2" -version = "0.19.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b903b73e45dc0c6c596f2d37eccece7c1c8bb6e4407b001096387c63d0d93724" -dependencies = [ - "bitflags 2.6.0", - "libc", - "libgit2-sys", - "log", - "openssl-probe", - "openssl-sys", - "url", -] - [[package]] name = "hashbrown" version = "0.15.0" @@ -396,16 +361,6 @@ dependencies = [ "cc", ] -[[package]] -name = "idna" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" -dependencies = [ - "unicode-bidi", - "unicode-normalization", -] - [[package]] name = "indenter" version = "0.3.3" @@ -445,15 +400,6 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" -[[package]] -name = "jobserver" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48d1dbcbbeb6a7fec7e059840aa538bd62aaccf972c7346c4d9d2059312853d0" -dependencies = [ - "libc", -] - [[package]] name = "js-sys" version = "0.3.70" @@ -475,46 +421,6 @@ version = "0.2.159" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" -[[package]] -name = "libgit2-sys" -version = "0.17.0+1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10472326a8a6477c3c20a64547b0059e4b0d086869eee31e6d7da728a8eb7224" -dependencies = [ - "cc", - "libc", - "libssh2-sys", - "libz-sys", - "openssl-sys", - "pkg-config", -] - -[[package]] -name = "libssh2-sys" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dc8a030b787e2119a731f1951d6a773e2280c660f8ec4b0f5e1505a386e71ee" -dependencies = [ - "cc", - "libc", - "libz-sys", - "openssl-sys", - "pkg-config", - "vcpkg", -] - -[[package]] -name = "libz-sys" -version = "1.1.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2d16453e800a8cf6dd2fc3eb4bc99b786a9b90c663b8559a5b1a041bf89e472" -dependencies = [ - "cc", - "libc", - "pkg-config", - "vcpkg", -] - [[package]] name = "lock_api" version = "0.4.12" @@ -545,10 +451,8 @@ dependencies = [ "chrono", "clap", "color-eyre", - "git2", "inquire", "lazy_static", - "ron", "serde", "strum", "strum_macros", @@ -610,24 +514,6 @@ version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" -[[package]] -name = "openssl-probe" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" - -[[package]] -name = "openssl-sys" -version = "0.9.103" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f9e8deee91df40a943c71b917e5874b951d32a802526c85721ce3b776c929d6" -dependencies = [ - "cc", - "libc", - "pkg-config", - "vcpkg", -] - [[package]] name = "owo-colors" version = "3.5.0" @@ -657,24 +543,12 @@ dependencies = [ "windows-targets 0.52.6", ] -[[package]] -name = "percent-encoding" -version = "2.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" - [[package]] name = "pin-project-lite" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" -[[package]] -name = "pkg-config" -version = "0.3.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" - [[package]] name = "proc-macro2" version = "1.0.87" @@ -702,18 +576,6 @@ dependencies = [ "bitflags 2.6.0", ] -[[package]] -name = "ron" -version = "0.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94" -dependencies = [ - "base64", - "bitflags 2.6.0", - "serde", - "serde_derive", -] - [[package]] name = "rustc-demangle" version = "0.1.24" @@ -861,21 +723,6 @@ dependencies = [ "once_cell", ] -[[package]] -name = "tinyvec" -version = "1.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" -dependencies = [ - "tinyvec_macros", -] - -[[package]] -name = "tinyvec_macros" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" - [[package]] name = "toml" version = "0.8.19" @@ -951,27 +798,12 @@ dependencies = [ "tracing-core", ] -[[package]] -name = "unicode-bidi" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ab17db44d7388991a428b2ee655ce0c212e862eff1768a455c58f9aad6e7893" - [[package]] name = "unicode-ident" version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" -[[package]] -name = "unicode-normalization" -version = "0.1.24" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5033c97c4262335cded6d6fc3e5c18ab755e1a3dc96376350f3d8e9f009ad956" -dependencies = [ - "tinyvec", -] - [[package]] name = "unicode-segmentation" version = "1.12.0" @@ -984,17 +816,6 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" -[[package]] -name = "url" -version = "2.5.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" -dependencies = [ - "form_urlencoded", - "idna", - "percent-encoding", -] - [[package]] name = "utf8parse" version = "0.2.2" @@ -1017,12 +838,6 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" -[[package]] -name = "vcpkg" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" - [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 3d7f306..1c76adb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,8 @@ bincode = "1.3.3" chrono = { version = "0.4.38", features = ["serde"] } clap = { version = "4.5.20", features = ["derive"] } color-eyre = "0.6.3" -git2 = "0.19.0" inquire = "0.7.5" lazy_static = "1.5.0" -ron = "0.8.1" serde = { version = "1.0.210", features = ["derive"] } strum = { version = "0.26.3", features = ["derive"] } strum_macros = "0.26.4" diff --git a/src/ingestions.rs b/src/ingestions.rs index 9e3d16a..811d6e2 100644 --- a/src/ingestions.rs +++ b/src/ingestions.rs @@ -1,3 +1,7 @@ +use crate::ingestions_util::{ + self, ensure_ingestion_files, get_dose_unit, get_ingestion_confirmation, get_ingestion_method, + get_substance, get_user_datetime, +}; use chrono::{NaiveDateTime, Utc}; use color_eyre::Section; use inquire; @@ -9,17 +13,17 @@ use uuid::Uuid; use crate::{config::INGESTIONS_FILE, substances::Substance}; #[derive(Serialize, Deserialize, Debug, Clone)] -struct Ingestion { - substance: String, - dose: Dose, - ingest_method: IngestionMethod, - time: NaiveDateTime, +pub struct Ingestion { + pub substance: String, + pub dose: Dose, + pub ingestion_method: IngestionMethod, + pub time: NaiveDateTime, } #[derive(Serialize, Deserialize, Debug, Clone)] -struct Dose { - unit: String, - value: f64, +pub struct Dose { + pub unit: String, + pub value: f64, } #[derive(Serialize, Deserialize, Debug, strum::Display, strum::EnumIter)] @@ -47,77 +51,29 @@ pub enum IngestionMethod { } pub fn add_ingestion() { - let mut ingesstions_bytes_loaded_des: HashMap; - if crate::substances::path_exists(INGESTIONS_FILE.to_string()) { - let substances_bytes_loaded = std::fs::read(INGESTIONS_FILE.to_string()).unwrap(); - ingesstions_bytes_loaded_des = bincode::deserialize(&substances_bytes_loaded).unwrap(); - } else { - std::fs::File::create(INGESTIONS_FILE.to_string()).unwrap(); - ingesstions_bytes_loaded_des = HashMap::new(); - let ingesstions_bytes_loaded_ser = - bincode::serialize(&ingesstions_bytes_loaded_des).unwrap(); - std::fs::write(INGESTIONS_FILE.to_string(), ingesstions_bytes_loaded_ser).unwrap(); - } - let substances = crate::substances::substances_to_vec(); - if substances.is_empty() { - eprintln!("Add a substance before you log an ingestions"); - exit(1) - } - let substance = inquire::Select::new("What did yout ingest?", substances) - .prompt() - .unwrap(); - let ingestion_method_select = inquire::Select::new( - "How did you ingest?", - IngestionMethod::iter().collect::>(), - ) - .prompt() - .unwrap(); + let mut ingesstions_bytes_loaded_des: HashMap = ensure_ingestion_files(); - dbg!(&substance); - dbg!(&ingestion_method_select); - let current_time = Utc::now().naive_utc(); + let substance = get_substance(); - let date_time: chrono::NaiveDateTime = inquire::CustomType::::new( - "Enter the date and time (YYYY-MM-DD HH:MM):", - ) - .with_placeholder("YYYY-MM-DD HH:MM") - .with_default(current_time) - .with_parser(&|input| { - chrono::NaiveDateTime::parse_from_str(input, "%Y-%m-%d %H:%M").map_err(|_| ()) - }) - .with_error_message("Please enter a valid date and time in the format YYYY-MM-DD HH:MM.") - .with_help_message("Use the format YYYY-MM-DD HH:MM") - .prompt() - .unwrap(); + let ingestion_method = get_ingestion_method(); + + let time: NaiveDateTime = get_user_datetime(); let dose_num: f64 = inquire::prompt_f64("Enter the amount consumed:").unwrap(); - let dose_unit = inquire::Select::new( - "What unit should be used?", - DoseUnit::iter().collect::>(), - ) - .prompt() - .unwrap(); + let dose_unit: DoseUnit = get_dose_unit(); let dose = Dose { unit: dose_unit.to_string(), value: dose_num, }; + let ingestion = Ingestion { substance, dose, - ingest_method: ingestion_method_select, - time: date_time, + ingestion_method, + time, }; - println!( - "Substance: {} ({})\nDose: {}{}\nTime: {}\n", - ingestion.substance, - ingestion.ingest_method, - ingestion.dose.value, - ingestion.dose.unit, - ingestion.time, - ); - let confirm = - inquire::prompt_confirmation("Does the ingestion above look alright? [y/N]").unwrap(); - dbg!(&confirm); + + let confirm = get_ingestion_confirmation(ingestion.clone()); if confirm { ingesstions_bytes_loaded_des.insert(Uuid::new_v4(), ingestion.clone()); let ingestion_ser = bincode::serialize(&ingesstions_bytes_loaded_des).unwrap(); @@ -134,7 +90,7 @@ pub fn list_ingestions() -> Result<(), std::io::Error> { println!( "Substance: {} ({})\nDose: {}{}\nTime: {}\nUUID: {:?}\n", ingestion.substance, - ingestion.ingest_method, + ingestion.ingestion_method, ingestion.dose.value, ingestion.dose.unit, ingestion.time, diff --git a/src/ingestions_util.rs b/src/ingestions_util.rs new file mode 100644 index 0000000..40730f4 --- /dev/null +++ b/src/ingestions_util.rs @@ -0,0 +1,87 @@ +use crate::ingestions::{DoseUnit, Ingestion, IngestionMethod}; +use crate::{config::INGESTIONS_FILE, substances::Substance}; +use chrono::{NaiveDateTime, Utc}; +use color_eyre::Section; +use inquire; +use serde::{self, Deserialize, Serialize}; +use std::{collections::HashMap, process::exit}; +use strum::{EnumIter, IntoEnumIterator}; +use uuid::Uuid; + +pub fn ensure_ingestion_files() -> HashMap { + let mut ingesstions_bytes_loaded_des: HashMap; + if crate::substances::path_exists(INGESTIONS_FILE.to_string()) { + let substances_bytes_loaded = std::fs::read(INGESTIONS_FILE.to_string()).unwrap(); + ingesstions_bytes_loaded_des = bincode::deserialize(&substances_bytes_loaded).unwrap(); + } else { + std::fs::File::create(INGESTIONS_FILE.to_string()).unwrap(); + ingesstions_bytes_loaded_des = HashMap::new(); + let ingesstions_bytes_loaded_ser = + bincode::serialize(&ingesstions_bytes_loaded_des).unwrap(); + std::fs::write(INGESTIONS_FILE.to_string(), ingesstions_bytes_loaded_ser).unwrap(); + } + ingesstions_bytes_loaded_des +} + +pub fn get_user_datetime() -> chrono::NaiveDateTime { + let current_time = Utc::now().naive_utc(); + let date_time: chrono::NaiveDateTime = inquire::CustomType::::new( + "Enter the date and time (YYYY-MM-DD HH:MM):", + ) + .with_placeholder("YYYY-MM-DD HH:MM") + .with_default(current_time) + .with_parser(&|input| { + chrono::NaiveDateTime::parse_from_str(input, "%Y-%m-%d %H:%M").map_err(|_| ()) + }) + .with_error_message("Please enter a valid date and time in the format YYYY-MM-DD HH:MM.") + .with_help_message("Use the format YYYY-MM-DD HH:MM") + .prompt() + .unwrap(); + date_time +} + +pub fn get_dose_unit() -> DoseUnit { + let dose_unit = inquire::Select::new( + "What unit should be used?", + DoseUnit::iter().collect::>(), + ) + .prompt() + .unwrap(); + dose_unit +} + +pub fn get_substance() -> String { + let substances = crate::substances::substances_to_vec(); + if substances.is_empty() { + eprintln!("Add a substance before you log an ingestions"); + exit(1) + } + let substance = inquire::Select::new("What did yout ingest?", substances) + .prompt() + .unwrap(); + substance +} + +pub fn get_ingestion_method() -> IngestionMethod { + let ingestion_method = inquire::Select::new( + "How did you ingest?", + IngestionMethod::iter().collect::>(), + ) + .prompt() + .unwrap(); + ingestion_method +} + +pub fn get_ingestion_confirmation(ingestion: Ingestion) -> bool { + println!( + "Substance: {} ({})\nDose: {}{}\nTime: {}\n", + ingestion.substance, + ingestion.ingestion_method, + ingestion.dose.value, + ingestion.dose.unit, + ingestion.time, + ); + let confirm = + inquire::prompt_confirmation("Does the ingestion above look alright? [y/N]").unwrap(); + confirm +} diff --git a/src/main.rs b/src/main.rs index bfb7820..ce3066b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,12 @@ use core::panic; -use std::{collections::HashMap, path::Path}; -use bincode::serialize; -use chrono::Utc; use clap::{Parser, Subcommand}; use config::{INGESTIONS_FILE, LOCAL_PATH, SUBSTANCES_FILE}; -use git2; -use inquire; -use serde::{self, Deserialize, Serialize}; -use strum::{EnumIter, IntoEnumIterator}; -use uuid::Uuid; mod config; mod ingestions; +mod ingestions_util; mod substances; #[derive(Parser)] @@ -53,8 +46,23 @@ enum Commands { } fn main() { - // let home = std::env::var("HOME").unwrap(); - // let local_path = format!("{}/.local/share/meowlog", &home); + ensure_files(); + let cli = Cli::parse(); + + match &cli.command { + Some(Commands::AddIngestion) => ingestions::add_ingestion(), + Some(Commands::EditIngestion) => {} + Some(Commands::ListIngestions) => ingestions::list_ingestions().unwrap(), + Some(Commands::RemoveIngestion) => {} + Some(Commands::AddSubstance) => substances::add_substance().unwrap(), + Some(Commands::EditSubstance) => {} + Some(Commands::ListSubstances) => substances::list_substances().unwrap(), + Some(Commands::RemoveSubstance) => {} + None => {} + } +} + +fn ensure_files() { if !substances::path_exists(LOCAL_PATH.to_string()) { match std::fs::create_dir(LOCAL_PATH.to_string()) { Ok(_) => {} @@ -92,17 +100,4 @@ fn main() { } }; } - let cli = Cli::parse(); - - match &cli.command { - Some(Commands::AddIngestion) => ingestions::add_ingestion(), - Some(Commands::EditIngestion) => {} - Some(Commands::ListIngestions) => ingestions::list_ingestions().unwrap(), - Some(Commands::RemoveIngestion) => {} - Some(Commands::AddSubstance) => substances::add_substance().unwrap(), - Some(Commands::EditSubstance) => {} - Some(Commands::ListSubstances) => substances::list_substances().unwrap(), - Some(Commands::RemoveSubstance) => {} - None => {} - } }