msg_tool\utils/
counter.rs

1//! A simple counter for tracking script execution results.
2use crate::types::*;
3use std::sync::atomic::AtomicUsize;
4use std::sync::atomic::Ordering::SeqCst;
5
6/// A counter for tracking script execution results.
7pub struct Counter {
8    ok: AtomicUsize,
9    ignored: AtomicUsize,
10    error: AtomicUsize,
11    warning: AtomicUsize,
12}
13
14impl Counter {
15    /// Creates a new Counter instance.
16    pub fn new() -> Self {
17        Self {
18            ok: AtomicUsize::new(0),
19            ignored: AtomicUsize::new(0),
20            error: AtomicUsize::new(0),
21            warning: AtomicUsize::new(0),
22        }
23    }
24
25    /// Increments the count of errors.
26    pub fn inc_error(&self) {
27        self.error.fetch_add(1, SeqCst);
28    }
29
30    /// Increments the count of warnings.
31    pub fn inc_warning(&self) {
32        self.warning.fetch_add(1, SeqCst);
33    }
34
35    /// Increments the count of script executions.
36    pub fn inc(&self, result: ScriptResult) {
37        match result {
38            ScriptResult::Ok => self.ok.fetch_add(1, SeqCst),
39            ScriptResult::Ignored => self.ignored.fetch_add(1, SeqCst),
40        };
41    }
42}
43
44impl std::fmt::Display for Counter {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        write!(
47            f,
48            "OK: {}, Ignored: {}, Error: {}, Warning: {}",
49            self.ok.load(SeqCst),
50            self.ignored.load(SeqCst),
51            self.error.load(SeqCst),
52            self.warning.load(SeqCst),
53        )
54    }
55}