From f4696e0b31c4b60bfceb8a6ab27df6fe17e07865 Mon Sep 17 00:00:00 2001 From: chaos Date: Sun, 24 Nov 2024 13:10:19 +0000 Subject: [PATCH] update --- journal/src/journal/json_journal.rs | 8 +------- journal/src/types/dose_with_unit.rs | 25 +++++++++++-------------- journal/src/types/ingestion.rs | 20 ++++++++++++++++++-- journal/src/types/unit.rs | 2 +- journal_cli/src/display.rs | 2 +- journal_cli/src/formatting.rs | 15 ++++++--------- 6 files changed, 38 insertions(+), 34 deletions(-) diff --git a/journal/src/journal/json_journal.rs b/journal/src/journal/json_journal.rs index 4494f69..e921b1e 100644 --- a/journal/src/journal/json_journal.rs +++ b/journal/src/journal/json_journal.rs @@ -118,13 +118,7 @@ impl Journal for JSONJournal { Some(substance) => Some(Unit::Simple(substance.unit)), None => None, }, - Some(custom_unit_id) => match self.get_custom_unit(custom_unit_id) { - Some(custom_unit) => Some(Unit::Custom { - id: custom_unit_id, - unit: Some(custom_unit), - }), - None => None, - }, + Some(custom_unit_id) => self.get_custom_unit(custom_unit_id).map(Unit::Custom), } } diff --git a/journal/src/types/dose_with_unit.rs b/journal/src/types/dose_with_unit.rs index f176552..19deaec 100644 --- a/journal/src/types/dose_with_unit.rs +++ b/journal/src/types/dose_with_unit.rs @@ -60,8 +60,8 @@ impl Display for DoseWithUnitRefs<'_> { f.write_str((" ".to_string() + unit).as_str()) } - Unit::Custom { id: _id, unit } => { - let unit = unit.clone().unwrap(); + Unit::Custom(unit) => { + let unit = unit.clone(); let unit_unit = Unit::Simple(unit.unit.clone()); let ingestion_dose = &unit.dose * dose; @@ -165,18 +165,15 @@ mod tests { fn format_unknown_dose_with_custom_unit() { let result = super::format_dose( &Dose::new_unknown(), - &Unit::Custom { - id: 0, - unit: Some(CustomUnit { - name: "Beverage".to_string(), - substance_name: "Caffeine".to_string(), - unit: "sip".to_string(), - original_unit: "mg".to_string(), - dose: Dose::new_estimate(10.0), - administration_route: AdministrationRoute::Oral, - ..CustomUnit::default() - }), - }, + &Unit::Custom(CustomUnit { + name: "Beverage".to_string(), + substance_name: "Caffeine".to_string(), + unit: "sip".to_string(), + original_unit: "mg".to_string(), + dose: Dose::new_estimate(10.0), + administration_route: AdministrationRoute::Oral, + ..CustomUnit::default() + }), ); assert_eq!(result, "Unknown mg (~10 mg * Unknown sip)"); } diff --git a/journal/src/types/ingestion.rs b/journal/src/types/ingestion.rs index 710dcc6..6d0cb29 100644 --- a/journal/src/types/ingestion.rs +++ b/journal/src/types/ingestion.rs @@ -1,6 +1,6 @@ use chrono::{DateTime, Utc}; -use super::{dose::Dose, from_unix_millis, AdministrationRoute, Consumer, Unit}; +use super::{dose::Dose, from_unix_millis, AdministrationRoute, Consumer}; #[derive(PartialEq, Default, Debug, Clone)] #[derive(serde::Serialize, serde::Deserialize)] @@ -16,6 +16,22 @@ pub struct Ingestion { pub stomach_fullness: Option, } +pub enum IngestionKind { + Substance(SubstanceIngestion), +} + +#[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 consumer: Consumer, + pub notes: String, + pub stomach_fullness: Option, +} + impl From for Ingestion { fn from(ingestion: psychonaut_journal_types::Ingestion) -> Self { Ingestion { @@ -55,7 +71,7 @@ impl From for Ingestion { #[cfg(test)] mod tests { - use crate::types::{from_unix_millis, AdministrationRoute, Consumer, Dose, Unit}; + use crate::types::{from_unix_millis, AdministrationRoute, Consumer, Dose}; use super::Ingestion; use psychonaut_journal_types::Ingestion as PsychonautIngestion; diff --git a/journal/src/types/unit.rs b/journal/src/types/unit.rs index 02b5df3..ee3c192 100644 --- a/journal/src/types/unit.rs +++ b/journal/src/types/unit.rs @@ -4,7 +4,7 @@ use super::CustomUnit; #[derive(serde::Serialize, serde::Deserialize)] pub enum Unit { Simple(String), - Custom { id: i32, unit: Option }, + Custom(CustomUnit), } impl Default for Unit { diff --git a/journal_cli/src/display.rs b/journal_cli/src/display.rs index f2d3b04..6f22954 100644 --- a/journal_cli/src/display.rs +++ b/journal_cli/src/display.rs @@ -28,7 +28,7 @@ pub fn print_ingestion_log( } } - let unit = journal.resolve_unit(&ingestion).unwrap(); + let unit = journal.resolve_unit(ingestion).unwrap(); // println!("{:#?} {:#?}", &ingestion, &custom_unit); println!( diff --git a/journal_cli/src/formatting.rs b/journal_cli/src/formatting.rs index b813252..1137c1a 100644 --- a/journal_cli/src/formatting.rs +++ b/journal_cli/src/formatting.rs @@ -9,8 +9,8 @@ pub fn format_ingestion_time(ingestion: &Ingestion) -> String { } pub fn format_ingestion_roa(ingestion: &Ingestion, unit: &Unit) -> String { - if let Unit::Custom { id: _id, unit } = unit { - format!("{:?} ({})", ingestion.roa, &unit.as_ref().unwrap().name) + if let Unit::Custom(unit) = unit { + format!("{:?} ({})", ingestion.roa, &unit.name) } else { format!("{:?}", ingestion.roa) } @@ -61,13 +61,10 @@ mod tests { roa: AdministrationRoute::Oral, ..Ingestion::default() }, - &Unit::Custom { - id: 0, - unit: Some(CustomUnit { - name: "32mg/ml".to_string(), - ..CustomUnit::default() - }), - }, + &Unit::Custom(CustomUnit { + name: "32mg/ml".to_string(), + ..CustomUnit::default() + }), ); assert_eq!(result, "Oral (32mg/ml)"); }