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
use std::fmt;

use chrono::{offset::Local, DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Record of a shell command execution.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum ShCmdExecutionRecord {
    /// There is no execution record.
    ///
    /// Represents when the command has either never been executed, or has been
    /// cleaned up.
    None,
    /// Record of the command's last execution.
    Some {
        /// Timestamp of beginning of execution.
        start_datetime: chrono::DateTime<Utc>,
        /// Timestamp that the execution ended.
        end_datetime: chrono::DateTime<Utc>,
        /// Exit code of the process, if any.
        ///
        /// See [`ExitStatus::code()`].
        ///
        /// [`ExitStatus::code()`]: std::process::ExitStatus::code
        exit_code: Option<i32>,
    },
}

impl fmt::Display for ShCmdExecutionRecord {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::None => write!(f, "not executed"),
            Self::Some {
                start_datetime,
                exit_code,
                ..
            } => match exit_code {
                Some(0) => {
                    let start_datetime_local = DateTime::<Local>::from(*start_datetime);
                    write!(f, "executed successfully at {start_datetime_local}")
                }
                Some(code) => write!(f, "execution failed with code: {code}"),
                None => write!(f, "execution was interrupted"),
            },
        }
    }
}