use serde_derive::{Deserialize, Serialize};
use crate::metric::Metric;
use crate::unit::Unit;
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize)]
pub struct Graph {
#[serde(skip_serializing)]
pub name: String,
pub label: String,
pub unit: Unit,
pub metrics: Vec<Metric>,
}
impl Graph {
#[doc(hidden)]
pub fn has_diff(&self) -> bool {
self.metrics.iter().any(|metric| metric.diff)
}
}
#[macro_export]
macro_rules! graph {
(
name: $name:expr,
label: $label:expr,
unit: $unit:expr,
metrics: [$( {$( $metrics:tt )*} ),+ $(,)?] $(,)?
) => {{
assert!(
str::chars($name).all(|c| matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' | '.' | '*' | '#'))
&& !$name.starts_with('.') && !$name.ends_with('.')
);
$crate::Graph {
name: $name.into(),
label: $label.into(),
unit: $unit.parse().unwrap(),
metrics: vec![$( $crate::metric! {$( $metrics )*} ),+],
}
}};
($($_:tt)*) => {
compile_error!("name, label, unit, and metrics are required");
};
}