journal/journal_cli/src/formatting.rs
2024-11-24 14:26:48 +00:00

72 lines
1.8 KiB
Rust

use journal::types::{Ingestion, Session, SubstanceIngestion, Unit};
pub fn format_session_title(session: &Session) -> String {
format!("{}: {}", session.title, session.creation_time)
}
pub fn format_ingestion_time(ingestion: &Ingestion) -> String {
ingestion.ingestion_time.format("%a %I:%M %p").to_string()
}
pub fn format_substance_ingestion_roa(ingestion: &SubstanceIngestion, unit: &Unit) -> String {
if let Unit::Custom(unit) = unit {
format!("{:?} ({})", ingestion.roa, &unit.name)
} else {
format!("{:?}", ingestion.roa)
}
}
#[cfg(test)]
mod tests {
use chrono::{TimeZone, Utc};
use journal::types::{AdministrationRoute, CustomUnit};
use super::*;
#[test]
fn format_session_title() {
let result = super::format_session_title(&Session {
title: "Title".to_string(),
creation_time: Utc.timestamp_millis_opt(0).unwrap(),
..Session::default()
});
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]
fn format_substance_ingestion_roa() {
let result: String = super::format_substance_ingestion_roa(
&SubstanceIngestion {
roa: AdministrationRoute::Oral,
..SubstanceIngestion::default()
},
&Unit::default(),
);
assert_eq!(result, "Oral");
}
#[test]
fn format_ingestion_roa_with_custom_unit() {
let result = super::format_substance_ingestion_roa(
&SubstanceIngestion {
roa: AdministrationRoute::Oral,
..SubstanceIngestion::default()
},
&Unit::Custom(CustomUnit {
name: "32mg/ml".to_string(),
..CustomUnit::default()
}),
);
assert_eq!(result, "Oral (32mg/ml)");
}
}