145 lines
3.7 KiB
Rust
145 lines
3.7 KiB
Rust
use journal::{
|
|
journal::JournalType,
|
|
types::{
|
|
format_dose, Consumer, Dose, Ingestion, IngestionKind, Session, SubstanceIngestion,
|
|
SustenanceIngestion,
|
|
},
|
|
};
|
|
|
|
use crate::formatting::{format_ingestion_time, format_substance_ingestion_roa};
|
|
|
|
fn print_substance_ingestion(
|
|
journal: &JournalType,
|
|
ingestion: &Ingestion,
|
|
substance_ingestion: &SubstanceIngestion,
|
|
) {
|
|
let unit = journal.resolve_substance_unit(substance_ingestion).unwrap();
|
|
|
|
println!(
|
|
"Substance|{}|{}|{}|{}|{}",
|
|
substance_ingestion.substance_name,
|
|
format_dose(&substance_ingestion.dose, &unit),
|
|
format_substance_ingestion_roa(substance_ingestion, &unit),
|
|
ingestion.consumer,
|
|
format_ingestion_time(ingestion)
|
|
)
|
|
}
|
|
|
|
fn print_sustenance_ingestion(
|
|
journal: &JournalType,
|
|
ingestion: &Ingestion,
|
|
sustenance_ingestion: &SustenanceIngestion,
|
|
) {
|
|
let sustenance = journal
|
|
.get_sustenance(&sustenance_ingestion.sustenance_id)
|
|
.unwrap();
|
|
|
|
println!(
|
|
"Sustenance|{}|{} {}|{}|{}",
|
|
sustenance.name,
|
|
sustenance_ingestion.amount,
|
|
sustenance.unit,
|
|
ingestion.consumer,
|
|
format_ingestion_time(ingestion)
|
|
)
|
|
}
|
|
|
|
pub fn resolve_ingestion(journal: &JournalType, ingestion: &IngestionKind) -> Vec<IngestionKind> {
|
|
//println!("{ingestion:?}");
|
|
|
|
let mut ingestions: Vec<IngestionKind> = Vec::new();
|
|
|
|
match &ingestion {
|
|
IngestionKind::Substance(substance) => {
|
|
ingestions.push(IngestionKind::Substance(substance.clone()));
|
|
}
|
|
IngestionKind::Hydration { amount: _ } => {
|
|
ingestions.push(ingestion.clone());
|
|
}
|
|
IngestionKind::Sustenance(sustenance) => {
|
|
let sustenance_ingestions = journal
|
|
.get_sustenance_ingestions(&sustenance.sustenance_id)
|
|
.unwrap();
|
|
|
|
for sustenance_ingestion in sustenance_ingestions.into_iter() {
|
|
match sustenance_ingestion {
|
|
IngestionKind::Substance(substance_ingestion) => {
|
|
ingestions.push(IngestionKind::Substance(SubstanceIngestion {
|
|
dose: Dose::new_precise(sustenance.amount) * substance_ingestion.dose,
|
|
..substance_ingestion
|
|
}))
|
|
}
|
|
IngestionKind::Sustenance(sustenance_ingestion) => {
|
|
ingestions.push(IngestionKind::Sustenance(SustenanceIngestion {
|
|
amount: sustenance.amount * sustenance_ingestion.amount,
|
|
..sustenance_ingestion
|
|
}))
|
|
}
|
|
IngestionKind::Hydration { amount } => {
|
|
ingestions.push(IngestionKind::Hydration {
|
|
amount: (sustenance.amount * amount as f64).round() as u64,
|
|
})
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
_ => {}
|
|
};
|
|
|
|
//println!("{ingestions:#?}");
|
|
ingestions
|
|
}
|
|
|
|
pub fn print_ingestion(journal: &JournalType, ingestion: &Ingestion, kind: &IngestionKind) {
|
|
match kind {
|
|
IngestionKind::Unknown => {}
|
|
IngestionKind::Substance(substance_ingestion) => {
|
|
print_substance_ingestion(journal, ingestion, substance_ingestion);
|
|
}
|
|
IngestionKind::Sustenance(sustenance_ingestion) => {
|
|
print_sustenance_ingestion(journal, ingestion, sustenance_ingestion);
|
|
}
|
|
IngestionKind::Hydration { amount } => {
|
|
println!(
|
|
"Hydration|{} ml|{}|{}",
|
|
amount,
|
|
ingestion.consumer,
|
|
format_ingestion_time(ingestion)
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn print_ingestion_log(
|
|
journal: &JournalType,
|
|
session: &Session,
|
|
consumer_filter: Option<&Vec<Consumer>>,
|
|
) {
|
|
for ingestion in journal
|
|
.get_session_ingestions(session.id)
|
|
.expect("could not find ingestions for session")
|
|
.iter()
|
|
{
|
|
if let Some(consumer_filter) = consumer_filter {
|
|
if !consumer_filter.contains(&ingestion.consumer) {
|
|
continue;
|
|
}
|
|
}
|
|
|
|
match &ingestion.kind {
|
|
IngestionKind::Sustenance(_) => {
|
|
print_ingestion(journal, ingestion, &ingestion.kind);
|
|
|
|
for kind in resolve_ingestion(journal, &ingestion.kind) {
|
|
print!("- ");
|
|
print_ingestion(journal, ingestion, &kind)
|
|
}
|
|
}
|
|
_ => {
|
|
print_ingestion(journal, ingestion, &ingestion.kind);
|
|
}
|
|
}
|
|
}
|
|
}
|