servlet 1.0.0

A small application runtime with an explicit operational contract
Documentation
//! Registration limits are checked before invoking the native metric builders.
use super::{Labels, MetricError};

pub(super) fn identifier(value: &str, maximum: usize) -> bool {
    !value.is_empty()
        && value.len() <= maximum
        && value.bytes().enumerate().all(|(index, byte)| {
            byte.is_ascii_alphabetic() || byte == b'_' || index > 0 && byte.is_ascii_digit()
        })
}
pub(super) fn definition(name: &str, help: &str, labels: &[Labels]) -> Result<(), MetricError> {
    if !identifier(name, 96)
        || name.starts_with("servlet_")
        || help.trim().is_empty()
        || help.len() > 512
    {
        return Err(MetricError::InvalidDefinition);
    }
    if labels.is_empty() || labels.len() > 64 {
        return Err(MetricError::Capacity);
    }
    for (index, label) in labels.iter().enumerate() {
        if labels[..index].contains(label)
            || !label
                .0
                .iter()
                .map(|(name, _)| name)
                .eq(labels[0].0.iter().map(|(name, _)| name))
        {
            return Err(MetricError::InvalidDefinition);
        }
    }
    Ok(())
}
pub(super) fn buckets(buckets: &[f64]) -> Result<(), MetricError> {
    if buckets.is_empty()
        || buckets.len() > 20
        || buckets.iter().any(|value| !value.is_finite())
        || buckets.windows(2).any(|pair| pair[0] >= pair[1])
    {
        return Err(MetricError::InvalidDefinition);
    }
    Ok(())
}