use std::collections::HashSet;
use serde::{
Deserialize,
Serialize,
};
use super::{
ProgressCounter,
ProgressMetric,
};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProgressSchema {
metrics: Vec<ProgressMetric>,
}
impl ProgressSchema {
#[inline]
pub fn new(metrics: Vec<ProgressMetric>) -> Self {
Self { metrics }
}
#[inline]
pub fn single(id: &str, name: &str) -> Self {
Self::new(vec![ProgressMetric::new(id, name)])
}
#[inline]
pub fn metrics(&self) -> &[ProgressMetric] {
self.metrics.as_slice()
}
#[inline]
pub fn metric(&self, id: &str) -> Option<&ProgressMetric> {
self.metrics.iter().find(|metric| metric.id() == id)
}
#[inline]
pub fn metric_name(&self, id: &str) -> Option<&str> {
self.metric(id).map(ProgressMetric::name)
}
#[inline]
pub fn contains_metric(&self, id: &str) -> bool {
self.metric(id).is_some()
}
#[inline]
pub fn validate_counter(&self, counter: &ProgressCounter) -> bool {
self.contains_metric(counter.metric_id())
}
pub fn validate_counters(&self, counters: &[ProgressCounter]) -> bool {
let mut seen = HashSet::with_capacity(counters.len());
counters
.iter()
.all(|counter| self.validate_counter(counter) && seen.insert(counter.metric_id()))
}
}
impl Default for ProgressSchema {
#[inline]
fn default() -> Self {
Self::new(Vec::new())
}
}