use journal::types::{Dose, Estimation, Experience, Ingestion, Unit}; pub fn format_experience_title(experience: &Experience) -> String { format!("{}: {}", experience.title, experience.creation_time) } 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(); let estimate = if is_estimate { "~" } else { "" }; let standard_deviation = { if let Estimation::StandardDeviation(deviation) = dose.estimation { format!("±{}", (deviation.deviation * 100.0).round() / 100.0) } else { "".to_string() } }; let unknown = if dose.contains_unknown { "+Unknown" } else { "" }; let dose = (dose.value * 100.0).round() / 100.0; format!("{estimate}{dose}{standard_deviation}{unknown} {unit}") } Unit::Custom { id: _id, unit } => { let unit = unit.clone().unwrap(); let unit_unit = Unit::Simple(unit.unit.clone()); let ingestion_dose = dose * &unit.dose; let ingestion_unit = Unit::Simple(unit.original_unit.clone()); let ingestion_dose = format_ingestion_dose(&ingestion_dose, &ingestion_unit); let dose_per_unit = format_ingestion_dose(&unit.dose, &ingestion_unit); let custom_unit_dose = format_ingestion_dose(dose, &unit_unit); format!("{ingestion_dose} ({dose_per_unit} * {custom_unit_dose})") } } } else { format!( "Unknown {}", match unit { Unit::Simple(unit) => unit, Unit::Custom { id: _id, unit } => &unit.as_ref().unwrap().original_unit, } ) } } 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, unit: &Unit) -> String { if let Unit::Custom { id: _id, unit } = unit { format!("{:?} ({})", ingestion.roa, &unit.as_ref().unwrap().name) } else { format!("{:?}", ingestion.roa) } } #[cfg(test)] mod tests { use journal::types::CustomUnit; use super::*; #[test] fn format_unknown_dose() { 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( &Dose { value: 0.0, contains_unknown: true, estimation: Estimation::Estimate, }, &Unit::Custom { id: 0, unit: Some(CustomUnit { original_unit: "mg".to_string(), ..CustomUnit::default() }), }, ); assert_eq!(result, "Unknown mg"); } }