diff --git a/journal/src/calculate.rs b/journal/src/calculate.rs index 345f462..963a851 100644 --- a/journal/src/calculate.rs +++ b/journal/src/calculate.rs @@ -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 = Vec::new(); - ingestions.push(IngestionKind::Sustenance(root)); + let mut ingestions: Vec = 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() { - match include { - IngestionKind::Unknown => {} - IngestionKind::Substance(ingestion) => { - ingestion.dose = ingestion.dose.clone() * Dose::new_precise(amount) + if multiply_includes { + match include { + IngestionKind::Unknown => {} + 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()); } } IngestionKind::Hydration(hydration) => { - values.hydration.amount_ml *= hydration.amount_ml; + values.hydration.amount_ml += hydration.amount_ml; } } } diff --git a/journal_cli/src/commands/print_session.rs b/journal_cli/src/commands/print_session.rs index 7194359..ad5d6f9 100644 --- a/journal_cli/src/commands/print_session.rs +++ b/journal_cli/src/commands/print_session.rs @@ -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>) -> Option> { @@ -51,7 +53,13 @@ pub fn print_session(args: &PrintSessionArgs) -> Result<(), Box { + print!("Unknown"); + } + 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!( - "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, 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( 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; + if let IngestionKind::Sustenance(sustenance) = &ingestion.kind { + 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)); - println!("Nutrients:"); - print!("{}", " ".repeat((depth + 1) as usize)); - println!("kcal: {}", values.nutrients.kcal); + if tree { + print!("{}", " ".repeat(depth as usize)); + println!("Includes:"); - print!("{}", " ".repeat(depth as usize)); - println!("Includes:"); - } + depth += 1; - if tree { - let includes = sustenance.sustenance.unwrap().includes; + fn print_includes(journal: &JournalType, ingestion: &mut Ingestion, depth: i32) { + print_ingestion_kind(journal, ingestion); - for include in includes { - ingestion.kind = include; + if let IngestionKind::Sustenance(sustenance) = &mut ingestion.kind { + 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) => { - 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>, 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); } } diff --git a/journal_cli/src/main.rs b/journal_cli/src/main.rs index 1e9d6f3..421e36d 100644 --- a/journal_cli/src/main.rs +++ b/journal_cli/src/main.rs @@ -18,7 +18,7 @@ fn main() -> Result<(), Box> { let command = args.command.to_owned(); - eprintln!("{:#?}", args); + //eprintln!("{:#?}", args); match command { Commands::PrintSession(subommand_args) => {