use super::MetricError;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Labels(pub(super) Vec<(String, String)>);
impl Labels {
pub fn new(labels: &[(&str, &str)]) -> Result<Self, MetricError> {
if labels.len() > 4 {
return Err(MetricError::Capacity);
}
let mut pairs = Vec::new();
for &(name, value) in labels {
if !super::validation::identifier(name, 32)
|| name.starts_with("__")
|| matches!(name, "le" | "quantile")
|| value.len() > 64
{
return Err(MetricError::InvalidDefinition);
}
pairs.push((name.to_owned(), value.to_owned()));
}
pairs.sort_by(|left, right| left.0.cmp(&right.0));
if pairs.windows(2).any(|pair| pair[0].0 == pair[1].0) {
return Err(MetricError::InvalidDefinition);
}
Ok(Self(pairs))
}
pub(super) fn values(&self) -> Vec<&str> {
self.0.iter().map(|(_, value)| value.as_str()).collect()
}
}
#[derive(Clone, Debug)]
pub struct Family<M> {
pub(super) members: Vec<(Labels, M)>,
}
impl<M: Clone> Family<M> {
pub fn get(&self, labels: &Labels) -> Option<M> {
self.members
.iter()
.find(|(key, _)| key == labels)
.map(|(_, metric)| metric.clone())
}
}