diff --git a/journal/src/types/custom_unit.rs b/journal/src/types/custom_unit.rs index 12aea3e..d4a1dbf 100644 --- a/journal/src/types/custom_unit.rs +++ b/journal/src/types/custom_unit.rs @@ -10,7 +10,7 @@ pub struct CustomUnit { pub unit: String, pub original_unit: String, - pub dose: Option, + pub dose: Dose, pub administration_route: AdministrationRoute, pub creation_time: DateTime, @@ -26,26 +26,33 @@ impl From for CustomUnit { unit: custom_unit.unit, original_unit: custom_unit.original_unit, - dose: match custom_unit.dose { - Some(dose) => Some(Dose { - value: dose, - contains_unknown: false, - estimation: { - if custom_unit.is_estimate { - if let Some(deviation) = custom_unit.estimate_standard_deviation { - Estimation::StandardDeviation(StandardDeviation { - expectation: custom_unit.dose.unwrap_or_default(), - deviation, - }) + dose: { + if let Some(dose) = custom_unit.dose { + Dose { + value: dose, + contains_unknown: false, + estimation: { + if custom_unit.is_estimate { + if let Some(deviation) = custom_unit.estimate_standard_deviation { + Estimation::StandardDeviation(StandardDeviation { + expectation: custom_unit.dose.unwrap_or_default(), + deviation, + }) + } else { + Estimation::Estimate + } } else { - Estimation::Estimate + Estimation::Precise } - } else { - 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 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 diff --git a/journal/src/types/experience.rs b/journal/src/types/experience.rs index c3650a9..82b277c 100644 --- a/journal/src/types/experience.rs +++ b/journal/src/types/experience.rs @@ -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(), diff --git a/journal/src/types/ingestion.rs b/journal/src/types/ingestion.rs index 638dfcd..31afa4c 100644 --- a/journal/src/types/ingestion.rs +++ b/journal/src/types/ingestion.rs @@ -10,7 +10,7 @@ pub struct Ingestion { pub substance_name: String, pub ingestion_time: DateTime, pub creation_time: DateTime, - pub dose: Option, + pub dose: Dose, pub unit: Unit, pub roa: AdministrationRoute, pub consumer: Consumer, @@ -26,7 +26,7 @@ impl From 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 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, diff --git a/journal_cli/src/display.rs b/journal_cli/src/display.rs index 3403d71..f1c4451 100644 --- a/journal_cli/src/display.rs +++ b/journal_cli/src/display.rs @@ -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) diff --git a/journal_cli/src/formatting.rs b/journal_cli/src/formatting.rs index 9d07339..ef7ffd7 100644 --- a/journal_cli/src/formatting.rs +++ b/journal_cli/src/formatting.rs @@ -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 {