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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::fmt::Debug;

use crate::{Resource, ServiceState};

/// The runner is a helper to run a function that returns a [Result] with a [Resource] and maps the error
/// case to a [ServiceState] and a message. This is to avoid boilerplate in every plugin.
///
/// ## Example
///
/// ```no_run
/// use std::error::Error;
///
/// use nagiosplugin::{Metric, Resource, Runner, TriggerIfValue};
///
/// fn main() {
///     Runner::new().safe_run(do_check).print_and_exit()
/// }
///
/// fn do_check() -> Result<Resource, Box<dyn Error>> {
///    // The first metric will not issue an alarm, the second one will.
///    let resource = Resource::new("foo")
///         .with_description("This is a simple test plugin")
///         .with_result(Metric::new("test", 15).with_thresholds(20, 50, TriggerIfValue::Greater))
///         .with_result(Metric::new("alerting", 42).with_thresholds(40, 50, TriggerIfValue::Greater));
///
///     Ok(resource)
/// }
/// ```

pub struct Runner<E> {
    #[allow(clippy::type_complexity)]
    on_error: Option<Box<dyn FnOnce(E) -> (ServiceState, E)>>,
}

impl<E: Debug> Runner<E> {
    pub fn new() -> Self {
        Self { on_error: None }
    }

    /// This will set a custom error handler. The is mostly useful to provide better plugin output.
    pub fn on_error(mut self, f: impl FnOnce(E) -> (ServiceState, E) + 'static) -> Self {
        self.on_error = Some(Box::new(f));
        self
    }

    /// This will run either the default `on_error` handler or the one specified by calling
    /// [Self::on_error]. It will use the given ([ServiceState], message) tuple and exit with these.
    pub fn safe_run(self, f: impl FnOnce() -> Result<Resource, E>) -> RunnerResult<E> {
        match f() {
            Ok(resource) => RunnerResult::Ok(resource),
            Err(err) => {
                let (state, msg) = match self.on_error {
                    None => (ServiceState::Critical, err),
                    Some(f) => f(err),
                };

                RunnerResult::Err(state, msg)
            }
        }
    }
}

impl<E: Debug> Default for Runner<E> {
    fn default() -> Self {
        Self::new()
    }
}

/// The result of a runner execution.
#[derive(Debug)]
pub enum RunnerResult<E> {
    /// The run was successful and it contains the returned [Resource].
    Ok(Resource),
    /// The run was not successful and it contains the [ServiceState] and the error.
    Err(ServiceState, E),
}

impl<E: Debug> RunnerResult<E> {
    pub fn print_and_exit(self) -> ! {
        match self {
            RunnerResult::Ok(resource) => resource.print_and_exit(),
            RunnerResult::Err(state, msg) => {
                println!("{}: {:?}", state, msg);
                std::process::exit(state.exit_code());
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(Debug, thiserror::Error)]
    #[error("woops")]
    struct EmptyError;

    #[test]
    fn test_runner_ok() {
        let result = Runner::<EmptyError>::new()
            .on_error(|_| {
                assert!(false);
                (ServiceState::Unknown, EmptyError)
            })
            .safe_run(|| Ok(Resource::new("test")));

        matches!(result, RunnerResult::Ok(_));
    }

    #[test]
    fn test_runner_error() {
        let result = Runner::<EmptyError>::new()
            .on_error(|_| (ServiceState::Unknown, EmptyError))
            .safe_run(|| Err(EmptyError {}));

        matches!(result, RunnerResult::Err(_, _));
    }
}