journal/journal_cli/src/formatting.rs

67 lines
2 KiB
Rust
Raw Normal View History

2024-11-20 19:15:07 +00:00
use journal::{
2024-11-21 10:04:06 +00:00
helpers::calulate_custom_unit_dose,
types::{CustomUnit, CustomUnitDose, Dose, Estimation, Experience, Ingestion, StandardDose},
2024-11-20 19:15:07 +00:00
};
pub fn format_experience_title(experience: &Experience) -> String {
format!("{}: {}", experience.title, experience.creation_time)
}
2024-11-21 10:04:06 +00:00
pub fn format_ingestion_dose(dose: &Dose, custom_unit_dose: Option<&CustomUnitDose>) -> String {
2024-11-20 19:15:07 +00:00
match dose {
2024-11-21 10:04:06 +00:00
Dose::Unknown(dose) => format!("Unknown {}", dose.unit),
Dose::Standard(dose) => {
2024-11-21 07:20:46 +00:00
let is_estimate = dose.estimation.is_estimate();
2024-11-20 19:15:07 +00:00
let estimate = if is_estimate { "~" } else { "" };
2024-11-21 07:20:46 +00:00
let standard_deviation = {
if let Estimation::StandardDeviation(standard_deviation) = dose.estimation {
2024-11-20 19:15:07 +00:00
format!("±{}", (standard_deviation * 100.0).round() / 100.0)
} else {
"".to_string()
2024-11-21 07:20:46 +00:00
}
};
2024-11-20 19:15:07 +00:00
let dose_value = (dose.dose * 100.0).round() / 100.0;
let unit = dose.unit.clone();
format!("{estimate}{dose_value}{standard_deviation} {unit}")
}
2024-11-21 10:04:06 +00:00
Dose::CustomUnit(dose) => {
2024-11-21 07:20:46 +00:00
let custom_unit_dose = custom_unit_dose.expect("custom unit dose required");
2024-11-20 19:15:07 +00:00
2024-11-21 10:04:06 +00:00
let ingestion_dose = calulate_custom_unit_dose(dose, custom_unit_dose);
2024-11-20 19:15:07 +00:00
2024-11-21 10:04:06 +00:00
let ingestion_dose = format_ingestion_dose(&Dose::Standard(ingestion_dose), None);
2024-11-20 19:15:07 +00:00
2024-11-21 07:20:46 +00:00
let dose_per_unit = format_ingestion_dose(
2024-11-21 10:04:06 +00:00
&Dose::Standard(StandardDose::from(custom_unit_dose.clone())),
2024-11-20 19:15:07 +00:00
None,
);
let custom_unit_dose = format_ingestion_dose(
2024-11-21 10:04:06 +00:00
&Dose::Standard(StandardDose {
2024-11-20 19:15:07 +00:00
dose: dose.dose,
2024-11-21 07:20:46 +00:00
unit: custom_unit_dose.unit.clone(),
contains_unknown: false,
estimation: dose.estimation,
2024-11-20 19:15:07 +00:00
}),
None,
);
2024-11-21 07:20:46 +00:00
format!("{ingestion_dose} ({dose_per_unit} * {custom_unit_dose})")
2024-11-20 19:15:07 +00:00
}
}
}
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)
}
}