isothrum_runner/events/
result_data.rs1use serde::{Deserialize, Serialize};
7
8use crate::state::RunState;
9
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
12#[serde(untagged, deny_unknown_fields)]
13#[non_exhaustive]
14pub enum ResultData {
15 Started {
17 state: RunState,
19 outputs: Vec<ShownOutput>,
21 },
22 Swept {
24 destroyed: Vec<String>,
26 pending: Vec<String>,
28 failed: Vec<SweepFailure>,
30 },
31 Machine {
33 machine: Option<String>,
35 },
36 Moved {
38 state: RunState,
40 },
41}
42
43#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
45#[serde(deny_unknown_fields)]
46#[non_exhaustive]
47pub struct ShownOutput {
48 pub name: String,
50 pub value: Option<String>,
52}
53
54impl ShownOutput {
55 #[must_use]
57 pub fn new(name: impl Into<String>, value: Option<String>) -> ShownOutput {
58 ShownOutput { name: name.into(), value }
59 }
60}
61
62#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
64#[serde(deny_unknown_fields)]
65#[non_exhaustive]
66pub struct SweepFailure {
67 pub run: String,
69 pub error: String,
71}
72
73impl SweepFailure {
74 #[must_use]
76 pub fn new(run: impl Into<String>, error: impl Into<String>) -> SweepFailure {
77 SweepFailure {
78 run: run.into(),
79 error: error.into(),
80 }
81 }
82}
83
84impl ResultData {
85 #[must_use]
87 pub(crate) fn masked(self, mask: impl Fn(&str) -> String) -> ResultData {
88 let m = |s: String| mask(&s);
89 let mo = |s: Option<String>| s.map(|s| mask(&s));
90 match self {
91 ResultData::Started { state, outputs } => ResultData::Started {
92 state,
93 outputs: outputs
94 .into_iter()
95 .map(|o| ShownOutput {
96 name: m(o.name),
97 value: mo(o.value),
98 })
99 .collect(),
100 },
101 ResultData::Swept { destroyed, pending, failed } => ResultData::Swept {
102 destroyed,
103 pending,
104 failed: failed.into_iter().map(|f| SweepFailure { run: f.run, error: m(f.error) }).collect(),
105 },
106 other => other,
107 }
108 }
109}
110
111#[cfg(test)]
112mod tests {
113 use super::*;
114
115 #[test]
118 fn every_shape_is_its_wire_json_and_reads_back_from_it() {
119 let shapes = [
120 (
121 ResultData::Started {
122 state: RunState::Ready,
123 outputs: vec![ShownOutput::new("web", Some("http://127.0.0.1:8080".into()))],
124 },
125 serde_json::json!({"state": "ready", "outputs": [{"name": "web", "value": "http://127.0.0.1:8080"}]}),
126 ),
127 (
128 ResultData::Swept {
129 destroyed: vec!["a".into()],
130 pending: Vec::new(),
131 failed: vec![SweepFailure::new("b", "x")],
132 },
133 serde_json::json!({"destroyed": ["a"], "pending": [], "failed": [{"run": "b", "error": "x"}]}),
134 ),
135 (ResultData::Machine { machine: Some("web".into()) }, serde_json::json!({"machine": "web"})),
136 (ResultData::Machine { machine: None }, serde_json::json!({"machine": null})),
137 (ResultData::Moved { state: RunState::Parked }, serde_json::json!({"state": "parked"})),
138 ];
139 for (shape, json) in shapes {
140 assert_eq!(serde_json::to_value(&shape).unwrap(), json, "{shape:?}");
141 assert_eq!(serde_json::from_value::<ResultData>(json.clone()).unwrap(), shape, "{json}");
142 }
143 }
144}