update
This commit is contained in:
parent
d7fb6f147a
commit
cb2b13a9cf
|
@ -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<dyn Journal>;
|
||||
|
@ -11,7 +12,7 @@ pub trait Journal {
|
|||
fn get_session_ingestions(&self, id: i32) -> Option<Vec<Ingestion>>;
|
||||
fn get_substance(&self, name: &str) -> Option<Substance>;
|
||||
fn get_sustenance(&self, id: &str) -> Option<Sustenance>;
|
||||
fn get_sustenance_ingestions(&self, id: &str) -> Option<Vec<IngestionKind>>;
|
||||
fn get_ingestion_kinds(&self, ingestion: &IngestionKind) -> Option<IngestionKinds>;
|
||||
fn get_custom_unit(&self, id: i32) -> Option<CustomUnit>;
|
||||
|
||||
fn create_session(&mut self, title: &str) -> Session;
|
||||
|
|
|
@ -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<Vec<IngestionKind>> {
|
||||
let sustenance = self.get_sustenance(sustenance_id)?;
|
||||
fn get_ingestion_kinds(&self, kind: &IngestionKind) -> Option<IngestionKinds> {
|
||||
let mut kinds: IngestionKinds = IngestionKinds::from(kind);
|
||||
|
||||
let mut ingestions: Vec<IngestionKind> = 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<CustomUnit> {
|
||||
|
|
|
@ -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<i32>,
|
||||
pub roa: AdministrationRoute,
|
||||
pub stomach_fullness: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Default, Debug, Clone)]
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
pub struct SustenanceIngestion {
|
||||
pub sustenance_id: String,
|
||||
pub amount: f64,
|
||||
}
|
||||
|
||||
impl From<psychonaut_journal_types::Ingestion> for Ingestion {
|
||||
fn from(ingestion: psychonaut_journal_types::Ingestion) -> Self {
|
||||
Ingestion {
|
||||
|
|
30
journal/src/types/ingestion/kind.rs
Normal file
30
journal/src/types/ingestion/kind.rs
Normal file
|
@ -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<IngestionKinds>,
|
||||
}
|
||||
|
||||
impl From<&IngestionKind> for IngestionKinds {
|
||||
fn from(value: &IngestionKind) -> Self {
|
||||
IngestionKinds {
|
||||
ingestion: value.clone(),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
11
journal/src/types/ingestion/substance.rs
Normal file
11
journal/src/types/ingestion/substance.rs
Normal file
|
@ -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<i32>,
|
||||
pub roa: AdministrationRoute,
|
||||
pub stomach_fullness: Option<String>,
|
||||
}
|
6
journal/src/types/ingestion/sustenance.rs
Normal file
6
journal/src/types/ingestion/sustenance.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
#[derive(PartialEq, Default, Debug, Clone)]
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
pub struct SustenanceIngestion {
|
||||
pub sustenance_id: String,
|
||||
pub amount: f64,
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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<IngestionKind> {
|
||||
//println!("{ingestion:?}");
|
||||
pub fn resolve_ingestion_kinds(journal: &JournalType, kind: &mut IngestionKinds) {
|
||||
//println!("{kind:#?}");
|
||||
|
||||
let mut ingestions: Vec<IngestionKind> = 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<Consumer>>,
|
||||
) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue