Skip to main content

openjd_sessions/
action.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// Copyright by contributors to this project.
3// SPDX-License-Identifier: (Apache-2.0 OR MIT)
4
5//! Action state and result types.
6
7/// State of an action execution.
8///
9/// ```
10/// use openjd_sessions::ActionState;
11///
12/// let state = ActionState::Success;
13/// assert_eq!(state, ActionState::Success);
14/// assert_ne!(state, ActionState::Failed);
15/// ```
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum ActionState {
18    Running,
19    Success,
20    Failed,
21    Canceled,
22    Timeout,
23}
24
25impl std::fmt::Display for ActionState {
26    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        match self {
28            Self::Running => write!(f, "Running"),
29            Self::Success => write!(f, "Success"),
30            Self::Failed => write!(f, "Failed"),
31            Self::Canceled => write!(f, "Canceled"),
32            Self::Timeout => write!(f, "Timeout"),
33        }
34    }
35}
36
37/// A parsed openjd stdout message from a running action.
38#[derive(Debug, Clone)]
39pub enum ActionMessage {
40    /// `openjd_progress: <number>`
41    Progress(f64),
42    /// `openjd_status: <message>`
43    Status(String),
44    /// `openjd_fail: <message>`
45    Fail(String),
46    /// `openjd_env: <var>=<value>`
47    SetEnv { name: String, value: String },
48    /// `openjd_unset_env: <var>`
49    UnsetEnv { name: String },
50    /// `openjd_redacted_env: <var>=<value>`
51    RedactedEnv { name: String, value: String },
52    /// Request to cancel the action and mark it as failed (from malformed env commands)
53    CancelMarkFailed { fail_message: String },
54}
55
56impl std::fmt::Display for ActionMessage {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        match self {
59            Self::Progress(v) => write!(f, "Progress({v})"),
60            Self::Status(s) => write!(f, "Status({s})"),
61            Self::Fail(s) => write!(f, "Fail({s})"),
62            Self::SetEnv { name, .. } => write!(f, "SetEnv({name})"),
63            Self::UnsetEnv { name } => write!(f, "UnsetEnv({name})"),
64            Self::RedactedEnv { name, .. } => write!(f, "RedactedEnv({name})"),
65            Self::CancelMarkFailed { fail_message } => {
66                write!(f, "CancelMarkFailed({fail_message})")
67            }
68        }
69    }
70}
71
72/// Result of running an action.
73#[derive(Debug)]
74pub struct ActionResult {
75    pub state: ActionState,
76    pub exit_code: Option<i32>,
77    pub stdout: String,
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn action_state_display() {
86        assert_eq!(ActionState::Running.to_string(), "Running");
87        assert_eq!(ActionState::Success.to_string(), "Success");
88        assert_eq!(ActionState::Failed.to_string(), "Failed");
89        assert_eq!(ActionState::Canceled.to_string(), "Canceled");
90        assert_eq!(ActionState::Timeout.to_string(), "Timeout");
91    }
92
93    #[test]
94    fn action_message_display() {
95        assert_eq!(ActionMessage::Progress(50.0).to_string(), "Progress(50)");
96        assert_eq!(ActionMessage::Status("ok".into()).to_string(), "Status(ok)");
97        assert_eq!(ActionMessage::Fail("err".into()).to_string(), "Fail(err)");
98        assert_eq!(
99            ActionMessage::SetEnv {
100                name: "K".into(),
101                value: "V".into()
102            }
103            .to_string(),
104            "SetEnv(K)"
105        );
106        assert_eq!(
107            ActionMessage::UnsetEnv { name: "K".into() }.to_string(),
108            "UnsetEnv(K)"
109        );
110        assert_eq!(
111            ActionMessage::RedactedEnv {
112                name: "K".into(),
113                value: "V".into()
114            }
115            .to_string(),
116            "RedactedEnv(K)"
117        );
118        assert_eq!(
119            ActionMessage::CancelMarkFailed {
120                fail_message: "bad".into()
121            }
122            .to_string(),
123            "CancelMarkFailed(bad)"
124        );
125    }
126}