From f711240faa0c66538c75cb31ce90a786e34a2267 Mon Sep 17 00:00:00 2001 From: chaos Date: Wed, 27 Nov 2024 20:49:53 +0000 Subject: [PATCH] update --- journal/src/calculate.rs | 116 +++++++----------- journal/src/journal/json_journal.rs | 3 +- journal/src/types/custom_unit.rs | 2 +- journal_cli/src/commands/psychonaut/import.rs | 2 +- journal_cli/src/display.rs | 17 +-- 5 files changed, 56 insertions(+), 84 deletions(-) diff --git a/journal/src/calculate.rs b/journal/src/calculate.rs index 31096cd..9fbff83 100644 --- a/journal/src/calculate.rs +++ b/journal/src/calculate.rs @@ -1,102 +1,72 @@ -use std::{ - collections::HashMap, - ops::{Add, Mul}, -}; +use std::collections::HashMap; -use crate::types::{Dose, HydrationIngestion, IngestionKind, Nutrients, SubstanceIngestion}; +use crate::types::{ + AdministrationRoute, Dose, HydrationIngestion, IngestionKind, Nutrients, SubstanceIngestion, + SustenanceIngestion, +}; #[derive(Debug, Default, PartialEq)] #[derive(serde::Serialize, serde::Deserialize)] pub struct CalculatedValues { - pub doses: HashMap, + pub substances: HashMap, pub nutrients: Nutrients, pub hydration: HydrationIngestion, } -impl Add for CalculatedValues { - type Output = CalculatedValues; +pub fn calculate_sustenance_ingestion_amounts(sustenance: &mut SustenanceIngestion) { + let includes = &mut sustenance.sustenance.as_mut().unwrap().includes; - fn add(self, rhs: CalculatedValues) -> Self::Output { - let mut doses = self.doses.clone(); - let mut nutrients = self.nutrients.clone(); - let mut hydration = self.hydration.clone(); + for include in includes.iter_mut() { + match include { + IngestionKind::Substance(ingestion) => { + ingestion.dose = Dose::new_precise(sustenance.amount) * ingestion.dose.clone(); + } + IngestionKind::Sustenance(ingestion) => { + ingestion.amount *= sustenance.amount; - for (substance_id, substance_dose) in rhs.doses.iter() { - let dose = self - .doses - .get(substance_id) - .cloned() - .or(Some(Dose::new_precise(0.0))) - .unwrap(); - - doses.insert(substance_id.clone(), dose + substance_dose.clone()); - } - - nutrients.kcal += rhs.nutrients.kcal; - hydration.amount_ml += rhs.hydration.amount_ml; - - CalculatedValues { - doses, - nutrients, - hydration, + calculate_sustenance_ingestion_amounts(ingestion) + } + IngestionKind::Hydration(hydration) => { + hydration.amount_ml *= sustenance.amount + } + _ => {} } } } -impl Mul for CalculatedValues { - type Output = CalculatedValues; - - fn mul(self, rhs: f64) -> Self::Output { - let mut doses = self.doses.clone(); - let mut nutrients = self.nutrients.clone(); - let mut hydration = self.hydration.clone(); - - for (_substance_id, substance_dose) in doses.iter_mut() { - *substance_dose = substance_dose.clone() * Dose::new_precise(rhs); - } - - nutrients.kcal = (rhs * nutrients.kcal as f64).round() as u64; - hydration.amount_ml *= rhs; - - CalculatedValues { - doses, - nutrients, - hydration, - } - } -} - -impl CalculatedValues { - fn new() -> Self { - Self { - ..Default::default() - } - } -} - -pub fn calculate_ingestion_kind(kind: IngestionKind) -> CalculatedValues { - let mut values = CalculatedValues::new(); +pub fn calculate_sustenance_contents(root: SustenanceIngestion) -> CalculatedValues { + let mut values = CalculatedValues::default(); let mut ingestions: Vec = Vec::new(); - ingestions.push(kind); + ingestions.push(IngestionKind::Sustenance(root)); while !ingestions.is_empty() { let current_ingestions = ingestions.clone(); - for kind in current_ingestions { - match kind { + for ingestion in current_ingestions { + match ingestion { IngestionKind::Unknown => {} IngestionKind::Substance(substance) => { - let dose = values - .doses + let substance_ingestion = values + .substances .get(&substance.id) .cloned() - .or(Some(Dose::new_precise(0.0))) - .unwrap(); + .unwrap_or(SubstanceIngestion { + id: substance.id.clone(), + substance: None, + dose: Dose::new_precise(0.0), + custom_unit_id: None, + roa: AdministrationRoute::Oral, + stomach_fullness: None, + }); - values - .doses - .insert(substance.id.clone(), dose + substance.dose.clone()); + values.substances.insert( + substance.id.clone(), + SubstanceIngestion { + dose: substance_ingestion.dose.clone() + substance.dose, + ..substance_ingestion + }, + ); } IngestionKind::Sustenance(sustenance) => { let amount = sustenance.amount; diff --git a/journal/src/journal/json_journal.rs b/journal/src/journal/json_journal.rs index 18da89d..31e48bb 100644 --- a/journal/src/journal/json_journal.rs +++ b/journal/src/journal/json_journal.rs @@ -4,8 +4,7 @@ use chrono::Utc; use rand::Rng; use crate::types::{ - CustomUnit, Dose, Ingestion, IngestionKind, Session, Substance, SubstanceIngestion, Sustenance, - SustenanceIngestion, Unit, + CustomUnit, Ingestion, IngestionKind, Session, Substance, SubstanceIngestion, Sustenance, Unit, }; use super::{Journal, JournalIntegrityChecks, JournalTrait, JournalType}; diff --git a/journal/src/types/custom_unit.rs b/journal/src/types/custom_unit.rs index cf6c0d0..b89e897 100644 --- a/journal/src/types/custom_unit.rs +++ b/journal/src/types/custom_unit.rs @@ -1,4 +1,4 @@ -use super::{from_unix_millis, AdministrationRoute, Dose}; +use super::{AdministrationRoute, Dose}; use chrono::{DateTime, Utc}; #[derive(Default, PartialEq, Debug, Clone)] diff --git a/journal_cli/src/commands/psychonaut/import.rs b/journal_cli/src/commands/psychonaut/import.rs index 72bdb67..9c41616 100644 --- a/journal_cli/src/commands/psychonaut/import.rs +++ b/journal_cli/src/commands/psychonaut/import.rs @@ -3,7 +3,7 @@ use std::{collections::HashMap, fs::File}; use journal::{ journal::JournalType, types::{ - Consumer, CustomUnit, Dose, Ingestion, IngestionKind, Session, Substance, + Consumer, Dose, Ingestion, IngestionKind, Session, Substance, SubstanceIngestion, }, }; diff --git a/journal_cli/src/display.rs b/journal_cli/src/display.rs index e0404cf..7f5b394 100644 --- a/journal_cli/src/display.rs +++ b/journal_cli/src/display.rs @@ -1,4 +1,5 @@ use journal::{ + calculate::calculate_sustenance_ingestion_amounts, journal::JournalType, types::{ format_dose, Consumer, Ingestion, IngestionKind, Session, SubstanceIngestion, @@ -57,17 +58,21 @@ pub fn print_ingestion(journal: &JournalType, ingestion: &Ingestion, depth: u64) match &ingestion.kind { IngestionKind::Unknown => {} IngestionKind::Substance(substance) => { - print_substance_ingestion(journal, ingestion, &substance); + print_substance_ingestion(journal, ingestion, substance); } IngestionKind::Sustenance(sustenance) => { - print_sustenance_ingestion(journal, ingestion, sustenance); - let sustenance = sustenance.sustenance.as_ref().unwrap(); + let mut sustenance = sustenance.clone(); + calculate_sustenance_ingestion_amounts(&mut sustenance); - for include in &sustenance.includes { + print_sustenance_ingestion(journal, ingestion, &sustenance); + + let includes = sustenance.sustenance.unwrap().includes; + + for include in includes { print_ingestion( journal, &Ingestion { - kind: include.clone(), + kind: include, ..ingestion.clone() }, depth + 1, @@ -105,8 +110,6 @@ pub fn print_ingestion_log( .resolve_ingestion_kind(ingestion.kind.clone()) .unwrap(); - //let calculated_values = calculate_ingestion_kinds(ingestion_kinds); - print_ingestion(journal, ingestion, 0) } }