This commit is contained in:
chaos 2024-11-28 12:26:17 +00:00
parent 53573c878e
commit f111f38197
4 changed files with 96 additions and 92 deletions

View file

@ -14,21 +14,19 @@ pub struct CalculatedValues {
pub hydration: HydrationIngestion, pub hydration: HydrationIngestion,
} }
pub fn calculate_ingestion_amounts(ingestion: &mut IngestionKind, recurse: bool) { pub fn calculate_ingestion_amounts(ingestion: &mut IngestionKind) {
if let IngestionKind::Sustenance(sustenance) = ingestion { if let IngestionKind::Sustenance(sustenance) = ingestion {
let includes = &mut sustenance.sustenance.as_mut().unwrap().includes; let includes = &mut sustenance.sustenance.as_mut().unwrap().includes;
for include in includes.iter_mut() { for include in includes.iter_mut() {
match include { match include {
IngestionKind::Substance(ingestion) => { IngestionKind::Substance(ingestion) => {
ingestion.dose = Dose::new_precise(sustenance.amount) * ingestion.dose.clone(); ingestion.dose = Dose::new_precise(sustenance.amount) * &ingestion.dose;
} }
IngestionKind::Sustenance(ingestion) => { IngestionKind::Sustenance(ingestion) => {
ingestion.amount *= sustenance.amount; ingestion.amount *= sustenance.amount;
if recurse { calculate_ingestion_amounts(include);
calculate_ingestion_amounts(include, false);
}
} }
IngestionKind::Hydration(hydration) => hydration.amount_ml *= sustenance.amount, IngestionKind::Hydration(hydration) => hydration.amount_ml *= sustenance.amount,
_ => {} _ => {}
@ -37,15 +35,17 @@ pub fn calculate_ingestion_amounts(ingestion: &mut IngestionKind, recurse: bool)
} }
} }
pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedValues { pub fn calculate_sustenance_contents(
root: SustenanceIngestion,
multiply_includes: bool,
) -> CalculatedValues {
let mut values = CalculatedValues::default(); let mut values = CalculatedValues::default();
let mut ingestions: Vec<IngestionKind> = Vec::new(); let mut ingestions: Vec<IngestionKind> = vec![IngestionKind::Sustenance(root)];
ingestions.push(IngestionKind::Sustenance(root));
while !ingestions.is_empty() { while !ingestions.is_empty() {
let current_ingestions = ingestions.clone(); let current_ingestions = ingestions;
ingestions.clear(); ingestions = Vec::new();
for ingestion in current_ingestions { for ingestion in current_ingestions {
match ingestion { match ingestion {
@ -79,19 +79,24 @@ pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedVal
} }
for include in sustenance.includes.iter_mut() { for include in sustenance.includes.iter_mut() {
match include { if multiply_includes {
IngestionKind::Unknown => {} match include {
IngestionKind::Substance(ingestion) => { IngestionKind::Unknown => {}
ingestion.dose = ingestion.dose.clone() * Dose::new_precise(amount) IngestionKind::Substance(ingestion) => {
ingestion.dose = Dose::new_precise(amount) * &ingestion.dose
}
IngestionKind::Sustenance(ingestion) => ingestion.amount *= amount,
IngestionKind::Hydration(ingestion) => {
ingestion.amount_ml *= amount
}
} }
IngestionKind::Sustenance(ingestion) => ingestion.amount *= amount,
IngestionKind::Hydration(ingestion) => ingestion.amount_ml *= amount,
} }
ingestions.push(include.clone()); ingestions.push(include.clone());
} }
} }
IngestionKind::Hydration(hydration) => { IngestionKind::Hydration(hydration) => {
values.hydration.amount_ml *= hydration.amount_ml; values.hydration.amount_ml += hydration.amount_ml;
} }
} }
} }

View file

@ -18,6 +18,8 @@ pub struct PrintSessionArgs {
#[arg(long)] #[arg(long)]
pub tree: bool, pub tree: bool,
#[arg(long)]
pub no_multiply_tree: bool,
} }
pub fn parse_consumer_filter(consumer_filter: Option<Vec<String>>) -> Option<Vec<Consumer>> { pub fn parse_consumer_filter(consumer_filter: Option<Vec<String>>) -> Option<Vec<Consumer>> {
@ -51,7 +53,13 @@ pub fn print_session(args: &PrintSessionArgs) -> Result<(), Box<dyn std::error::
let consumer_filter = parse_consumer_filter(args.consumer_filter.clone()); let consumer_filter = parse_consumer_filter(args.consumer_filter.clone());
print_ingestion_log(&journal, &session, consumer_filter.as_ref(), args.tree); print_ingestion_log(
&journal,
&session,
consumer_filter.as_ref(),
args.tree,
!args.no_multiply_tree,
);
Ok(()) Ok(())
} }

View file

@ -1,104 +1,94 @@
use journal::{ use journal::{
calculate::{calculate_ingestion_amounts, calculate_sustenance_contents}, calculate::{calculate_ingestion_amounts, calculate_sustenance_contents},
journal::JournalType, journal::JournalType,
types::{ types::{format_dose, Consumer, Ingestion, IngestionKind, Session},
format_dose, Consumer, Ingestion, IngestionKind, Session, SubstanceIngestion,
SustenanceIngestion,
},
}; };
use crate::formatting::{format_ingestion_time, format_substance_ingestion_roa}; use crate::formatting::{format_ingestion_time, format_substance_ingestion_roa};
fn print_substance_ingestion( pub fn print_ingestion_kind(journal: &JournalType, ingestion: &Ingestion) {
journal: &JournalType, match &ingestion.kind {
ingestion: &Ingestion, IngestionKind::Unknown => {
substance_ingestion: &SubstanceIngestion, print!("Unknown");
) { }
let unit = journal.resolve_substance_unit(substance_ingestion).unwrap(); IngestionKind::Substance(substance_ingestion) => {
let unit = journal.resolve_substance_unit(substance_ingestion).unwrap();
print!(
"Substance|{}|{}|{}",
substance_ingestion
.substance
.as_ref()
.expect("substance_ingestion not provided with a substance")
.name,
format_dose(&substance_ingestion.dose, &unit),
format_substance_ingestion_roa(substance_ingestion, &unit),
)
}
IngestionKind::Sustenance(sustenance_ingestion) => {
let sustenance = sustenance_ingestion.sustenance.as_ref().unwrap();
print!(
"Sustenance|{}|{} {}",
sustenance.name, sustenance_ingestion.amount, sustenance.unit,
)
}
IngestionKind::Hydration(hydration) => {
print!("Hydration|{}", hydration)
}
}
println!( println!(
"Substance|{}|{}|{}|{}|{}", "|{}|{}",
substance_ingestion
.substance
.as_ref()
.expect("substance_ingestion not provided with a substance")
.name,
format_dose(&substance_ingestion.dose, &unit),
format_substance_ingestion_roa(substance_ingestion, &unit),
ingestion.consumer, ingestion.consumer,
format_ingestion_time(ingestion) format_ingestion_time(ingestion)
) );
}
fn print_sustenance_ingestion(
journal: &JournalType,
ingestion: &Ingestion,
sustenance_ingestion: &SustenanceIngestion,
) {
let sustenance = journal.get_sustenance(&sustenance_ingestion.id).unwrap();
println!(
"Sustenance|{}|{} {}|{}|{}",
sustenance.name,
sustenance_ingestion.amount,
sustenance.unit,
ingestion.consumer,
format_ingestion_time(ingestion)
)
} }
pub fn print_ingestion( pub fn print_ingestion(
journal: &JournalType, journal: &JournalType,
mut ingestion: Ingestion, mut ingestion: Ingestion,
mut depth: u64,
tree: bool, tree: bool,
multiply_tree: bool,
) { ) {
if depth > 0 { if tree && multiply_tree {
print!("{}- ", " ".repeat(depth as usize)); calculate_ingestion_amounts(&mut ingestion.kind);
}
if depth == 0 {
calculate_ingestion_amounts(&mut ingestion.kind, true);
} }
match ingestion.kind.clone() { print_ingestion_kind(journal, &ingestion);
IngestionKind::Unknown => {}
IngestionKind::Substance(substance) => {
print_substance_ingestion(journal, &ingestion, &substance);
}
IngestionKind::Sustenance(sustenance) => {
print_sustenance_ingestion(journal, &ingestion, &sustenance);
if depth == 0 { if let IngestionKind::Sustenance(sustenance) = &ingestion.kind {
depth += 2; let contents = calculate_sustenance_contents(sustenance.clone(), !(tree && multiply_tree));
let mut depth = 1;
let values = calculate_sustenance_contents(sustenance.clone()); print!("{}", " ".repeat(depth as usize));
println!("Nutrients:");
print!("{}", " ".repeat((depth + 1) as usize));
println!("kcal: {}", contents.nutrients.kcal);
print!("{}", " ".repeat(depth as usize)); if tree {
println!("Nutrients:"); print!("{}", " ".repeat(depth as usize));
print!("{}", " ".repeat((depth + 1) as usize)); println!("Includes:");
println!("kcal: {}", values.nutrients.kcal);
print!("{}", " ".repeat(depth as usize)); depth += 1;
println!("Includes:");
}
if tree { fn print_includes(journal: &JournalType, ingestion: &mut Ingestion, depth: i32) {
let includes = sustenance.sustenance.unwrap().includes; print_ingestion_kind(journal, ingestion);
for include in includes { if let IngestionKind::Sustenance(sustenance) = &mut ingestion.kind {
ingestion.kind = include; let sustenance = sustenance.sustenance.as_mut().unwrap();
print_ingestion(journal, ingestion.clone(), depth + 1, tree) let includes = std::mem::take(&mut sustenance.includes);
for include in includes.into_iter() {
print!("{}- ", " ".repeat(depth as usize));
ingestion.kind = include;
print_includes(journal, ingestion, depth + 1);
}
} }
} }
}
IngestionKind::Hydration(hydration) => { print_includes(journal, &mut ingestion, depth);
println!(
"Hydration|{}|{}|{}",
hydration,
ingestion.consumer.clone(),
format_ingestion_time(&ingestion)
)
} }
} }
} }
@ -108,6 +98,7 @@ pub fn print_ingestion_log(
session: &Session, session: &Session,
consumer_filter: Option<&Vec<Consumer>>, consumer_filter: Option<&Vec<Consumer>>,
tree: bool, tree: bool,
multiply_tree: bool,
) { ) {
let ingestions = journal let ingestions = journal
.get_session_ingestions(session.id) .get_session_ingestions(session.id)
@ -122,6 +113,6 @@ pub fn print_ingestion_log(
ingestion.kind = journal.resolve_ingestion_kind(ingestion.kind).unwrap(); ingestion.kind = journal.resolve_ingestion_kind(ingestion.kind).unwrap();
print_ingestion(journal, ingestion, 0, tree) print_ingestion(journal, ingestion, tree, multiply_tree);
} }
} }

View file

@ -18,7 +18,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let command = args.command.to_owned(); let command = args.command.to_owned();
eprintln!("{:#?}", args); //eprintln!("{:#?}", args);
match command { match command {
Commands::PrintSession(subommand_args) => { Commands::PrintSession(subommand_args) => {