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

View file

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

View file

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

View file

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

View file

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