use crate::threshold::Threshold;
pub struct Ceiling {
limit: Option<f64>,
actual: f64,
subject: &'static str,
percentage: bool,
}
impl Ceiling {
#[must_use]
pub fn count(limit: Option<usize>, actual: usize, subject: &'static str) -> Self {
Self {
limit: limit.map(|value| value as f64),
actual: actual as f64,
subject,
percentage: false,
}
}
#[must_use]
pub fn percent(limit: Option<Threshold>, actual: f64, subject: &'static str) -> Self {
Self {
limit: limit.map(Threshold::as_percent),
actual,
subject,
percentage: true,
}
}
#[must_use]
pub fn breach(&self) -> Option<String> {
let limit = self.limit?;
if self.actual <= limit {
return None;
}
Some(if self.percentage {
format!("{:.1}% {} (max: {:.1}%)", self.actual, self.subject, limit)
} else {
format!(
"{} {} (max: {})",
self.actual as usize, self.subject, limit as usize
)
})
}
}