journal/journal_cli/src/formatting.rs

74 lines
2.2 KiB
Rust
Raw Normal View History

2024-11-20 19:15:07 +00:00
use journal::{
2024-11-21 07:20:46 +00:00
helpers::calulate_custom_unit_ingestion_dose,
types::{
2024-11-21 09:57:49 +00:00
CustomUnit, CustomUnitIngestionDose, Estimation, Experience, Ingestion, IngestionDose,
2024-11-21 07:20:46 +00:00
StandardIngestionDose,
},
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 07:20:46 +00:00
pub fn format_ingestion_dose(
dose: &IngestionDose,
2024-11-21 09:57:49 +00:00
custom_unit_dose: Option<&CustomUnitIngestionDose>,
2024-11-21 07:20:46 +00:00
) -> String {
2024-11-20 19:15:07 +00:00
match dose {
IngestionDose::Unknown(dose) => format!("Unknown {}", dose.unit),
IngestionDose::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 07:20:46 +00:00
IngestionDose::CustomUnit(dose) => {
let custom_unit_dose = custom_unit_dose.expect("custom unit dose required");
2024-11-20 19:15:07 +00:00
2024-11-21 07:20:46 +00:00
let ingestion_dose = calulate_custom_unit_ingestion_dose(dose, custom_unit_dose);
2024-11-20 19:15:07 +00:00
2024-11-21 09:57:49 +00:00
let ingestion_dose =
format_ingestion_dose(&IngestionDose::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(
&IngestionDose::Standard(StandardIngestionDose::from(custom_unit_dose.clone())),
2024-11-20 19:15:07 +00:00
None,
);
let custom_unit_dose = format_ingestion_dose(
&IngestionDose::Standard(StandardIngestionDose {
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)
}
}