update
This commit is contained in:
parent
53573c878e
commit
f111f38197
|
@ -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() {
|
||||||
|
if multiply_includes {
|
||||||
match include {
|
match include {
|
||||||
IngestionKind::Unknown => {}
|
IngestionKind::Unknown => {}
|
||||||
IngestionKind::Substance(ingestion) => {
|
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::Sustenance(ingestion) => ingestion.amount *= amount,
|
||||||
IngestionKind::Hydration(ingestion) => ingestion.amount_ml *= 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,21 @@
|
||||||
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");
|
||||||
) {
|
}
|
||||||
|
IngestionKind::Substance(substance_ingestion) => {
|
||||||
let unit = journal.resolve_substance_unit(substance_ingestion).unwrap();
|
let unit = journal.resolve_substance_unit(substance_ingestion).unwrap();
|
||||||
|
|
||||||
println!(
|
print!(
|
||||||
"Substance|{}|{}|{}|{}|{}",
|
"Substance|{}|{}|{}",
|
||||||
substance_ingestion
|
substance_ingestion
|
||||||
.substance
|
.substance
|
||||||
.as_ref()
|
.as_ref()
|
||||||
|
@ -25,80 +23,72 @@ fn print_substance_ingestion(
|
||||||
.name,
|
.name,
|
||||||
format_dose(&substance_ingestion.dose, &unit),
|
format_dose(&substance_ingestion.dose, &unit),
|
||||||
format_substance_ingestion_roa(substance_ingestion, &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(
|
print!(
|
||||||
journal: &JournalType,
|
"Sustenance|{}|{} {}",
|
||||||
ingestion: &Ingestion,
|
sustenance.name, sustenance_ingestion.amount, sustenance.unit,
|
||||||
sustenance_ingestion: &SustenanceIngestion,
|
)
|
||||||
) {
|
}
|
||||||
let sustenance = journal.get_sustenance(&sustenance_ingestion.id).unwrap();
|
IngestionKind::Hydration(hydration) => {
|
||||||
|
print!("Hydration|{}", hydration)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"Sustenance|{}|{} {}|{}|{}",
|
"|{}|{}",
|
||||||
sustenance.name,
|
|
||||||
sustenance_ingestion.amount,
|
|
||||||
sustenance.unit,
|
|
||||||
ingestion.consumer,
|
ingestion.consumer,
|
||||||
format_ingestion_time(ingestion)
|
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));
|
print!("{}", " ".repeat(depth as usize));
|
||||||
println!("Nutrients:");
|
println!("Nutrients:");
|
||||||
print!("{}", " ".repeat((depth + 1) as usize));
|
print!("{}", " ".repeat((depth + 1) as usize));
|
||||||
println!("kcal: {}", values.nutrients.kcal);
|
println!("kcal: {}", contents.nutrients.kcal);
|
||||||
|
|
||||||
print!("{}", " ".repeat(depth as usize));
|
|
||||||
println!("Includes:");
|
|
||||||
}
|
|
||||||
|
|
||||||
if tree {
|
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;
|
ingestion.kind = include;
|
||||||
|
print_includes(journal, ingestion, depth + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
print_ingestion(journal, ingestion.clone(), depth + 1, tree)
|
print_includes(journal, &mut ingestion, depth);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
IngestionKind::Hydration(hydration) => {
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) => {
|
||||||
|
|
Loading…
Reference in a new issue