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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::fmt::Formatter;
use std::hash::{Hash, Hasher};

/// Identifier representing the type of the workflow step being defined
#[derive(Clone, Hash, Debug, Eq, PartialEq)]
pub struct WorkflowStepType(pub String);

/// The definition of a workflow step and any parameters it may be using
#[derive(Clone, Debug)]
pub struct WorkflowStepDefinition {
    pub step_type: WorkflowStepType,
    pub parameters: HashMap<String, Option<String>>,
}

/// The definition of a workflow and the steps (in order) it contains
#[derive(Clone, Debug)]
pub struct WorkflowDefinition {
    pub name: String,
    pub routed_by_reactor: bool,
    pub steps: Vec<WorkflowStepDefinition>,
}

impl std::fmt::Display for WorkflowStepType {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl WorkflowStepDefinition {
    /// Gets an identifier for the workflow step that's based on the step's parameters.  Two
    /// steps with the same set of parameters and values will always produce the same id within
    /// a single run of the the application, but the identifiers are not guaranteed to be consistent
    /// across application runs.
    pub fn get_id(&self) -> u64 {
        let mut hasher = DefaultHasher::new();
        self.hash(&mut hasher);
        hasher.finish()
    }
}

impl Hash for WorkflowStepDefinition {
    fn hash<H: Hasher>(&self, state: &mut H) {
        let mut sorted_keys: Vec<&String> = self.parameters.keys().collect();
        sorted_keys.sort();

        self.step_type.hash(state);
        for key in sorted_keys {
            key.hash(state);
            self.parameters.get(key).hash(state);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn two_steps_with_identical_setups_have_same_id() {
        let mut step1 = WorkflowStepDefinition {
            step_type: WorkflowStepType("test".to_string()),
            parameters: HashMap::new(),
        };

        step1
            .parameters
            .insert("a".to_string(), Some("b".to_string()));
        step1
            .parameters
            .insert("c".to_string(), Some("d".to_string()));

        let mut step2 = WorkflowStepDefinition {
            step_type: WorkflowStepType("test".to_string()),
            parameters: HashMap::new(),
        };

        step2
            .parameters
            .insert("c".to_string(), Some("d".to_string()));
        step2
            .parameters
            .insert("a".to_string(), Some("b".to_string()));

        assert_eq!(step1.get_id(), step2.get_id());
    }

    #[test]
    fn two_steps_with_different_types_do_not_have_same_id() {
        let mut step1 = WorkflowStepDefinition {
            step_type: WorkflowStepType("test".to_string()),
            parameters: HashMap::new(),
        };

        step1
            .parameters
            .insert("a".to_string(), Some("b".to_string()));
        step1
            .parameters
            .insert("c".to_string(), Some("d".to_string()));

        let mut step2 = WorkflowStepDefinition {
            step_type: WorkflowStepType("test2".to_string()),
            parameters: HashMap::new(),
        };

        step2
            .parameters
            .insert("c".to_string(), Some("d".to_string()));
        step2
            .parameters
            .insert("a".to_string(), Some("b".to_string()));

        assert_ne!(step1.get_id(), step2.get_id());
    }

    #[test]
    fn two_steps_with_different_parameters_do_not_have_same_id() {
        let mut step1 = WorkflowStepDefinition {
            step_type: WorkflowStepType("test".to_string()),
            parameters: HashMap::new(),
        };

        step1
            .parameters
            .insert("a".to_string(), Some("b".to_string()));
        step1
            .parameters
            .insert("c".to_string(), Some("d".to_string()));

        let mut step2 = WorkflowStepDefinition {
            step_type: WorkflowStepType("test2".to_string()),
            parameters: HashMap::new(),
        };

        step2
            .parameters
            .insert("c".to_string(), Some("d".to_string()));
        step2
            .parameters
            .insert("a".to_string(), Some("f".to_string()));

        assert_ne!(step1.get_id(), step2.get_id());
    }
}