From a01edcd8986e4f5f2a4940138ba02b77a37b6522 Mon Sep 17 00:00:00 2001 From: chaos Date: Mon, 25 Nov 2024 03:28:07 +0000 Subject: [PATCH] update --- journal/src/journal.rs | 6 +++- journal/src/journal/json_journal.rs | 55 ++++++++++++++++++++++++----- journal_cli/src/display.rs | 21 ++++++++--- 3 files changed, 68 insertions(+), 14 deletions(-) diff --git a/journal/src/journal.rs b/journal/src/journal.rs index 74a87f8..c559ec3 100644 --- a/journal/src/journal.rs +++ b/journal/src/journal.rs @@ -12,7 +12,11 @@ pub trait Journal { 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(&self, ingestion: &IngestionKind) -> Option; + fn get_ingestion_kinds( + &self, + ingestion: &IngestionKind, + calculate: bool, + ) -> Option; fn get_custom_unit(&self, id: i32) -> Option; fn create_session(&mut self, title: &str) -> Session; diff --git a/journal/src/journal/json_journal.rs b/journal/src/journal/json_journal.rs index 77d624b..08a5ebe 100644 --- a/journal/src/journal/json_journal.rs +++ b/journal/src/journal/json_journal.rs @@ -4,8 +4,8 @@ use chrono::Utc; use rand::Rng; use crate::types::{ - CustomUnit, Ingestion, IngestionKind, IngestionKinds, Session, Substance, SubstanceIngestion, - Sustenance, Unit, + CustomUnit, Dose, Ingestion, IngestionKind, IngestionKinds, Session, Substance, + SubstanceIngestion, Sustenance, SustenanceIngestion, Unit, }; use super::{Journal, JournalType}; @@ -83,18 +83,57 @@ impl Journal for JSONJournal { self.data.sustenances.get(sustenance_id).cloned() } - fn get_ingestion_kinds(&self, kind: &IngestionKind) -> Option { + // TODO: make error when substance depends on itself + // maybe track the IDs of sustenances visited as a mutable array? + fn get_ingestion_kinds(&self, kind: &IngestionKind, calculate: bool) -> Option { + println!("{kind:#?}"); let mut kinds: IngestionKinds = IngestionKinds::from(kind); if let IngestionKind::Sustenance(sustenance) = kind { let includes = self.get_sustenance(&sustenance.sustenance_id)?.includes; for include in includes.into_iter() { - if let IngestionKind::Sustenance(_) = include { - kinds.ingestions.push(IngestionKinds { - ingestion: include.clone(), - ingestions: self.get_ingestion_kinds(&include).unwrap().ingestions, - }); + if calculate { + match include { + IngestionKind::Sustenance(ingestion) => { + kinds.ingestions.push(IngestionKinds { + ingestion: IngestionKind::Sustenance(SustenanceIngestion { + amount: sustenance.amount * ingestion.amount, + ..ingestion.clone() + }), + ingestions: self + .get_ingestion_kinds( + &IngestionKind::Sustenance(ingestion), + calculate, + ) + .unwrap() + .ingestions, + }); + } + IngestionKind::Unknown => { + kinds + .ingestions + .push(IngestionKinds::from(&IngestionKind::Unknown)); + } + IngestionKind::Substance(ingestion) => { + kinds + .ingestions + .push(IngestionKinds::from(&IngestionKind::Substance( + SubstanceIngestion { + dose: Dose::new_precise(sustenance.amount) + * ingestion.dose.clone(), + ..ingestion + }, + ))); + } + IngestionKind::Hydration { amount } => { + kinds.ingestions.push(IngestionKinds::from( + &IngestionKind::Hydration { + amount: (sustenance.amount * amount as f64).round() as u64, + }, + )); + } + } } else { kinds.ingestions.push(IngestionKinds::from(&include)) } diff --git a/journal_cli/src/display.rs b/journal_cli/src/display.rs index 5fee6a6..7526e05 100644 --- a/journal_cli/src/display.rs +++ b/journal_cli/src/display.rs @@ -67,7 +67,19 @@ pub fn resolve_ingestion_kinds(journal: &JournalType, kind: &mut IngestionKinds) } } -pub fn print_ingestion_kinds(journal: &JournalType, ingestion: &Ingestion, kinds: &IngestionKinds) { +pub fn print_ingestion_kinds( + journal: &JournalType, + ingestion: &Ingestion, + kinds: &IngestionKinds, + depth: u64, +) { + for _ in 0..(depth) { + print!(" "); + } + if depth > 0 { + print!("- "); + } + match &kinds.ingestion { IngestionKind::Unknown => {} IngestionKind::Substance(substance_ingestion) => { @@ -87,7 +99,7 @@ pub fn print_ingestion_kinds(journal: &JournalType, ingestion: &Ingestion, kinds } for kinds in &kinds.ingestions { - print_ingestion_kinds(journal, ingestion, kinds); + print_ingestion_kinds(journal, ingestion, kinds, depth + 1); } } @@ -107,10 +119,9 @@ pub fn print_ingestion_log( } } - let mut ingestion_kinds = journal.get_ingestion_kinds(&ingestion.kind).unwrap(); + let ingestion_kinds = journal.get_ingestion_kinds(&ingestion.kind, true).unwrap(); println!("{ingestion_kinds:#?}"); - resolve_ingestion_kinds(journal, &mut ingestion_kinds); - print_ingestion_kinds(journal, ingestion, &ingestion_kinds) + print_ingestion_kinds(journal, ingestion, &ingestion_kinds, 0) } }