This commit is contained in:
chaos 2024-11-21 10:04:06 +00:00
parent b4ec09c9af
commit 13d9436d71
3 changed files with 38 additions and 45 deletions

View file

@ -1,11 +1,11 @@
use crate::types::{CustomUnitIngestionDose, StandardIngestionDose}; use crate::types::{CustomUnitDose, StandardDose};
pub fn calulate_custom_unit_ingestion_dose( pub fn calulate_custom_unit_dose(
dose: &CustomUnitIngestionDose, dose: &CustomUnitDose,
custom_unit_dose: &CustomUnitIngestionDose, custom_unit_dose: &CustomUnitDose,
) -> StandardIngestionDose { ) -> StandardDose {
let dose: StandardIngestionDose = dose.clone().into(); let dose: StandardDose = dose.clone().into();
let custom_unit_dose: StandardIngestionDose = custom_unit_dose.clone().into(); let custom_unit_dose: StandardDose = custom_unit_dose.clone().into();
custom_unit_dose * dose custom_unit_dose * dose
} }

View file

@ -60,23 +60,23 @@ impl Mul for Estimation {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct UnknownIngestionDose { pub struct UnknownDose {
pub unit: String, pub unit: String,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct StandardIngestionDose { pub struct StandardDose {
pub dose: f64, pub dose: f64,
pub unit: String, pub unit: String,
pub contains_unknown: bool, pub contains_unknown: bool,
pub estimation: Estimation, pub estimation: Estimation,
} }
impl Add for StandardIngestionDose { impl Add for StandardDose {
type Output = Self; type Output = Self;
fn add(self, rhs: Self) -> Self::Output { fn add(self, rhs: Self) -> Self::Output {
StandardIngestionDose { StandardDose {
dose: self.dose + rhs.dose, dose: self.dose + rhs.dose,
unit: self.unit, unit: self.unit,
contains_unknown: self.contains_unknown || rhs.contains_unknown, contains_unknown: self.contains_unknown || rhs.contains_unknown,
@ -85,11 +85,11 @@ impl Add for StandardIngestionDose {
} }
} }
impl Mul for StandardIngestionDose { impl Mul for StandardDose {
type Output = Self; type Output = Self;
fn mul(self, rhs: Self) -> Self::Output { fn mul(self, rhs: Self) -> Self::Output {
StandardIngestionDose { StandardDose {
dose: self.dose * rhs.dose, dose: self.dose * rhs.dose,
unit: self.unit, unit: self.unit,
contains_unknown: self.contains_unknown || rhs.contains_unknown, contains_unknown: self.contains_unknown || rhs.contains_unknown,
@ -105,9 +105,9 @@ impl Mul for StandardIngestionDose {
} }
} }
impl From<CustomUnitIngestionDose> for StandardIngestionDose { impl From<CustomUnitDose> for StandardDose {
fn from(value: CustomUnitIngestionDose) -> StandardIngestionDose { fn from(value: CustomUnitDose) -> StandardDose {
StandardIngestionDose { StandardDose {
dose: value.dose, dose: value.dose,
unit: value.original_unit, unit: value.original_unit,
contains_unknown: false, contains_unknown: false,
@ -117,7 +117,7 @@ impl From<CustomUnitIngestionDose> for StandardIngestionDose {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct CustomUnitIngestionDose { pub struct CustomUnitDose {
pub dose: f64, pub dose: f64,
pub unit: String, pub unit: String,
pub original_unit: String, pub original_unit: String,
@ -126,10 +126,10 @@ pub struct CustomUnitIngestionDose {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum IngestionDose { pub enum Dose {
Unknown(UnknownIngestionDose), Unknown(UnknownDose),
Standard(StandardIngestionDose), Standard(StandardDose),
CustomUnit(CustomUnitIngestionDose), CustomUnit(CustomUnitDose),
} }
#[derive(PartialEq, Debug, Clone)] #[derive(PartialEq, Debug, Clone)]
@ -164,7 +164,7 @@ pub struct Ingestion {
pub substance_name: String, pub substance_name: String,
pub ingestion_time: DateTime<Utc>, pub ingestion_time: DateTime<Utc>,
pub creation_time: DateTime<Utc>, pub creation_time: DateTime<Utc>,
pub dose: IngestionDose, pub dose: Dose,
pub roa: AdministrationRoute, pub roa: AdministrationRoute,
pub consumer: Consumer, pub consumer: Consumer,
pub notes: String, pub notes: String,
@ -194,7 +194,7 @@ pub struct CustomUnit {
pub substance_name: String, pub substance_name: String,
pub administration_route: AdministrationRoute, pub administration_route: AdministrationRoute,
pub dose: CustomUnitIngestionDose, pub dose: CustomUnitDose,
pub creation_time: DateTime<Utc>, pub creation_time: DateTime<Utc>,
pub is_archived: bool, pub is_archived: bool,
@ -215,7 +215,7 @@ impl Journal {
pub fn maybe_custom_unit(&self, ingestion: &Ingestion) -> Option<CustomUnit> { pub fn maybe_custom_unit(&self, ingestion: &Ingestion) -> Option<CustomUnit> {
match &ingestion.dose { match &ingestion.dose {
IngestionDose::CustomUnit(dose) => self.get_custom_unit(dose.custom_unit_id), Dose::CustomUnit(dose) => self.get_custom_unit(dose.custom_unit_id),
_ => None, _ => None,
} }
} }
@ -246,7 +246,7 @@ impl Journal {
substance_name: custom_unit.substance_name, substance_name: custom_unit.substance_name,
administration_route: custom_unit.administration_route, administration_route: custom_unit.administration_route,
dose: CustomUnitIngestionDose { dose: CustomUnitDose {
dose: custom_unit.dose, dose: custom_unit.dose,
unit: custom_unit.unit, unit: custom_unit.unit,
original_unit: custom_unit.original_unit, original_unit: custom_unit.original_unit,
@ -300,7 +300,7 @@ impl Journal {
.get(&custom_unit_id) .get(&custom_unit_id)
.expect("custom unit not found"); .expect("custom unit not found");
IngestionDose::CustomUnit(CustomUnitIngestionDose { Dose::CustomUnit(CustomUnitDose {
dose, dose,
unit: custom_unit.dose.unit.clone(), unit: custom_unit.dose.unit.clone(),
original_unit: custom_unit.dose.original_unit.clone(), original_unit: custom_unit.dose.original_unit.clone(),
@ -308,7 +308,7 @@ impl Journal {
custom_unit_id, custom_unit_id,
}) })
} else { } else {
IngestionDose::Standard(StandardIngestionDose { Dose::Standard(StandardDose {
dose, dose,
unit: ingestion.unit, unit: ingestion.unit,
estimation, estimation,
@ -316,7 +316,7 @@ impl Journal {
}) })
} }
} else { } else {
IngestionDose::Unknown(UnknownIngestionDose { Dose::Unknown(UnknownDose {
unit: ingestion.unit, unit: ingestion.unit,
}) })
} }

View file

@ -1,22 +1,16 @@
use journal::{ use journal::{
helpers::calulate_custom_unit_ingestion_dose, helpers::calulate_custom_unit_dose,
types::{ types::{CustomUnit, CustomUnitDose, Dose, Estimation, Experience, Ingestion, StandardDose},
CustomUnit, CustomUnitIngestionDose, Estimation, Experience, Ingestion, IngestionDose,
StandardIngestionDose,
},
}; };
pub fn format_experience_title(experience: &Experience) -> String { pub fn format_experience_title(experience: &Experience) -> String {
format!("{}: {}", experience.title, experience.creation_time) format!("{}: {}", experience.title, experience.creation_time)
} }
pub fn format_ingestion_dose( pub fn format_ingestion_dose(dose: &Dose, custom_unit_dose: Option<&CustomUnitDose>) -> String {
dose: &IngestionDose,
custom_unit_dose: Option<&CustomUnitIngestionDose>,
) -> String {
match dose { match dose {
IngestionDose::Unknown(dose) => format!("Unknown {}", dose.unit), Dose::Unknown(dose) => format!("Unknown {}", dose.unit),
IngestionDose::Standard(dose) => { Dose::Standard(dose) => {
let is_estimate = dose.estimation.is_estimate(); let is_estimate = dose.estimation.is_estimate();
let estimate = if is_estimate { "~" } else { "" }; let estimate = if is_estimate { "~" } else { "" };
@ -32,21 +26,20 @@ pub fn format_ingestion_dose(
format!("{estimate}{dose_value}{standard_deviation} {unit}") format!("{estimate}{dose_value}{standard_deviation} {unit}")
} }
IngestionDose::CustomUnit(dose) => { Dose::CustomUnit(dose) => {
let custom_unit_dose = custom_unit_dose.expect("custom unit dose required"); let custom_unit_dose = custom_unit_dose.expect("custom unit dose required");
let ingestion_dose = calulate_custom_unit_ingestion_dose(dose, custom_unit_dose); let ingestion_dose = calulate_custom_unit_dose(dose, custom_unit_dose);
let ingestion_dose = let ingestion_dose = format_ingestion_dose(&Dose::Standard(ingestion_dose), None);
format_ingestion_dose(&IngestionDose::Standard(ingestion_dose), None);
let dose_per_unit = format_ingestion_dose( let dose_per_unit = format_ingestion_dose(
&IngestionDose::Standard(StandardIngestionDose::from(custom_unit_dose.clone())), &Dose::Standard(StandardDose::from(custom_unit_dose.clone())),
None, None,
); );
let custom_unit_dose = format_ingestion_dose( let custom_unit_dose = format_ingestion_dose(
&IngestionDose::Standard(StandardIngestionDose { &Dose::Standard(StandardDose {
dose: dose.dose, dose: dose.dose,
unit: custom_unit_dose.unit.clone(), unit: custom_unit_dose.unit.clone(),
contains_unknown: false, contains_unknown: false,