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(
dose: &CustomUnitIngestionDose,
custom_unit_dose: &CustomUnitIngestionDose,
) -> StandardIngestionDose {
let dose: StandardIngestionDose = dose.clone().into();
let custom_unit_dose: StandardIngestionDose = custom_unit_dose.clone().into();
pub fn calulate_custom_unit_dose(
dose: &CustomUnitDose,
custom_unit_dose: &CustomUnitDose,
) -> StandardDose {
let dose: StandardDose = dose.clone().into();
let custom_unit_dose: StandardDose = custom_unit_dose.clone().into();
custom_unit_dose * dose
}

View file

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

View file

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