journal/journal_cli/src/formatting.rs

72 lines
1.8 KiB
Rust
Raw Normal View History

2024-11-24 14:26:48 +00:00
use journal::types::{Ingestion, Session, SubstanceIngestion, Unit};
2024-11-20 19:15:07 +00:00
2024-11-23 21:43:28 +00:00
pub fn format_session_title(session: &Session) -> String {
format!("{}: {}", session.title, session.creation_time)
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()
}
2024-11-24 14:26:48 +00:00
pub fn format_substance_ingestion_roa(ingestion: &SubstanceIngestion, unit: &Unit) -> String {
2024-11-24 13:10:19 +00:00
if let Unit::Custom(unit) = unit {
format!("{:?} ({})", ingestion.roa, &unit.name)
2024-11-20 19:15:07 +00:00
} else {
format!("{:?}", ingestion.roa)
}
}
2024-11-22 17:04:18 +00:00
#[cfg(test)]
mod tests {
2024-11-22 18:50:18 +00:00
use chrono::{TimeZone, Utc};
use journal::types::{AdministrationRoute, CustomUnit};
2024-11-22 17:04:18 +00:00
use super::*;
#[test]
2024-11-23 21:43:28 +00:00
fn format_session_title() {
let result = super::format_session_title(&Session {
2024-11-22 18:50:18 +00:00
title: "Title".to_string(),
creation_time: Utc.timestamp_millis_opt(0).unwrap(),
2024-11-23 21:43:28 +00:00
..Session::default()
2024-11-22 18:50:18 +00:00
});
assert_eq!(result, "Title: 1970-01-01 00:00:00 UTC");
}
#[test]
fn format_ingestion_time() {
let result = super::format_ingestion_time(&Ingestion {
creation_time: Utc.timestamp_millis_opt(0).unwrap(),
..Ingestion::default()
});
assert_eq!(result, "Thu 12:00 AM");
}
#[test]
2024-11-24 14:26:48 +00:00
fn format_substance_ingestion_roa() {
let result: String = super::format_substance_ingestion_roa(
&SubstanceIngestion {
2024-11-22 18:50:18 +00:00
roa: AdministrationRoute::Oral,
2024-11-24 14:26:48 +00:00
..SubstanceIngestion::default()
2024-11-22 17:22:43 +00:00
},
2024-11-22 18:50:18 +00:00
&Unit::default(),
2024-11-22 17:22:43 +00:00
);
2024-11-22 18:50:18 +00:00
assert_eq!(result, "Oral");
2024-11-22 17:04:18 +00:00
}
#[test]
2024-11-22 18:50:18 +00:00
fn format_ingestion_roa_with_custom_unit() {
2024-11-24 14:26:48 +00:00
let result = super::format_substance_ingestion_roa(
&SubstanceIngestion {
2024-11-22 18:50:18 +00:00
roa: AdministrationRoute::Oral,
2024-11-24 14:26:48 +00:00
..SubstanceIngestion::default()
2024-11-22 17:22:43 +00:00
},
2024-11-24 13:10:19 +00:00
&Unit::Custom(CustomUnit {
name: "32mg/ml".to_string(),
..CustomUnit::default()
}),
2024-11-22 17:04:18 +00:00
);
2024-11-22 18:50:18 +00:00
assert_eq!(result, "Oral (32mg/ml)");
2024-11-22 17:04:18 +00:00
}
}