rust_actions/
outputs.rs

1use serde_json::Value;
2use std::collections::HashMap;
3
4#[derive(Debug, Clone, Default)]
5pub struct StepOutputs {
6    values: HashMap<String, Value>,
7}
8
9impl StepOutputs {
10    pub fn new() -> Self {
11        Self::default()
12    }
13
14    pub fn from_value(value: Value) -> Self {
15        match value {
16            Value::Object(map) => Self {
17                values: map.into_iter().collect(),
18            },
19            _ => Self::default(),
20        }
21    }
22
23    pub fn get(&self, key: &str) -> Option<&Value> {
24        self.values.get(key)
25    }
26
27    pub fn get_string(&self, key: &str) -> Option<String> {
28        self.values.get(key).and_then(|v| match v {
29            Value::String(s) => Some(s.clone()),
30            _ => Some(v.to_string()),
31        })
32    }
33
34    pub fn insert(&mut self, key: impl Into<String>, value: impl Into<Value>) {
35        self.values.insert(key.into(), value.into());
36    }
37
38    pub fn is_empty(&self) -> bool {
39        self.values.is_empty()
40    }
41
42    pub fn to_value(&self) -> Value {
43        Value::Object(self.values.iter().map(|(k, v)| (k.clone(), v.clone())).collect())
44    }
45}
46
47pub trait IntoOutputs {
48    fn into_outputs(self) -> StepOutputs;
49}
50
51impl IntoOutputs for () {
52    fn into_outputs(self) -> StepOutputs {
53        StepOutputs::new()
54    }
55}
56
57impl IntoOutputs for StepOutputs {
58    fn into_outputs(self) -> StepOutputs {
59        self
60    }
61}