diff --git a/Cargo.lock b/Cargo.lock index 21d3e35..3aee18f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -229,18 +229,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" -[[package]] -name = "fraction" -version = "0.15.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f158e3ff0a1b334408dc9fb811cd99b446986f4d8b741bb08f9df1604085ae7" -dependencies = [ - "lazy_static", - "num", - "serde", - "serde_derive", -] - [[package]] name = "getrandom" version = "0.2.15" @@ -306,20 +294,20 @@ checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" [[package]] name = "itoa" -version = "1.0.12" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a73e9fe3c49d7afb2ace819fa181a287ce54a0983eda4e0eb05c22f82ffe534" +checksum = "540654e97a3f4470a492cd30ff187bc95d89557a903a2bbf112e2fae98104ef2" [[package]] name = "journal" version = "0.1.0" dependencies = [ "chrono", - "fraction", "psychonaut_journal_types", "rand", "serde", "serde_json", + "thiserror 2.0.3", ] [[package]] @@ -354,9 +342,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.162" +version = "0.2.164" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18d287de67fe55fd7e1581fe933d965a5a9477b38e949cfa9f8574ef01506398" +checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" [[package]] name = "libredox" @@ -380,73 +368,6 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" -[[package]] -name = "num" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35bd024e8b2ff75562e5f34e7f4905839deb4b22955ef5e73d2fea1b9813cb23" -dependencies = [ - "num-bigint", - "num-complex", - "num-integer", - "num-iter", - "num-rational", - "num-traits", -] - -[[package]] -name = "num-bigint" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" -dependencies = [ - "num-integer", - "num-traits", - "serde", -] - -[[package]] -name = "num-complex" -version = "0.4.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" -dependencies = [ - "num-traits", - "serde", -] - -[[package]] -name = "num-integer" -version = "0.1.46" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" -dependencies = [ - "num-traits", -] - -[[package]] -name = "num-iter" -version = "0.1.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1429034a0490724d0075ebb2bc9e875d6503c3cf69e235a8941aa757d83ef5bf" -dependencies = [ - "autocfg", - "num-integer", - "num-traits", -] - -[[package]] -name = "num-rational" -version = "0.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f83d14da390562dca69fc84082e73e548e1ad308d24accdedd2720017cb37824" -dependencies = [ - "num-bigint", - "num-integer", - "num-traits", - "serde", -] - [[package]] name = "num-traits" version = "0.2.19" @@ -487,9 +408,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.89" +version = "1.0.92" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" dependencies = [ "unicode-ident", ] @@ -548,7 +469,7 @@ checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" dependencies = [ "getrandom", "libredox", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -609,9 +530,9 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "2.0.87" +version = "2.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" +checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" dependencies = [ "proc-macro2", "quote", @@ -635,7 +556,16 @@ version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" dependencies = [ - "thiserror-impl", + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa" +dependencies = [ + "thiserror-impl 2.0.3", ] [[package]] @@ -650,10 +580,21 @@ dependencies = [ ] [[package]] -name = "unicode-ident" -version = "1.0.13" +name = "thiserror-impl" +version = "2.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" +checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" [[package]] name = "unicode-width" diff --git a/journal/Cargo.lock b/journal/Cargo.lock deleted file mode 100644 index f2b8669..0000000 --- a/journal/Cargo.lock +++ /dev/null @@ -1,330 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "android-tzdata" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" - -[[package]] -name = "android_system_properties" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" -dependencies = [ - "libc", -] - -[[package]] -name = "autocfg" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" - -[[package]] -name = "bumpalo" -version = "3.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" - -[[package]] -name = "cc" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" -dependencies = [ - "shlex", -] - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "chrono" -version = "0.4.38" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" -dependencies = [ - "android-tzdata", - "iana-time-zone", - "js-sys", - "num-traits", - "serde", - "wasm-bindgen", - "windows-targets", -] - -[[package]] -name = "core-foundation-sys" -version = "0.8.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" - -[[package]] -name = "iana-time-zone" -version = "0.1.61" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "windows-core", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" -dependencies = [ - "cc", -] - -[[package]] -name = "journal" -version = "0.1.0" -dependencies = [ - "chrono", - "psychonaut_journal_types", - "serde", -] - -[[package]] -name = "js-sys" -version = "0.3.72" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" -dependencies = [ - "wasm-bindgen", -] - -[[package]] -name = "libc" -version = "0.2.164" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f" - -[[package]] -name = "log" -version = "0.4.22" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" - -[[package]] -name = "num-traits" -version = "0.2.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" -dependencies = [ - "autocfg", -] - -[[package]] -name = "once_cell" -version = "1.20.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" - -[[package]] -name = "proc-macro2" -version = "1.0.89" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "psychonaut_journal_types" -version = "0.1.0" -dependencies = [ - "serde", -] - -[[package]] -name = "quote" -version = "1.0.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "serde" -version = "1.0.215" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.215" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "shlex" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" - -[[package]] -name = "syn" -version = "2.0.87" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "unicode-ident" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" - -[[package]] -name = "wasm-bindgen" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" -dependencies = [ - "cfg-if", - "once_cell", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" -dependencies = [ - "bumpalo", - "log", - "once_cell", - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.95" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" - -[[package]] -name = "windows-core" -version = "0.52.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/journal/Cargo.toml b/journal/Cargo.toml index b4b6dd9..f5a0f6c 100644 --- a/journal/Cargo.toml +++ b/journal/Cargo.toml @@ -4,9 +4,13 @@ version = "0.1.0" edition = "2021" [dependencies] -serde = { version = "1.0.215", features = ["std", "derive", "serde_derive"] } -serde_json = "1.0.132" +serde = { version = "1.0.215", features = ["std", "derive"] } +serde_json = { version = "1.0.132", optional = true } chrono = { version = "0.4.38", features = ["serde"] } psychonaut_journal_types = { path = "../psychonaut_journal_types" } rand = "0.8.5" -fraction = { version = "0.15.3", features = ["with-serde-support"] } +thiserror = "2.0.3" + +[features] +default = [ "json_journal" ] +json_journal = [ "dep:serde_json" ] \ No newline at end of file diff --git a/journal/src/journal/json_journal.rs b/journal/src/journal/json_journal.rs index ea21989..d18e92c 100644 --- a/journal/src/journal/json_journal.rs +++ b/journal/src/journal/json_journal.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, fs::File}; +use std::fs::File; use chrono::Utc; use rand::Rng; @@ -10,28 +10,29 @@ use crate::types::{ use super::{Journal, JournalType}; -#[derive(Debug, Default, Clone)] -#[derive(serde::Serialize, serde::Deserialize)] -pub struct JSONJournalData { - pub sustenances: HashMap, - pub substances: HashMap, - pub ingestions: HashMap>, - pub sessions: HashMap, - pub custom_units: HashMap, -} +mod data; +use data::JSONJournalData; + +mod options; +pub use options::JSONJournalOptions; #[derive(Debug, Default)] pub struct JSONJournal { data: JSONJournalData, filename: String, + options: JSONJournalOptions, } impl JSONJournal { - pub fn create(filename: &str) -> Result> { + pub fn create( + filename: &str, + options: JSONJournalOptions, + ) -> Result> { let data: JSONJournalData = JSONJournalData::default(); - let journal = JSONJournal { + let mut journal = JSONJournal { data, + options, filename: filename.to_string(), }; @@ -40,7 +41,10 @@ impl JSONJournal { Ok(Box::new(journal)) } - pub fn load(filename: &str) -> Result> { + pub fn load( + filename: &str, + options: JSONJournalOptions, + ) -> Result> { let file = File::open(filename)?; let data: JSONJournalData = serde_json::from_reader(file)?; @@ -48,13 +52,14 @@ impl JSONJournal { let journal = JSONJournal { data, + options, filename: filename.to_string(), }; Ok(Box::new(journal)) } - pub fn save(&self) -> Result<(), Box> { + fn save(&mut self) -> Result<(), Box> { let file = File::create(self.filename.clone())?; serde_json::to_writer_pretty(&file, &self.data)?; @@ -63,7 +68,7 @@ impl JSONJournal { } impl Journal for JSONJournal { - fn save(&self) -> Result<(), Box> { + fn save(&mut self) -> Result<(), Box> { self.save() } @@ -83,7 +88,15 @@ impl Journal for JSONJournal { self.data.sustenances.get(sustenance_id).cloned() } - fn get_ingestion_kinds(&self, kind: &IngestionKind, calculate: bool) -> Option { + fn get_custom_unit(&self, id: i32) -> Option { + self.data.custom_units.get(&id).cloned() + } + + fn resolve_ingestion_kind( + &self, + kind: &IngestionKind, + calculate: bool, + ) -> Option { println!("{kind:#?}"); let mut kinds: IngestionKinds = IngestionKinds::from(kind); @@ -94,28 +107,28 @@ impl Journal for JSONJournal { if calculate { match include { IngestionKind::Sustenance(ingestion) => { - kinds.ingestions.push(IngestionKinds { + kinds.includes.push(IngestionKinds { ingestion: IngestionKind::Sustenance(SustenanceIngestion { amount: sustenance.amount * ingestion.amount, ..ingestion.clone() }), - ingestions: self - .get_ingestion_kinds( + includes: self + .resolve_ingestion_kind( &IngestionKind::Sustenance(ingestion), calculate, ) .unwrap() - .ingestions, + .includes, }); } IngestionKind::Unknown => { kinds - .ingestions + .includes .push(IngestionKinds::from(&IngestionKind::Unknown)); } IngestionKind::Substance(ingestion) => { kinds - .ingestions + .includes .push(IngestionKinds::from(&IngestionKind::Substance( SubstanceIngestion { dose: Dose::new_precise(sustenance.amount) @@ -125,15 +138,15 @@ impl Journal for JSONJournal { ))); } IngestionKind::Hydration { amount } => { - kinds.ingestions.push(IngestionKinds::from( - &IngestionKind::Hydration { + kinds + .includes + .push(IngestionKinds::from(&IngestionKind::Hydration { amount: (sustenance.amount * amount as f64).round() as u64, - }, - )); + })); } } } else { - kinds.ingestions.push(IngestionKinds::from(&include)) + kinds.includes.push(IngestionKinds::from(&include)) } } } @@ -141,8 +154,14 @@ impl Journal for JSONJournal { Some(kinds) } - fn get_custom_unit(&self, id: i32) -> Option { - self.data.custom_units.get(&id).cloned() + fn resolve_substance_unit(&self, ingestion: &SubstanceIngestion) -> Option { + match ingestion.custom_unit_id { + None => match self.get_substance(&ingestion.substance_name) { + Some(substance) => Some(Unit::Simple(substance.unit)), + None => None, + }, + Some(custom_unit_id) => self.get_custom_unit(custom_unit_id).map(Unit::Custom), + } } fn create_session(&mut self, title: &str) -> Session { @@ -233,14 +252,4 @@ impl Journal for JSONJournal { .find(|&session| session.title == title) .cloned() } - - fn resolve_substance_unit(&self, ingestion: &SubstanceIngestion) -> Option { - match ingestion.custom_unit_id { - None => match self.get_substance(&ingestion.substance_name) { - Some(substance) => Some(Unit::Simple(substance.unit)), - None => None, - }, - Some(custom_unit_id) => self.get_custom_unit(custom_unit_id).map(Unit::Custom), - } - } } diff --git a/journal/src/journal/json_journal/data.rs b/journal/src/journal/json_journal/data.rs new file mode 100644 index 0000000..23987a0 --- /dev/null +++ b/journal/src/journal/json_journal/data.rs @@ -0,0 +1,13 @@ +use std::collections::HashMap; + +use crate::types::{CustomUnit, Ingestion, Session, Substance, Sustenance}; + +#[derive(Debug, Default, Clone)] +#[derive(serde::Serialize, serde::Deserialize)] +pub struct JSONJournalData { + pub sustenances: HashMap, + pub substances: HashMap, + pub ingestions: HashMap>, + pub sessions: HashMap, + pub custom_units: HashMap, +} diff --git a/journal/src/journal/json_journal/options.rs b/journal/src/journal/json_journal/options.rs new file mode 100644 index 0000000..86b9a21 --- /dev/null +++ b/journal/src/journal/json_journal/options.rs @@ -0,0 +1,2 @@ +#[derive(Debug, Default)] +pub struct JSONJournalOptions {} diff --git a/journal/src/journal.rs b/journal/src/journal/mod.rs similarity index 85% rename from journal/src/journal.rs rename to journal/src/journal/mod.rs index c559ec3..c1349c9 100644 --- a/journal/src/journal.rs +++ b/journal/src/journal/mod.rs @@ -5,19 +5,27 @@ use crate::types::{ pub type JournalType = Box; +#[derive(thiserror::Error, Debug)] +pub enum JournalError { + #[error("unknown")] + Unknown, +} + pub trait Journal { - fn save(&self) -> Result<(), Box>; + fn save(&mut self) -> Result<(), Box>; fn get_session(&self, id: i32) -> Option; fn get_session_ingestions(&self, id: i32) -> Option>; fn get_substance(&self, name: &str) -> Option; fn get_sustenance(&self, id: &str) -> Option; - fn get_ingestion_kinds( + fn get_custom_unit(&self, id: i32) -> Option; + + fn resolve_ingestion_kind( &self, ingestion: &IngestionKind, calculate: bool, ) -> Option; - fn get_custom_unit(&self, id: i32) -> Option; + fn resolve_substance_unit(&self, ingestion: &SubstanceIngestion) -> Option; fn create_session(&mut self, title: &str) -> Session; fn create_substance(&mut self, substance: Substance) -> Substance; @@ -32,8 +40,9 @@ pub trait Journal { fn set_session_ingestions(&mut self, session_id: i32, ingestions: Vec); fn first_session_by_title(&self, title: &str) -> Option; - fn resolve_substance_unit(&self, ingestion: &SubstanceIngestion) -> Option; } +#[cfg(feature = "json_journal")] mod json_journal; +#[cfg(feature = "json_journal")] pub use json_journal::JSONJournal; diff --git a/journal/src/lib.rs b/journal/src/lib.rs index 225befb..f158672 100644 --- a/journal/src/lib.rs +++ b/journal/src/lib.rs @@ -1,4 +1,2 @@ pub mod journal; pub mod types; - -pub use journal::JSONJournal; diff --git a/journal/src/types/dose/add.rs b/journal/src/types/dose/add.rs index 3bcb612..4e79894 100644 --- a/journal/src/types/dose/add.rs +++ b/journal/src/types/dose/add.rs @@ -46,15 +46,15 @@ impl Add<&Dose> for Dose { } }, contains_unknown: { - match ( - self.contains_unknown, - rhs.contains_unknown, - self.value, - rhs.value, - ) { - (true, _, _, _) | (_, true, _, _) | (_, _, None, _) | (_, _, _, None) => true, - _ => false, - } + matches!( + ( + self.contains_unknown, + rhs.contains_unknown, + self.value, + rhs.value, + ), + (true, _, _, _) | (_, true, _, _) | (_, _, None, _) | (_, _, _, None) + ) }, } } diff --git a/journal/src/types/dose/multiply.rs b/journal/src/types/dose/multiply.rs index e3a4d0a..3188ba3 100644 --- a/journal/src/types/dose/multiply.rs +++ b/journal/src/types/dose/multiply.rs @@ -68,15 +68,15 @@ impl Mul<&Dose> for Dose { } }, contains_unknown: { - match ( - self.contains_unknown, - rhs.contains_unknown, - self.value, - rhs.value, - ) { - (true, _, _, _) | (_, true, _, _) | (_, _, None, _) | (_, _, _, None) => true, - _ => false, - } + matches!( + ( + self.contains_unknown, + rhs.contains_unknown, + self.value, + rhs.value, + ), + (true, _, _, _) | (_, true, _, _) | (_, _, None, _) | (_, _, _, None) + ) }, } } diff --git a/journal/src/types/ingestion/kind.rs b/journal/src/types/ingestion/kind.rs index 9249b0d..12cb068 100644 --- a/journal/src/types/ingestion/kind.rs +++ b/journal/src/types/ingestion/kind.rs @@ -17,7 +17,7 @@ pub enum IngestionKind { #[derive(serde::Serialize, serde::Deserialize)] pub struct IngestionKinds { pub ingestion: IngestionKind, - pub ingestions: Vec, + pub includes: Vec, } impl From<&IngestionKind> for IngestionKinds { diff --git a/journal_cli/Cargo.toml b/journal_cli/Cargo.toml index 68a5cd0..3565055 100644 --- a/journal_cli/Cargo.toml +++ b/journal_cli/Cargo.toml @@ -23,3 +23,7 @@ rand = "0.8.5" #string-error = "0.1.0" #termcolor = "1.4.1" #thiserror = "2.0.3" + +[build-dependencies] +clap = { version = "4.5.21", features = ["derive", "env"] } +clap_complete = "4.5.38" \ No newline at end of file diff --git a/journal_cli/src/args.rs b/journal_cli/src/args.rs index d5a18d8..38cb11f 100644 --- a/journal_cli/src/args.rs +++ b/journal_cli/src/args.rs @@ -12,20 +12,19 @@ pub struct Args { #[command(flatten_help = false)] pub enum Commands { Journal(commands::journal::JournalSubcommand), - PrintSession(commands::PrintSessionArgs), + Psychonaut(commands::psychonaut::PsychonautSubcommand), + PrintSession(commands::print_session::PrintSessionArgs), GenerateShellAutocompletions(GenerateShellAutocompletionslArgs), } + #[derive(Debug, Clone, clap::Args)] pub struct GenerateShellAutocompletionslArgs { pub kind: Shell, } -// We need two copies of this struct, one for the root command -// another for subcommands which require the required parts #[derive(Debug, Clone, clap::Args)] -#[group(id = "journal_location")] +#[group(id = "journal_location", args = vec!["journal_file"], required = true, multiple = false)] pub struct JournalLocation { - // journal file is writable #[arg(long, group = "journal_location", value_hint = clap::ValueHint::FilePath, env = "JOURNAL_FILE")] - pub journal_file: String, + pub journal_file: Option, } diff --git a/journal_cli/src/commands/journal/import_psychonaut.rs b/journal_cli/src/commands/journal/import_psychonaut.rs deleted file mode 100644 index ca7fa5e..0000000 --- a/journal_cli/src/commands/journal/import_psychonaut.rs +++ /dev/null @@ -1,236 +0,0 @@ -use std::{collections::HashMap, fs::File, path::Path}; - -use journal::{ - journal::{JSONJournal, Journal, JournalType}, - types::{CustomUnit, Ingestion, Session, Substance}, -}; -use rand::Rng; - -use crate::args::JournalLocation; - -#[derive(Debug, Clone, clap::Args)] -#[group(requires = "journal_location")] -pub struct JournalImportPsychonautArgs { - #[command(flatten)] - pub journal_location: JournalLocation, - - #[arg(long, value_hint = clap::ValueHint::FilePath)] - pub export_file: String, - - #[arg(long)] - pub create: bool, - #[arg(long)] - pub force_create: bool, - - #[arg(long)] - pub custom_unit_id: Option, - #[arg(long)] - pub custom_unit_name: Option, - - #[arg(long)] - pub custom_substance: Option, - - #[arg(long)] - pub infer_missing_substances: bool, -} - -#[derive(Debug, Default, Clone)] -#[derive(serde::Serialize, serde::Deserialize)] -struct Mappings { - pub substance_names: HashMap, -} - -pub fn journal_import_psychonaut( - args: &JournalImportPsychonautArgs, -) -> Result<(), Box> { - let journal_file_location = &args.journal_location.journal_file.clone(); - - let mut journal: Box = if args.create { - if Path::new(&journal_file_location).exists() && !args.force_create { - panic!("journal already exists, try --force-create if you want to recreate") - } - JSONJournal::create(journal_file_location)? - } else { - JSONJournal::load(journal_file_location)? - }; - - let export_file = File::open(args.export_file.clone())?; - let export_data: psychonaut_journal_types::ExportData = serde_json::from_reader(export_file)?; - - let mut rng = rand::thread_rng(); - - fn create_or_update_custom_unit(journal: &mut JournalType, unit: CustomUnit) { - match journal.get_custom_unit(unit.id) { - Some(custom_unit) => { - println!( - "Updating Custom Unit ID: {} Name: {}", - unit.id, custom_unit.name - ); - journal.update_custom_unit(unit.id, unit, false); - } - None => { - println!("Creating Custom Unit ID: {} Name: {}", unit.id, unit.name); - journal.update_custom_unit(unit.id, unit, true); - } - } - } - - if let Some(id) = &args.custom_unit_id { - let unit = export_data - .custom_units - .into_iter() - .find(|unit| unit.id == *id); - - match unit { - Some(unit) => { - create_or_update_custom_unit(&mut journal, CustomUnit::from(unit)); - } - None => { - panic!("Could not find custom unit with ID: {id}") - } - } - - return Ok(()); - } else if let Some(name) = &args.custom_unit_name { - let unit = export_data - .custom_units - .into_iter() - .find(|unit| unit.name == *name); - - match unit { - Some(unit) => { - create_or_update_custom_unit(&mut journal, CustomUnit::from(unit)); - } - None => { - panic!("Could not find custom unit with name: {name}") - } - } - - return Ok(()); - } else if args.custom_substance.is_none() { - for custom_unit in export_data.custom_units.into_iter() { - create_or_update_custom_unit(&mut journal, CustomUnit::from(custom_unit)); - } - } - - fn create_or_check_custom_substance( - journal: &mut JournalType, - args: &JournalImportPsychonautArgs, - custom_substance: Substance, - ) { - match journal.get_substance(&custom_substance.name) { - Some(substance) => { - println!("Checking substance units {}", custom_substance.name); - - if substance.unit != custom_substance.unit { - panic!( - "mismatch on unit for {}: {} != {} ", - custom_substance.name, substance.unit, custom_substance.unit - ) - } - } - None => { - if args.infer_missing_substances { - println!("Creating substance {}", custom_substance.name); - journal.create_substance(custom_substance); - } - } - } - } - - if let Some(substance_name) = &args.custom_substance { - let custom_substance: Option = export_data - .custom_substances - .into_iter() - .find(|substance| substance.name == *substance_name); - - match custom_substance { - Some(custom_substance) => { - create_or_check_custom_substance( - &mut journal, - args, - Substance::from(Substance { - name: custom_substance.name.clone(), - description: custom_substance.description.clone(), - unit: custom_substance.units, - }), - ); - } - None => { - panic!("Could not find substance with name: {substance_name}") - } - } - } else { - for custom_substance in export_data.custom_substances.into_iter() { - create_or_check_custom_substance( - &mut journal, - args, - Substance { - name: custom_substance.name.clone(), - description: custom_substance.description.clone(), - unit: custom_substance.units, - }, - ); - } - } - - for experience in export_data.experiences.iter() { - for ingestion in experience.ingestions.iter() { - let substance_name = &ingestion.substance_name; - - let ingestion_unit = match ingestion.custom_unit_id { - Some(id) => journal.get_custom_unit(id).unwrap().original_unit, - None => ingestion.unit.clone(), - }; - - match journal.get_substance(substance_name) { - Some(substance) => { - if substance.unit != ingestion_unit { - panic!( - "mismatch on units for {substance_name}: {} != {ingestion_unit} ", - substance.unit - ) - } - } - None => { - journal.create_substance(Substance { - name: substance_name.clone(), - description: "".to_string(), - unit: ingestion_unit, - }); - } - } - } - } - - for experience in export_data.experiences.into_iter() { - if journal.first_session_by_title(&experience.title).is_some() { - println!("Skipping {} as is already in journal", experience.title); - continue; - } else { - println!("Creating experience {}", experience.title); - } - - let session_id = rng.gen::(); - - journal.update_session( - session_id, - Session { - id: session_id, - ..Session::from(experience.clone()) - }, - true, - ); - - let mut ingestions: Vec = Vec::new(); - for ingestion in experience.ingestions.iter() { - ingestions.push(Ingestion::from(ingestion.clone())) - } - - journal.set_session_ingestions(session_id, ingestions); - } - - journal.save()?; - - Ok(()) -} diff --git a/journal_cli/src/commands/journal/mod.rs b/journal_cli/src/commands/journal/mod.rs index 31f6fc4..aae3908 100644 --- a/journal_cli/src/commands/journal/mod.rs +++ b/journal_cli/src/commands/journal/mod.rs @@ -9,8 +9,6 @@ pub struct JournalSubcommand { #[command(rename_all = "camelCase")] pub enum JournalSubcommands { New(new::JournalNewArgs), - ImportPsychonaut(import_psychonaut::JournalImportPsychonautArgs), } -pub mod import_psychonaut; pub mod new; diff --git a/journal_cli/src/commands/journal/new.rs b/journal_cli/src/commands/journal/new.rs index 945f270..8927972 100644 --- a/journal_cli/src/commands/journal/new.rs +++ b/journal_cli/src/commands/journal/new.rs @@ -1,8 +1,6 @@ use std::path::Path; -use journal::journal::JSONJournal; - -use crate::args::JournalLocation; +use crate::{args::JournalLocation, helpers::init_journal}; #[derive(Debug, Clone, clap::Args)] #[group(requires = "journal_location")] @@ -17,11 +15,11 @@ pub struct JournalNewArgs { pub fn journal_new(args: &JournalNewArgs) -> Result<(), Box> { let file_location = &args.journal_location.journal_file.clone(); - if Path::new(&file_location).exists() && !args.force { + if Path::new(&file_location.clone().unwrap()).exists() && !args.force { panic!("journal already exists, try --force if you want to recreate") } - JSONJournal::create(file_location)?; + init_journal(&args.journal_location)?; Ok(()) } diff --git a/journal_cli/src/commands/mod.rs b/journal_cli/src/commands/mod.rs index c37d04c..5b6d396 100644 --- a/journal_cli/src/commands/mod.rs +++ b/journal_cli/src/commands/mod.rs @@ -1,4 +1,4 @@ pub mod journal; +pub mod psychonaut; -mod print_session; -pub use print_session::{print_session, PrintSessionArgs}; +pub mod print_session; diff --git a/journal_cli/src/commands/print_session.rs b/journal_cli/src/commands/print_session.rs index 1b18347..860012f 100644 --- a/journal_cli/src/commands/print_session.rs +++ b/journal_cli/src/commands/print_session.rs @@ -1,7 +1,8 @@ -use journal::{journal::JSONJournal, types::Consumer}; +use journal::types::Consumer; use crate::{ args::JournalLocation, display::print_ingestion_log, formatting::format_session_title, + helpers::load_journal, }; #[derive(Debug, Clone, clap::Args)] @@ -35,7 +36,8 @@ pub fn parse_consumer_filter(consumer_filter: Option>) -> Option Result<(), Box> { - let journal = JSONJournal::load(&args.journal_location.journal_file)?; + let journal = load_journal(&args.journal_location)?; + let session = journal .first_session_by_title(&args.session_title) .expect("could not find session"); diff --git a/journal_cli/src/commands/psychonaut/import.rs b/journal_cli/src/commands/psychonaut/import.rs new file mode 100644 index 0000000..35bc99c --- /dev/null +++ b/journal_cli/src/commands/psychonaut/import.rs @@ -0,0 +1,149 @@ +use std::{collections::HashMap, fs::File}; + +use journal::{ + journal::JournalType, + types::{CustomUnit, Ingestion, Session, Substance}, +}; +use psychonaut_journal_types::ExportData; +use rand::Rng; + +use crate::{args::JournalLocation, helpers::load_journal}; + +mod custom_substance; +mod custom_unit; + +#[derive(Debug, Clone, clap::Args)] +pub struct PsychonautImportGlobalArgs { + #[command(flatten)] + pub journal_location: JournalLocation, + + #[arg(long, value_hint = clap::ValueHint::FilePath)] + pub export_file: String, +} + +#[derive(Debug, Clone, clap::Args)] +pub struct PsychonautImportArgs { + #[command(subcommand)] + pub subcommand: PsychonautImportSubcommands, +} + +#[derive(Debug, Clone, clap::Subcommand)] +#[command(rename_all = "camelCase")] +#[command(flatten_help = false)] +#[command(subcommand_negates_reqs = true)] +pub enum PsychonautImportSubcommands { + All(PsychonautImportGlobalArgs), + CustomUnit(custom_unit::PsychonautImportCustomUnitArgs), + CustomSubstance(custom_substance::PsychonautImportCustomSubstanceArgs), +} + +#[derive(Debug, Default, Clone)] +#[derive(serde::Serialize, serde::Deserialize)] +struct Mappings { + pub substance_names: HashMap, +} + +pub fn load_journal_and_export_data( + global_args: &PsychonautImportGlobalArgs, +) -> Result<(JournalType, ExportData), Box> { + let journal = load_journal(&global_args.journal_location)?; + + let export_file = File::open(global_args.export_file.clone())?; + let export_data: ExportData = serde_json::from_reader(export_file)?; + + Ok((journal, export_data)) +} + +pub fn psychonaut_import(args: &PsychonautImportArgs) -> Result<(), Box> { + match &args.subcommand { + PsychonautImportSubcommands::CustomUnit(subcommand_args) => { + custom_unit::psychonaut_import_custom_unit(subcommand_args)? + } + PsychonautImportSubcommands::CustomSubstance(subcommand_args) => { + custom_substance::psychonaut_import_custom_substance(subcommand_args)? + } + PsychonautImportSubcommands::All(args) => { + let (mut journal, export_data) = load_journal_and_export_data(args)?; + + let mut rng = rand::thread_rng(); + + for custom_unit in export_data.custom_units.into_iter() { + custom_unit::create_or_update_custom_unit( + &mut journal, + CustomUnit::from(custom_unit), + ); + } + + for custom_substance in export_data.custom_substances.into_iter() { + custom_substance::create_or_check_custom_substance( + &mut journal, + Substance { + name: custom_substance.name.clone(), + description: custom_substance.description.clone(), + unit: custom_substance.units, + }, + ); + } + + for experience in export_data.experiences.iter() { + for ingestion in experience.ingestions.iter() { + let substance_name = &ingestion.substance_name; + + let ingestion_unit = match ingestion.custom_unit_id { + Some(id) => journal.get_custom_unit(id).unwrap().original_unit, + None => ingestion.unit.clone(), + }; + + match journal.get_substance(substance_name) { + Some(substance) => { + if substance.unit != ingestion_unit { + panic!( + "mismatch on units for {substance_name}: {} != {ingestion_unit} ", + substance.unit + ) + } + } + None => { + journal.create_substance(Substance { + name: substance_name.clone(), + description: "".to_string(), + unit: ingestion_unit, + }); + } + } + } + } + + for experience in export_data.experiences.into_iter() { + if journal.first_session_by_title(&experience.title).is_some() { + println!("Skipping {} as is already in journal", experience.title); + continue; + } else { + println!("Creating experience {}", experience.title); + } + + let session_id = rng.gen::(); + + journal.update_session( + session_id, + Session { + id: session_id, + ..Session::from(experience.clone()) + }, + true, + ); + + let mut ingestions: Vec = Vec::new(); + for ingestion in experience.ingestions.iter() { + ingestions.push(Ingestion::from(ingestion.clone())) + } + + journal.set_session_ingestions(session_id, ingestions); + } + + journal.save()?; + } + } + + Ok(()) +} diff --git a/journal_cli/src/commands/psychonaut/import/custom_substance.rs b/journal_cli/src/commands/psychonaut/import/custom_substance.rs new file mode 100644 index 0000000..e69fb60 --- /dev/null +++ b/journal_cli/src/commands/psychonaut/import/custom_substance.rs @@ -0,0 +1,65 @@ +use journal::{journal::JournalType, types::Substance}; + +use super::{load_journal_and_export_data, PsychonautImportGlobalArgs}; + +pub fn create_or_check_custom_substance(journal: &mut JournalType, custom_substance: Substance) { + match journal.get_substance(&custom_substance.name) { + Some(substance) => { + println!("Checking substance units {}", custom_substance.name); + + if substance.unit != custom_substance.unit { + panic!( + "mismatch on unit for {}: {} != {} ", + custom_substance.name, substance.unit, custom_substance.unit + ) + } + } + None => { + println!("Creating substance {}", custom_substance.name); + journal.create_substance(custom_substance); + } + } +} + +#[derive(Debug, Clone, clap::Args)] +#[group(args = vec!["id", "name"], required = true, multiple = false)] +pub struct PsychonautImportCustomSubstanceArgs { + #[command(flatten)] + pub import_global_args: PsychonautImportGlobalArgs, + + #[arg(long)] + pub name: String, +} + +pub fn psychonaut_import_custom_substance( + args: &PsychonautImportCustomSubstanceArgs, +) -> Result<(), Box> { + let (mut journal, export_data) = load_journal_and_export_data(&args.import_global_args)?; + + let substance_name = &args.name; + + let custom_substance: Option = export_data + .custom_substances + .into_iter() + .find(|substance| substance.name == *substance_name); + + match custom_substance { + Some(custom_substance) => { + create_or_check_custom_substance( + &mut journal, + Substance { + name: custom_substance.name.clone(), + description: custom_substance.description.clone(), + unit: custom_substance.units, + }, + ); + } + None => { + panic!("Could not find substance with name: {substance_name}") + } + } + + journal.save()?; + + Ok(()) +} diff --git a/journal_cli/src/commands/psychonaut/import/custom_unit.rs b/journal_cli/src/commands/psychonaut/import/custom_unit.rs new file mode 100644 index 0000000..168960e --- /dev/null +++ b/journal_cli/src/commands/psychonaut/import/custom_unit.rs @@ -0,0 +1,75 @@ +use journal::{journal::JournalType, types::CustomUnit}; + +use super::{load_journal_and_export_data, PsychonautImportGlobalArgs}; + +pub fn create_or_update_custom_unit(journal: &mut JournalType, unit: CustomUnit) { + match journal.get_custom_unit(unit.id) { + Some(custom_unit) => { + println!( + "Updating Custom Unit ID: {} Name: {}", + unit.id, custom_unit.name + ); + journal.update_custom_unit(unit.id, unit, false); + } + None => { + println!("Creating Custom Unit ID: {} Name: {}", unit.id, unit.name); + journal.update_custom_unit(unit.id, unit, true); + } + } +} + +#[derive(Debug, Clone, clap::Args)] +#[group(args = vec!["id", "name"], required = true, multiple = false)] +pub struct PsychonautImportCustomUnitArgs { + #[command(flatten)] + pub import_global_args: PsychonautImportGlobalArgs, + + #[arg(long)] + pub id: Option, + #[arg(long)] + pub name: Option, +} + +pub fn psychonaut_import_custom_unit( + args: &PsychonautImportCustomUnitArgs, +) -> Result<(), Box> { + let (mut journal, export_data) = load_journal_and_export_data(&args.import_global_args)?; + + match (&args.id, &args.name) { + (Some(id), _) => { + let unit = export_data + .custom_units + .into_iter() + .find(|unit| unit.id == *id); + + match unit { + Some(unit) => { + create_or_update_custom_unit(&mut journal, CustomUnit::from(unit)); + } + None => { + panic!("Could not find custom unit with ID: {id}") + } + } + } + (_, Some(name)) => { + let unit = export_data + .custom_units + .into_iter() + .find(|unit| unit.name == *name); + + match unit { + Some(unit) => { + create_or_update_custom_unit(&mut journal, CustomUnit::from(unit)); + } + None => { + panic!("Could not find custom unit with name: {name}") + } + } + } + _ => unreachable!(), + } + + journal.save()?; + + Ok(()) +} diff --git a/journal_cli/src/commands/psychonaut/mod.rs b/journal_cli/src/commands/psychonaut/mod.rs new file mode 100644 index 0000000..706db6a --- /dev/null +++ b/journal_cli/src/commands/psychonaut/mod.rs @@ -0,0 +1,14 @@ +#[derive(Debug, Clone, clap::Args)] +#[command(rename_all = "camelCase")] +pub struct PsychonautSubcommand { + #[command(subcommand)] + pub subcommand: PsychonautSubcommands, +} + +#[derive(Debug, Clone, clap::Subcommand)] +#[command(rename_all = "camelCase")] +pub enum PsychonautSubcommands { + Import(import::PsychonautImportArgs), +} + +pub mod import; diff --git a/journal_cli/src/display.rs b/journal_cli/src/display.rs index 7526e05..c8d5303 100644 --- a/journal_cli/src/display.rs +++ b/journal_cli/src/display.rs @@ -1,7 +1,7 @@ use journal::{ journal::JournalType, types::{ - format_dose, Consumer, Dose, Ingestion, IngestionKind, IngestionKinds, Session, Substance, + format_dose, Consumer, Dose, Ingestion, IngestionKind, IngestionKinds, Session, SubstanceIngestion, SustenanceIngestion, }, }; @@ -44,19 +44,19 @@ fn print_sustenance_ingestion( ) } -pub fn resolve_ingestion_kinds(journal: &JournalType, kind: &mut IngestionKinds) { +pub fn calculate_ingestion_kinds(kind: &mut IngestionKinds) { //println!("{kind:#?}"); if let IngestionKind::Sustenance(sustenance) = &kind.ingestion { - for kinds in kind.ingestions.iter_mut() { + for kinds in kind.includes.iter_mut() { match &mut kinds.ingestion { IngestionKind::Substance(ingestion) => { ingestion.dose = Dose::new_precise(sustenance.amount) * ingestion.dose.clone(); } IngestionKind::Sustenance(ingestion) => { - ingestion.amount = sustenance.amount * ingestion.amount; + ingestion.amount *= sustenance.amount; - resolve_ingestion_kinds(journal, kinds) + calculate_ingestion_kinds(kinds) } IngestionKind::Hydration { amount } => { *amount = (sustenance.amount * *amount as f64).round() as u64 @@ -98,7 +98,7 @@ pub fn print_ingestion_kinds( } } - for kinds in &kinds.ingestions { + for kinds in &kinds.includes { print_ingestion_kinds(journal, ingestion, kinds, depth + 1); } } @@ -119,7 +119,9 @@ pub fn print_ingestion_log( } } - let ingestion_kinds = journal.get_ingestion_kinds(&ingestion.kind, true).unwrap(); + let ingestion_kinds = journal + .resolve_ingestion_kind(&ingestion.kind, true) + .unwrap(); println!("{ingestion_kinds:#?}"); print_ingestion_kinds(journal, ingestion, &ingestion_kinds, 0) diff --git a/journal_cli/src/helpers.rs b/journal_cli/src/helpers.rs new file mode 100644 index 0000000..dd6d279 --- /dev/null +++ b/journal_cli/src/helpers.rs @@ -0,0 +1,11 @@ +use journal::journal::{JSONJournal, JournalType}; + +use crate::args::JournalLocation; + +pub fn init_journal(location: &JournalLocation) -> Result> { + JSONJournal::create(&location.journal_file.clone().unwrap(), Default::default()) +} + +pub fn load_journal(location: &JournalLocation) -> Result> { + JSONJournal::load(&location.journal_file.clone().unwrap(), Default::default()) +} diff --git a/journal_cli/src/main.rs b/journal_cli/src/main.rs index 6e5550b..1e9d6f3 100644 --- a/journal_cli/src/main.rs +++ b/journal_cli/src/main.rs @@ -5,6 +5,7 @@ pub mod args; pub mod commands; pub mod display; pub mod formatting; +pub mod helpers; use args::{Args, Commands}; @@ -20,13 +21,17 @@ fn main() -> Result<(), Box> { eprintln!("{:#?}", args); match command { - Commands::PrintSession(subommand_args) => commands::print_session(&subommand_args)?, + Commands::PrintSession(subommand_args) => { + commands::print_session::print_session(&subommand_args)? + } Commands::Journal(command) => match command.subcommand { commands::journal::JournalSubcommands::New(subommand_args) => { commands::journal::new::journal_new(&subommand_args)? } - commands::journal::JournalSubcommands::ImportPsychonaut(subommand_args) => { - commands::journal::import_psychonaut::journal_import_psychonaut(&subommand_args)? + }, + Commands::Psychonaut(command) => match command.subcommand { + commands::psychonaut::PsychonautSubcommands::Import(subommand_args) => { + commands::psychonaut::import::psychonaut_import(&subommand_args)? } }, Commands::GenerateShellAutocompletions(subommand_args) => { diff --git a/psychonaut_journal_types/Cargo.lock b/psychonaut_journal_types/Cargo.lock deleted file mode 100644 index 9bf3e2b..0000000 --- a/psychonaut_journal_types/Cargo.lock +++ /dev/null @@ -1,65 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "proc-macro2" -version = "1.0.89" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "psychonaut_journal_types" -version = "0.1.0" -dependencies = [ - "serde", -] - -[[package]] -name = "quote" -version = "1.0.37" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "serde" -version = "1.0.215" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6513c1ad0b11a9376da888e3e0baa0077f1aed55c17f50e7b2397136129fb88f" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.215" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad1e866f866923f252f05c889987993144fb74e722403468a4ebd70c3cd756c0" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "syn" -version = "2.0.87" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "unicode-ident" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"