use std::ops::Deref;
use crate::tags::TagValues;
use crate::{MetricType, MetricUnit, MetricValue};
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct MetricMeta {
ty: MetricType,
unit: MetricUnit,
key: &'static str,
pub(crate) tag_keys: &'static [&'static str],
}
impl MetricMeta {
pub const fn new(ty: MetricType, unit: MetricUnit, key: &'static str) -> Self {
Self {
ty,
unit,
key,
tag_keys: &[],
}
}
pub const fn with_tags<const N: usize>(
mut self,
tag_keys: &'static [&'static str; N],
) -> TaggedMetricMeta<N> {
self.tag_keys = tag_keys;
TaggedMetricMeta { meta: self }
}
pub fn ty(&self) -> MetricType {
self.ty
}
pub fn unit(&self) -> MetricUnit {
self.unit
}
pub fn key(&self) -> &'static str {
self.key
}
}
#[derive(Debug)]
pub struct TaggedMetricMeta<const N: usize> {
pub(crate) meta: MetricMeta,
}
#[derive(Debug)]
pub struct MetricKey<'meta> {
pub(crate) meta: &'meta MetricMeta,
pub(crate) tag_values: TagValues,
}
impl<'meta> Deref for MetricKey<'meta> {
type Target = MetricMeta;
fn deref(&self) -> &Self::Target {
self.meta
}
}
impl<'meta> MetricKey<'meta> {
pub fn tags(&self) -> impl ExactSizeIterator<Item = (&str, &str)> {
let values = self.tag_values.as_deref().unwrap_or_default();
self.meta
.tag_keys
.iter()
.copied()
.zip(values.iter().map(|s| s.as_ref()))
}
}
#[derive(Debug)]
pub struct Metric {
pub(crate) key: MetricKey<'static>,
pub(crate) value: MetricValue,
}
impl Deref for Metric {
type Target = MetricKey<'static>;
fn deref(&self) -> &Self::Target {
&self.key
}
}
impl Metric {
pub fn value(&self) -> MetricValue {
self.value
}
}