journal/journal_cli/src/formatting.rs

76 lines
2.4 KiB
Rust
Raw Normal View History

2024-11-20 19:15:07 +00:00
use journal::{
helpers::canonical_dose,
types::{CustomUnit, 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: Option<&CustomUnit>) -> String {
match dose {
IngestionDose::Unknown(dose) => format!("Unknown {}", dose.unit),
IngestionDose::Standard(dose) => {
let is_estimate = match dose.estimation {
journal::types::Estimation::Precise => false,
journal::types::Estimation::Estimate
| journal::types::Estimation::StandardDeviation(_) => true,
};
let estimate = if is_estimate { "~" } else { "" };
let standard_deviation =
if let journal::types::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::Custom(dose) => {
let custom_unit: &CustomUnit = custom_unit.expect("custom unit required for dose type");
let canonical_dose =
canonical_dose(&IngestionDose::Custom(dose.clone()), Some(custom_unit));
let canonical_dose = format_ingestion_dose(&canonical_dose, None);
let custom_unit_dose_per = format_ingestion_dose(
&IngestionDose::Standard(StandardIngestionDose {
dose: custom_unit.dose.dose,
unit: custom_unit.original_unit.clone(),
estimation: custom_unit.dose.estimation.clone(),
}),
None,
);
let custom_unit_dose = format_ingestion_dose(
&IngestionDose::Standard(StandardIngestionDose {
dose: dose.dose,
unit: custom_unit.dose.unit.clone(),
estimation: custom_unit.dose.estimation.clone(),
}),
None,
);
format!("{canonical_dose} ({custom_unit_dose_per} * {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)
}
}