use std::{
borrow::Cow,
hash::{Hash, Hasher},
};
use crate::raw::MetricType;
#[derive(Clone, Debug)]
pub struct Metadata {
name: Cow<'static, str>,
help: Cow<'static, str>,
ty: MetricType,
unit: Option<Unit>,
}
impl PartialEq for Metadata {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.ty == other.ty && self.unit == other.unit
}
}
impl Eq for Metadata {}
impl Hash for Metadata {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
self.ty.hash(state);
self.unit.hash(state);
}
}
impl Metadata {
pub fn new(
name: impl Into<Cow<'static, str>>,
help: impl Into<Cow<'static, str>>,
ty: MetricType,
unit: Option<Unit>,
) -> Self {
Self { name: name.into(), help: help.into(), ty, unit }
}
pub fn name(&self) -> &str {
self.name.as_ref()
}
pub fn help(&self) -> &str {
self.help.as_ref()
}
pub fn metric_type(&self) -> MetricType {
self.ty
}
pub fn unit(&self) -> Option<&Unit> {
self.unit.as_ref()
}
}
#[allow(missing_docs)]
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub enum Unit {
Seconds,
Bytes,
Joules,
Grams,
Meters,
Ratios,
Volts,
Amperes,
Celsius,
Other(Cow<'static, str>),
}
impl Unit {
pub fn as_str(&self) -> &str {
match self {
Unit::Seconds => "seconds",
Unit::Bytes => "bytes",
Unit::Joules => "joules",
Unit::Grams => "grams",
Unit::Meters => "meters",
Unit::Ratios => "ratios",
Unit::Volts => "volts",
Unit::Amperes => "amperes",
Unit::Celsius => "celsius",
Unit::Other(other) => other.as_ref(),
}
}
}
impl<T> From<T> for Unit
where
T: Into<Cow<'static, str>>,
{
fn from(name: T) -> Self {
Unit::Other(name.into())
}
}