servlet 1.2.0

A small application runtime with an explicit operational contract
Documentation
//! Families expose only explicitly registered label combinations.
use super::MetricError;

/// An ordered set of at most four named labels, declared before application startup.
///
/// Label names are at most 32 ASCII bytes, values at most 64 UTF-8 bytes. Do not use
/// request IDs, URLs, user identifiers, or secrets as labels. Native encoding escapes
/// values. Names beginning with `__`, and `le`/`quantile`, are reserved.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Labels(pub(super) Vec<(String, String)>);
impl Labels {
    /// Validate one complete label combination. Input order is not significant.
    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()
    }
}

/// A finite family of native instrument handles; lookup never creates a series.
#[derive(Clone, Debug)]
pub struct Family<M> {
    pub(super) members: Vec<(Labels, M)>,
}
impl<M: Clone> Family<M> {
    /// Clone the handle for an approved combination, or return `None`.
    pub fn get(&self, labels: &Labels) -> Option<M> {
        self.members
            .iter()
            .find(|(key, _)| key == labels)
            .map(|(_, metric)| metric.clone())
    }
}