This commit is contained in:
chaos 2024-11-25 03:28:07 +00:00
parent cb2b13a9cf
commit a01edcd898
3 changed files with 68 additions and 14 deletions

View file

@ -12,7 +12,11 @@ pub trait Journal {
fn get_session_ingestions(&self, id: i32) -> Option<Vec<Ingestion>>; fn get_session_ingestions(&self, id: i32) -> Option<Vec<Ingestion>>;
fn get_substance(&self, name: &str) -> Option<Substance>; fn get_substance(&self, name: &str) -> Option<Substance>;
fn get_sustenance(&self, id: &str) -> Option<Sustenance>; fn get_sustenance(&self, id: &str) -> Option<Sustenance>;
fn get_ingestion_kinds(&self, ingestion: &IngestionKind) -> Option<IngestionKinds>; fn get_ingestion_kinds(
&self,
ingestion: &IngestionKind,
calculate: bool,
) -> Option<IngestionKinds>;
fn get_custom_unit(&self, id: i32) -> Option<CustomUnit>; fn get_custom_unit(&self, id: i32) -> Option<CustomUnit>;
fn create_session(&mut self, title: &str) -> Session; fn create_session(&mut self, title: &str) -> Session;

View file

@ -4,8 +4,8 @@ use chrono::Utc;
use rand::Rng; use rand::Rng;
use crate::types::{ use crate::types::{
CustomUnit, Ingestion, IngestionKind, IngestionKinds, Session, Substance, SubstanceIngestion, CustomUnit, Dose, Ingestion, IngestionKind, IngestionKinds, Session, Substance,
Sustenance, Unit, SubstanceIngestion, Sustenance, SustenanceIngestion, Unit,
}; };
use super::{Journal, JournalType}; use super::{Journal, JournalType};
@ -83,18 +83,57 @@ impl Journal for JSONJournal {
self.data.sustenances.get(sustenance_id).cloned() self.data.sustenances.get(sustenance_id).cloned()
} }
fn get_ingestion_kinds(&self, kind: &IngestionKind) -> Option<IngestionKinds> { // 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<IngestionKinds> {
println!("{kind:#?}");
let mut kinds: IngestionKinds = IngestionKinds::from(kind); let mut kinds: IngestionKinds = IngestionKinds::from(kind);
if let IngestionKind::Sustenance(sustenance) = kind { if let IngestionKind::Sustenance(sustenance) = kind {
let includes = self.get_sustenance(&sustenance.sustenance_id)?.includes; let includes = self.get_sustenance(&sustenance.sustenance_id)?.includes;
for include in includes.into_iter() { for include in includes.into_iter() {
if let IngestionKind::Sustenance(_) = include { if calculate {
kinds.ingestions.push(IngestionKinds { match include {
ingestion: include.clone(), IngestionKind::Sustenance(ingestion) => {
ingestions: self.get_ingestion_kinds(&include).unwrap().ingestions, 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 { } else {
kinds.ingestions.push(IngestionKinds::from(&include)) kinds.ingestions.push(IngestionKinds::from(&include))
} }

View file

@ -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 { match &kinds.ingestion {
IngestionKind::Unknown => {} IngestionKind::Unknown => {}
IngestionKind::Substance(substance_ingestion) => { IngestionKind::Substance(substance_ingestion) => {
@ -87,7 +99,7 @@ pub fn print_ingestion_kinds(journal: &JournalType, ingestion: &Ingestion, kinds
} }
for kinds in &kinds.ingestions { 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:#?}"); println!("{ingestion_kinds:#?}");
resolve_ingestion_kinds(journal, &mut ingestion_kinds); print_ingestion_kinds(journal, ingestion, &ingestion_kinds, 0)
print_ingestion_kinds(journal, ingestion, &ingestion_kinds)
} }
} }