This commit is contained in:
chaos 2024-11-24 13:10:19 +00:00
parent ea544ad4af
commit f4696e0b31
6 changed files with 38 additions and 34 deletions

View file

@ -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),
}
}

View file

@ -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,9 +165,7 @@ mod tests {
fn format_unknown_dose_with_custom_unit() {
let result = super::format_dose(
&Dose::new_unknown(),
&Unit::Custom {
id: 0,
unit: Some(CustomUnit {
&Unit::Custom(CustomUnit {
name: "Beverage".to_string(),
substance_name: "Caffeine".to_string(),
unit: "sip".to_string(),
@ -176,7 +174,6 @@ mod tests {
administration_route: AdministrationRoute::Oral,
..CustomUnit::default()
}),
},
);
assert_eq!(result, "Unknown mg (~10 mg * Unknown sip)");
}

View file

@ -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<String>,
}
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<i32>,
pub roa: AdministrationRoute,
pub consumer: Consumer,
pub notes: String,
pub stomach_fullness: Option<String>,
}
impl From<psychonaut_journal_types::Ingestion> for Ingestion {
fn from(ingestion: psychonaut_journal_types::Ingestion) -> Self {
Ingestion {
@ -55,7 +71,7 @@ impl From<psychonaut_journal_types::Ingestion> 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;

View file

@ -4,7 +4,7 @@ use super::CustomUnit;
#[derive(serde::Serialize, serde::Deserialize)]
pub enum Unit {
Simple(String),
Custom { id: i32, unit: Option<CustomUnit> },
Custom(CustomUnit),
}
impl Default for Unit {

View file

@ -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!(

View file

@ -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 {
&Unit::Custom(CustomUnit {
name: "32mg/ml".to_string(),
..CustomUnit::default()
}),
},
);
assert_eq!(result, "Oral (32mg/ml)");
}