update
This commit is contained in:
parent
cb2b13a9cf
commit
a01edcd898
|
@ -12,7 +12,11 @@ pub trait Journal {
|
|||
fn get_session_ingestions(&self, id: i32) -> Option<Vec<Ingestion>>;
|
||||
fn get_substance(&self, name: &str) -> Option<Substance>;
|
||||
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 create_session(&mut self, title: &str) -> Session;
|
||||
|
|
|
@ -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<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);
|
||||
|
||||
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 {
|
||||
if calculate {
|
||||
match include {
|
||||
IngestionKind::Sustenance(ingestion) => {
|
||||
kinds.ingestions.push(IngestionKinds {
|
||||
ingestion: include.clone(),
|
||||
ingestions: self.get_ingestion_kinds(&include).unwrap().ingestions,
|
||||
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))
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue