This commit is contained in:
chaos 2024-11-22 17:22:43 +00:00
parent aa8e5e0266
commit 0097705c82
5 changed files with 71 additions and 49 deletions

View file

@ -10,7 +10,7 @@ pub struct CustomUnit {
pub unit: String,
pub original_unit: String,
pub dose: Option<Dose>,
pub dose: Dose,
pub administration_route: AdministrationRoute,
pub creation_time: DateTime<Utc>,
@ -26,8 +26,9 @@ impl From<psychonaut_journal_types::CustomUnit> for CustomUnit {
unit: custom_unit.unit,
original_unit: custom_unit.original_unit,
dose: match custom_unit.dose {
Some(dose) => Some(Dose {
dose: {
if let Some(dose) = custom_unit.dose {
Dose {
value: dose,
contains_unknown: false,
estimation: {
@ -44,8 +45,14 @@ impl From<psychonaut_journal_types::CustomUnit> for CustomUnit {
Estimation::Precise
}
},
}),
None => None,
}
} else {
Dose {
value: 0.0,
contains_unknown: true,
estimation: Estimation::Estimate,
}
}
},
administration_route: custom_unit.administration_route,
@ -58,13 +65,15 @@ impl From<psychonaut_journal_types::CustomUnit> for CustomUnit {
#[cfg(test)]
mod tests {
use crate::types::{from_unix_millis, Dose, StandardDeviation};
use crate::types::{
from_unix_millis, AdministrationRoute, Dose, Estimation, StandardDeviation,
};
use super::{AdministrationRoute, CustomUnit};
use super::CustomUnit;
use psychonaut_journal_types::CustomUnit as PsychonautCustomUnit;
#[test]
fn conversion() {
fn psychonaut_journal_conversion() {
assert_eq!(
CustomUnit::from(PsychonautCustomUnit {
id: 0,
@ -85,14 +94,14 @@ mod tests {
substance_name: "Caffeine".to_string(),
unit: "sip".to_string(),
original_unit: "mg".to_string(),
dose: Some(Dose {
dose: Dose {
value: 10.0,
contains_unknown: false,
estimation: crate::types::Estimation::StandardDeviation(StandardDeviation {
estimation: Estimation::StandardDeviation(StandardDeviation {
expectation: 10.0,
deviation: 10.0
})
}),
},
administration_route: AdministrationRoute::Oral,
creation_time: from_unix_millis(0),
is_archived: false

View file

@ -31,7 +31,7 @@ mod tests {
use psychonaut_journal_types::Experience as PsychonautExperience;
#[test]
fn conversion() {
fn psychonaut_journal_conversion() {
assert_eq!(
Experience::from(PsychonautExperience {
title: "Experience".to_string(),

View file

@ -10,7 +10,7 @@ pub struct Ingestion {
pub substance_name: String,
pub ingestion_time: DateTime<Utc>,
pub creation_time: DateTime<Utc>,
pub dose: Option<Dose>,
pub dose: Dose,
pub unit: Unit,
pub roa: AdministrationRoute,
pub consumer: Consumer,
@ -26,7 +26,7 @@ impl From<psychonaut_journal_types::Ingestion> for Ingestion {
creation_time: from_unix_millis(ingestion.creation_time),
dose: match ingestion.dose {
Some(value) => Some(Dose {
Some(value) => Dose {
value,
contains_unknown: false,
estimation: {
@ -41,8 +41,12 @@ impl From<psychonaut_journal_types::Ingestion> for Ingestion {
Estimation::Estimate
}
},
}),
None => None,
},
None => Dose {
value: 0.0,
contains_unknown: true,
estimation: Estimation::Estimate,
},
},
unit: match ingestion.custom_unit_id {
@ -71,7 +75,7 @@ mod tests {
use psychonaut_journal_types::Ingestion as PsychonautIngestion;
#[test]
fn conversion() {
fn psychonaut_journal_conversion() {
assert_eq!(
Ingestion::from(PsychonautIngestion {
substance_name: "Caffeine".to_string(),
@ -91,11 +95,11 @@ mod tests {
substance_name: "Caffeine".to_string(),
ingestion_time: from_unix_millis(0),
creation_time: from_unix_millis(0),
dose: Some(Dose {
dose: Dose {
value: 10.0,
contains_unknown: false,
estimation: Estimation::Precise
}),
},
unit: Unit::Simple("mg".to_string()),
roa: AdministrationRoute::Oral,
consumer: Consumer::Default,

View file

@ -34,7 +34,7 @@ pub fn print_ingestion_log(
println!(
"{}|{}|{}|{}|{}",
ingestion.substance_name,
format_ingestion_dose(ingestion.dose.as_ref(), &unit),
format_ingestion_dose(&ingestion.dose, &unit),
format_ingestion_roa(ingestion, &unit),
ingestion.consumer,
format_ingestion_time(ingestion)

View file

@ -4,9 +4,9 @@ pub fn format_experience_title(experience: &Experience) -> String {
format!("{}: {}", experience.title, experience.creation_time)
}
pub fn format_ingestion_dose(dose: Option<&Dose>, unit: &Unit) -> String {
match dose {
Some(dose) => match unit {
pub fn format_ingestion_dose(dose: &Dose, unit: &Unit) -> String {
if dose.value != 0.0 && !dose.contains_unknown {
match unit {
Unit::Simple(unit) => {
let is_estimate = dose.estimation.is_estimate();
@ -31,29 +31,27 @@ pub fn format_ingestion_dose(dose: Option<&Dose>, unit: &Unit) -> String {
let unit = unit.clone().unwrap();
let unit_unit = Unit::Simple(unit.unit.clone());
let ingestion_dose = match &unit.dose {
Some(unit_dose) => dose * unit_dose,
None => dose.clone(),
};
let ingestion_dose = dose * &unit.dose;
let ingestion_unit = Unit::Simple(unit.original_unit.clone());
let ingestion_dose = format_ingestion_dose(Some(&ingestion_dose), &ingestion_unit);
let ingestion_dose = format_ingestion_dose(&ingestion_dose, &ingestion_unit);
let dose_per_unit = format_ingestion_dose(unit.dose.as_ref(), &ingestion_unit);
let dose_per_unit = format_ingestion_dose(&unit.dose, &ingestion_unit);
let custom_unit_dose = format_ingestion_dose(Some(dose), &unit_unit);
let custom_unit_dose = format_ingestion_dose(dose, &unit_unit);
format!("{ingestion_dose} ({dose_per_unit} * {custom_unit_dose})")
}
},
None => format!(
}
} else {
format!(
"Unknown {}",
match unit {
Unit::Simple(unit) => unit,
Unit::Custom { id: _id, unit } => &unit.as_ref().unwrap().original_unit,
}
),
)
}
}
@ -77,14 +75,25 @@ mod tests {
#[test]
fn format_unknown_dose() {
let result = format_ingestion_dose(None, &Unit::Simple("mg".to_string()));
let result = format_ingestion_dose(
&Dose {
value: 0.0,
contains_unknown: true,
estimation: Estimation::Estimate,
},
&Unit::Simple("mg".to_string()),
);
assert_eq!(result, "Unknown mg");
}
#[test]
fn format_unknown_dose_custom_unit() {
let result = format_ingestion_dose(
None,
&Dose {
value: 0.0,
contains_unknown: true,
estimation: Estimation::Estimate,
},
&Unit::Custom {
id: 0,
unit: Some(CustomUnit {