use journal::{ helpers::calulate_custom_unit_ingestion_dose, types::{ CustomUnit, CustomUnitDose, Estimation, Experience, Ingestion, IngestionDose, StandardIngestionDose, }, }; 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<&CustomUnitDose>, ) -> String { match dose { IngestionDose::Unknown(dose) => format!("Unknown {}", dose.unit), IngestionDose::Standard(dose) => { let is_estimate = dose.estimation.is_estimate(); let estimate = if is_estimate { "~" } else { "" }; let standard_deviation = { if let Estimation::StandardDeviation(standard_deviation) = dose.estimation { format!("±{}", (standard_deviation * 100.0).round() / 100.0) } else { "".to_string() } }; let dose_value = (dose.dose * 100.0).round() / 100.0; let unit = dose.unit.clone(); format!("{estimate}{dose_value}{standard_deviation} {unit}") } IngestionDose::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 = format_ingestion_dose(&ingestion_dose, None); let dose_per_unit = format_ingestion_dose( &IngestionDose::Standard(StandardIngestionDose::from(custom_unit_dose.clone())), None, ); let custom_unit_dose = format_ingestion_dose( &IngestionDose::Standard(StandardIngestionDose { dose: dose.dose, unit: custom_unit_dose.unit.clone(), contains_unknown: false, estimation: dose.estimation, }), None, ); format!("{ingestion_dose} ({dose_per_unit} * {custom_unit_dose})") } } } pub fn format_ingestion_time(ingestion: &Ingestion) -> String { ingestion.ingestion_time.format("%a %I:%M %p").to_string() } pub fn format_ingestion_roa(ingestion: &Ingestion, custom_unit: Option<&CustomUnit>) -> String { if let Some(custom_unit) = custom_unit { format!("{:?} ({})", ingestion.roa, custom_unit.name) } else { format!("{:?}", ingestion.roa) } }