use journal::{ helpers::calulate_custom_unit_dose, types::{ doses::{CustomUnitDose, Dose, StandardDose}, CustomUnit, Estimation, Experience, Ingestion, }, }; pub fn format_experience_title(experience: &Experience) -> String { format!("{}: {}", experience.title, experience.creation_time) } pub fn format_ingestion_dose(dose: &Dose, custom_unit_dose: Option<&CustomUnitDose>) -> String { match dose { Dose::Unknown(dose) => format!("Unknown {}", dose.unit), Dose::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}") } Dose::CustomUnit(dose) => { let custom_unit_dose = custom_unit_dose.expect("custom unit dose required"); let ingestion_dose = calulate_custom_unit_dose(dose, custom_unit_dose); let ingestion_dose = format_ingestion_dose(&Dose::Standard(ingestion_dose), None); let dose_per_unit = format_ingestion_dose( &Dose::Standard(StandardDose::from(custom_unit_dose.clone())), None, ); let custom_unit_dose = format_ingestion_dose( &Dose::Standard(StandardDose { 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) } }