Skip to main content

made_core/value_objects/
metric_label_name.rs

1use crate::error::DomainError;
2
3/// A Prometheus label name.
4#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
5pub struct MetricLabelName(String);
6
7impl MetricLabelName {
8    pub fn new(value: impl Into<String>) -> Result<Self, DomainError> {
9        let value = value.into();
10        let mut chars = value.chars();
11        let valid_first = chars
12            .next()
13            .is_some_and(|character| character.is_ascii_alphabetic() || character == '_');
14        if !valid_first
15            || !chars.all(|character| character.is_ascii_alphanumeric() || character == '_')
16        {
17            return Err(DomainError::InvariantViolated {
18                reason: "metric label name must use the Prometheus label alphabet",
19            });
20        }
21        Ok(Self(value))
22    }
23
24    #[must_use]
25    pub fn as_str(&self) -> &str {
26        &self.0
27    }
28}