From cb2b13a9cf3da17d050ce6c6e7d1b6233d80fc09 Mon Sep 17 00:00:00 2001 From: chaos Date: Mon, 25 Nov 2024 02:28:08 +0000 Subject: [PATCH] update --- journal/src/journal.rs | 5 +- journal/src/journal/json_journal.rs | 36 ++++----- journal/src/types/ingestion.rs | 41 +++------- journal/src/types/ingestion/kind.rs | 30 ++++++++ journal/src/types/ingestion/substance.rs | 11 +++ journal/src/types/ingestion/sustenance.rs | 6 ++ journal/src/types/mod.rs | 4 +- journal_cli/src/display.rs | 94 ++++++++--------------- 8 files changed, 111 insertions(+), 116 deletions(-) create mode 100644 journal/src/types/ingestion/kind.rs create mode 100644 journal/src/types/ingestion/substance.rs create mode 100644 journal/src/types/ingestion/sustenance.rs diff --git a/journal/src/journal.rs b/journal/src/journal.rs index d91349e..74a87f8 100644 --- a/journal/src/journal.rs +++ b/journal/src/journal.rs @@ -1,5 +1,6 @@ use crate::types::{ - CustomUnit, Ingestion, IngestionKind, Session, Substance, SubstanceIngestion, Sustenance, Unit, + CustomUnit, Ingestion, IngestionKind, IngestionKinds, Session, Substance, SubstanceIngestion, + Sustenance, Unit, }; pub type JournalType = Box; @@ -11,7 +12,7 @@ pub trait Journal { fn get_session_ingestions(&self, id: i32) -> Option>; fn get_substance(&self, name: &str) -> Option; fn get_sustenance(&self, id: &str) -> Option; - fn get_sustenance_ingestions(&self, id: &str) -> Option>; + fn get_ingestion_kinds(&self, ingestion: &IngestionKind) -> Option; fn get_custom_unit(&self, id: i32) -> Option; fn create_session(&mut self, title: &str) -> Session; diff --git a/journal/src/journal/json_journal.rs b/journal/src/journal/json_journal.rs index d0e796e..77d624b 100644 --- a/journal/src/journal/json_journal.rs +++ b/journal/src/journal/json_journal.rs @@ -4,7 +4,8 @@ use chrono::Utc; use rand::Rng; use crate::types::{ - CustomUnit, Ingestion, IngestionKind, Session, Substance, SubstanceIngestion, Sustenance, Unit, + CustomUnit, Ingestion, IngestionKind, IngestionKinds, Session, Substance, SubstanceIngestion, + Sustenance, Unit, }; use super::{Journal, JournalType}; @@ -82,32 +83,25 @@ impl Journal for JSONJournal { self.data.sustenances.get(sustenance_id).cloned() } - fn get_sustenance_ingestions(&self, sustenance_id: &str) -> Option> { - let sustenance = self.get_sustenance(sustenance_id)?; + fn get_ingestion_kinds(&self, kind: &IngestionKind) -> Option { + let mut kinds: IngestionKinds = IngestionKinds::from(kind); - let mut ingestions: Vec = Vec::new(); - for include in sustenance.includes.into_iter() { - match include { - IngestionKind::Unknown => continue, - IngestionKind::Hydration { amount } => { - ingestions.push(IngestionKind::Hydration { amount }) - } - IngestionKind::Substance(substance_ingestion) => { - ingestions.push(IngestionKind::Substance(substance_ingestion)) - } - IngestionKind::Sustenance(sustenance_ingestion) => { - ingestions.push(IngestionKind::Sustenance(sustenance_ingestion.clone())); + if let IngestionKind::Sustenance(sustenance) = kind { + let includes = self.get_sustenance(&sustenance.sustenance_id)?.includes; - ingestions.append( - &mut self - .get_sustenance_ingestions(&sustenance_ingestion.sustenance_id) - .unwrap(), - ); + for include in includes.into_iter() { + if let IngestionKind::Sustenance(_) = include { + kinds.ingestions.push(IngestionKinds { + ingestion: include.clone(), + ingestions: self.get_ingestion_kinds(&include).unwrap().ingestions, + }); + } else { + kinds.ingestions.push(IngestionKinds::from(&include)) } } } - Some(ingestions) + Some(kinds) } fn get_custom_unit(&self, id: i32) -> Option { diff --git a/journal/src/types/ingestion.rs b/journal/src/types/ingestion.rs index ec5c71e..7b6eea5 100644 --- a/journal/src/types/ingestion.rs +++ b/journal/src/types/ingestion.rs @@ -1,6 +1,15 @@ use chrono::{DateTime, Utc}; -use super::{dose::Dose, from_unix_millis, AdministrationRoute, Consumer}; +use super::{dose::Dose, from_unix_millis, Consumer}; + +mod kind; +pub use kind::{IngestionKind, IngestionKinds}; + +mod substance; +pub use substance::SubstanceIngestion; + +mod sustenance; +pub use sustenance::SustenanceIngestion; #[derive(PartialEq, Default, Debug, Clone)] #[derive(serde::Serialize, serde::Deserialize)] @@ -12,36 +21,6 @@ pub struct Ingestion { pub kind: IngestionKind, } -#[derive(PartialEq, Default, Debug, Clone)] -#[derive(serde::Serialize, serde::Deserialize)] -#[serde(tag = "type", rename_all = "lowercase")] -pub enum IngestionKind { - #[default] - Unknown, - Substance(SubstanceIngestion), - Sustenance(SustenanceIngestion), - Hydration { - amount: u64, - }, -} - -#[derive(PartialEq, Default, Debug, Clone)] -#[derive(serde::Serialize, serde::Deserialize)] -pub struct SubstanceIngestion { - pub substance_name: String, - pub dose: Dose, - pub custom_unit_id: Option, - pub roa: AdministrationRoute, - pub stomach_fullness: Option, -} - -#[derive(PartialEq, Default, Debug, Clone)] -#[derive(serde::Serialize, serde::Deserialize)] -pub struct SustenanceIngestion { - pub sustenance_id: String, - pub amount: f64, -} - impl From for Ingestion { fn from(ingestion: psychonaut_journal_types::Ingestion) -> Self { Ingestion { diff --git a/journal/src/types/ingestion/kind.rs b/journal/src/types/ingestion/kind.rs new file mode 100644 index 0000000..9249b0d --- /dev/null +++ b/journal/src/types/ingestion/kind.rs @@ -0,0 +1,30 @@ +use super::{SubstanceIngestion, SustenanceIngestion}; + +#[derive(PartialEq, Default, Debug, Clone)] +#[derive(serde::Serialize, serde::Deserialize)] +#[serde(tag = "type", rename_all = "lowercase")] +pub enum IngestionKind { + #[default] + Unknown, + Substance(SubstanceIngestion), + Sustenance(SustenanceIngestion), + Hydration { + amount: u64, + }, +} + +#[derive(PartialEq, Default, Debug, Clone)] +#[derive(serde::Serialize, serde::Deserialize)] +pub struct IngestionKinds { + pub ingestion: IngestionKind, + pub ingestions: Vec, +} + +impl From<&IngestionKind> for IngestionKinds { + fn from(value: &IngestionKind) -> Self { + IngestionKinds { + ingestion: value.clone(), + ..Default::default() + } + } +} diff --git a/journal/src/types/ingestion/substance.rs b/journal/src/types/ingestion/substance.rs new file mode 100644 index 0000000..3a28e65 --- /dev/null +++ b/journal/src/types/ingestion/substance.rs @@ -0,0 +1,11 @@ +use crate::types::{AdministrationRoute, Dose}; + +#[derive(PartialEq, Default, Debug, Clone)] +#[derive(serde::Serialize, serde::Deserialize)] +pub struct SubstanceIngestion { + pub substance_name: String, + pub dose: Dose, + pub custom_unit_id: Option, + pub roa: AdministrationRoute, + pub stomach_fullness: Option, +} diff --git a/journal/src/types/ingestion/sustenance.rs b/journal/src/types/ingestion/sustenance.rs new file mode 100644 index 0000000..0a7e671 --- /dev/null +++ b/journal/src/types/ingestion/sustenance.rs @@ -0,0 +1,6 @@ +#[derive(PartialEq, Default, Debug, Clone)] +#[derive(serde::Serialize, serde::Deserialize)] +pub struct SustenanceIngestion { + pub sustenance_id: String, + pub amount: f64, +} diff --git a/journal/src/types/mod.rs b/journal/src/types/mod.rs index a20a912..7b13f55 100644 --- a/journal/src/types/mod.rs +++ b/journal/src/types/mod.rs @@ -13,7 +13,9 @@ mod consumer; pub use consumer::Consumer; mod ingestion; -pub use ingestion::{Ingestion, IngestionKind, SubstanceIngestion, SustenanceIngestion}; +pub use ingestion::{ + Ingestion, IngestionKind, IngestionKinds, SubstanceIngestion, SustenanceIngestion, +}; mod substance; pub use substance::Substance; diff --git a/journal_cli/src/display.rs b/journal_cli/src/display.rs index 1d6d2a5..5fee6a6 100644 --- a/journal_cli/src/display.rs +++ b/journal_cli/src/display.rs @@ -1,8 +1,8 @@ use journal::{ journal::JournalType, types::{ - format_dose, Consumer, Dose, Ingestion, IngestionKind, Session, SubstanceIngestion, - SustenanceIngestion, + format_dose, Consumer, Dose, Ingestion, IngestionKind, IngestionKinds, Session, Substance, + SubstanceIngestion, SustenanceIngestion, }, }; @@ -44,55 +44,31 @@ fn print_sustenance_ingestion( ) } -pub fn resolve_ingestion(journal: &JournalType, ingestion: &IngestionKind) -> Vec { - //println!("{ingestion:?}"); +pub fn resolve_ingestion_kinds(journal: &JournalType, kind: &mut IngestionKinds) { + //println!("{kind:#?}"); - let mut ingestions: Vec = Vec::new(); - - match &ingestion { - IngestionKind::Substance(substance) => { - ingestions.push(IngestionKind::Substance(substance.clone())); - } - IngestionKind::Hydration { amount: _ } => { - ingestions.push(ingestion.clone()); - } - IngestionKind::Sustenance(sustenance) => { - let sustenance_ingestions = journal - .get_sustenance_ingestions(&sustenance.sustenance_id) - .unwrap(); - - for sustenance_ingestion in sustenance_ingestions.into_iter() { - match sustenance_ingestion { - IngestionKind::Substance(substance_ingestion) => { - ingestions.push(IngestionKind::Substance(SubstanceIngestion { - dose: Dose::new_precise(sustenance.amount) * substance_ingestion.dose, - ..substance_ingestion - })) - } - IngestionKind::Sustenance(sustenance_ingestion) => { - ingestions.push(IngestionKind::Sustenance(SustenanceIngestion { - amount: sustenance.amount * sustenance_ingestion.amount, - ..sustenance_ingestion - })) - } - IngestionKind::Hydration { amount } => { - ingestions.push(IngestionKind::Hydration { - amount: (sustenance.amount * amount as f64).round() as u64, - }) - } - _ => {} + if let IngestionKind::Sustenance(sustenance) = &kind.ingestion { + for kinds in kind.ingestions.iter_mut() { + match &mut kinds.ingestion { + IngestionKind::Substance(ingestion) => { + ingestion.dose = Dose::new_precise(sustenance.amount) * ingestion.dose.clone(); } + IngestionKind::Sustenance(ingestion) => { + ingestion.amount = sustenance.amount * ingestion.amount; + + resolve_ingestion_kinds(journal, kinds) + } + IngestionKind::Hydration { amount } => { + *amount = (sustenance.amount * *amount as f64).round() as u64 + } + _ => {} } } - _ => {} - }; - - //println!("{ingestions:#?}"); - ingestions + } } -pub fn print_ingestion(journal: &JournalType, ingestion: &Ingestion, kind: &IngestionKind) { - match kind { +pub fn print_ingestion_kinds(journal: &JournalType, ingestion: &Ingestion, kinds: &IngestionKinds) { + match &kinds.ingestion { IngestionKind::Unknown => {} IngestionKind::Substance(substance_ingestion) => { print_substance_ingestion(journal, ingestion, substance_ingestion); @@ -109,6 +85,10 @@ pub fn print_ingestion(journal: &JournalType, ingestion: &Ingestion, kind: &Inge ) } } + + for kinds in &kinds.ingestions { + print_ingestion_kinds(journal, ingestion, kinds); + } } pub fn print_ingestion_log( @@ -116,29 +96,21 @@ pub fn print_ingestion_log( session: &Session, consumer_filter: Option<&Vec>, ) { - for ingestion in journal + let ingestions = journal .get_session_ingestions(session.id) - .expect("could not find ingestions for session") - .iter() - { + .expect("could not find ingestions for session"); + + for ingestion in ingestions.iter() { if let Some(consumer_filter) = consumer_filter { if !consumer_filter.contains(&ingestion.consumer) { continue; } } - match &ingestion.kind { - IngestionKind::Sustenance(_) => { - print_ingestion(journal, ingestion, &ingestion.kind); + let mut ingestion_kinds = journal.get_ingestion_kinds(&ingestion.kind).unwrap(); + println!("{ingestion_kinds:#?}"); - for kind in resolve_ingestion(journal, &ingestion.kind) { - print!("- "); - print_ingestion(journal, ingestion, &kind) - } - } - _ => { - print_ingestion(journal, ingestion, &ingestion.kind); - } - } + resolve_ingestion_kinds(journal, &mut ingestion_kinds); + print_ingestion_kinds(journal, ingestion, &ingestion_kinds) } }