1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
macro_rules! impl_to_perf_string_on_to_string {
    ($($t:ty), *) => {
        $(
            impl ToPerfString for $t {
                fn to_perf_string(&self) -> String {
                    self.to_string()
                }
            }
        )*
    };
}

/// Let's you simply create a resource from multiple metrics. It's a bit like the vec! macro.
/// ```rust
/// # #[macro_use]
/// # extern crate nagiosplugin;
/// #
/// # use nagiosplugin::{SimpleMetric, State};
/// #
/// # fn main() {
/// let m1 = SimpleMetric::new("test", Some(State::Ok), 12, None, None, None, None);
/// let m2 = SimpleMetric::new("other", None, true, None, None, None, None);
/// let resource = resource![m1, m2];
/// # }
/// ```
#[macro_export]
macro_rules! resource {
    ($( $m:expr ), *) => {
        {
            use $crate::Resource;
            let mut r = Resource::new(None, None);
            $(
                r.push($m);
            )*
            r
        }
    };
}

macro_rules! metric_string {
    ($name:expr, $( $tps:expr), *) => {
        {
            let mut s = String::new();
            s.push_str(&format!("{}=", $name));
            $(
                s.push_str(&$tps.to_perf_string());
                s.push(';');
            )*
            s.trim_right_matches(';').to_string()
        }
    };
}

#[cfg(test)]
mod tests {
    use SimpleMetric;

    #[test]
    fn test_resource_macro() {
        let m1 = SimpleMetric::new("test", None, 12, None, None, None, None);
        let m2 = m1.clone();

        let _resource = resource![m1.clone()];
        let _resource = resource![m1.clone(), m2.clone()];
    }
}