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 fn calculate_ingestion_amounts(ingestion: &mut IngestionKind, recurse: bool) {
pub fn calculate_ingestion_amounts(ingestion: &mut IngestionKind) {
if let IngestionKind::Sustenance(sustenance) = ingestion {
let includes = &mut sustenance.sustenance.as_mut().unwrap().includes;
for include in includes.iter_mut() {
match include {
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) => {
ingestion.amount *= sustenance.amount;
if recurse {
calculate_ingestion_amounts(include, false);
}
calculate_ingestion_amounts(include);
}
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 ingestions: Vec<IngestionKind> = Vec::new();
ingestions.push(IngestionKind::Sustenance(root));
let mut ingestions: Vec<IngestionKind> = vec![IngestionKind::Sustenance(root)];
while !ingestions.is_empty() {
let current_ingestions = ingestions.clone();
ingestions.clear();
let current_ingestions = ingestions;
ingestions = Vec::new();
for ingestion in current_ingestions {
match ingestion {
@ -79,19 +79,24 @@ pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedVal
}
for include in sustenance.includes.iter_mut() {
if multiply_includes {
match include {
IngestionKind::Unknown => {}
IngestionKind::Substance(ingestion) => {
ingestion.dose = ingestion.dose.clone() * Dose::new_precise(amount)
ingestion.dose = Dose::new_precise(amount) * &ingestion.dose
}
IngestionKind::Sustenance(ingestion) => ingestion.amount *= amount,
IngestionKind::Hydration(ingestion) => ingestion.amount_ml *= amount,
IngestionKind::Hydration(ingestion) => {
ingestion.amount_ml *= amount
}
}
}
ingestions.push(include.clone());
}
}
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)]
pub tree: bool,
#[arg(long)]
pub no_multiply_tree: bool,
}
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());
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(())
}

View file

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