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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use crate::step::TaskStep;
use crate::task::{JobInstanceState, Task, TaskStepState};
use crate::workflow::Workflow;
use crate::workflow_instance::WorkflowInstance;
use anyhow::*;
use rion::Rion;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;

#[derive(PartialEq, Eq, Serialize, Deserialize, Clone, Debug, Default)]
pub struct JobTaskContext {
    pub current_state: TaskStepState,
    pub state: JobInstanceState,
}

/// resolve rion variables
pub fn resolve_variables(template: &str, ctx: TaskContext) -> Result<String> {
    let result = Rion::resolve_var_str(template, &ctx)?;
    Ok(result)
}

/// extract step input
pub fn extract_step_input<T>(properties: &Value) -> Result<T>
where
    T: serde::de::DeserializeOwned,
{
    let value = serde_json::from_value::<HashMap<String, Value>>(properties.clone())?;
    let d = json!({});
    let raw_input = value.get("input").unwrap_or(&d).clone();
    let input = serde_json::from_value::<T>(raw_input)?;
    Ok(input)
}

/// extract step output
pub fn extract_step_output<T>(properties: &Value) -> Result<T>
where
    T: serde::de::DeserializeOwned,
{
    let value = serde_json::from_value::<HashMap<String, Value>>(properties.clone())?;
    let d = json!({});
    let raw_input = value.get("output").unwrap_or(&d).clone();
    let output = serde_json::from_value::<T>(raw_input)?;
    Ok(output)
}

/// extract step output
pub fn convert_step_property<T>(value: &Value) -> Result<T>
where
    T: serde::de::DeserializeOwned,
{
    let output = serde_json::from_value::<T>(value.clone())?;
    Ok(output)
}

/// extract step input / output
pub fn extract_step_input_output<I, O>(properties: &Value) -> Result<(I, O)>
where
    I: serde::de::DeserializeOwned,
    O: serde::de::DeserializeOwned,
{
    let output = extract_step_output::<O>(properties)?;
    let input = extract_step_input::<I>(properties)?;

    Ok((input, output))
}

#[derive(PartialEq, Eq, Serialize, Deserialize, Clone, Debug)]
pub struct TaskContext {
    /// Task that owns this schema instance
    pub previous_task_id: Option<String>,

    /// Task that owns this schema instance
    pub current_task_id: Option<String>,

    /// Task that owns this schema instance
    pub next_task_id: Option<String>,

    pub steps: HashMap<String, TaskStepState>,

    pub env: HashMap<String, String>,

    pub input: HashMap<String, Value>,

    pub job: WorkflowInstance,

    pub step: TaskStep,

    pub workflow: Workflow,

    pub task: Task,
}

impl TaskContext {
    /// converts job to json
    pub fn to_json(&self) -> Value {
        serde_json::to_value(self.clone()).unwrap_or_default()
    }

    /// gets rion with context applied
    pub fn resolve_variables(&self, template: String) -> Result<String> {
        resolve_variables(template.as_str(), self.clone())
    }

    /// gets rion with context applied
    pub fn resolve_input_variables<T>(&self, template: HashMap<String, Value>) -> Result<T>
    where
        T: Serialize + serde::de::DeserializeOwned,
    {
        let j_str = serde_json::to_string::<HashMap<String, Value>>(&template)?;
        let val = Rion::resolve_var_str(j_str.as_str(), &self.clone())?;
        let output = serde_json::from_str::<T>(val.as_str())?;
        Ok(output)
    }

    /// gets rion with context applied
    pub fn value_to_output<T>(value: T) -> Result<HashMap<String, Value>>
    where
        T: Serialize + serde::de::DeserializeOwned,
    {
        let raw_value = serde_json::to_value(value)?;
        let output = serde_json::from_value::<HashMap<String, Value>>(raw_value)?;
        Ok(output)
    }
}