Skip to main content

ergo_runtime/action/implementations/ack/
impl.rs

1use std::collections::HashMap;
2
3use crate::action::{
4    ActionOutcome, ActionPrimitive, ActionPrimitiveManifest, ActionValue, ParameterValue,
5};
6
7use super::manifest::ack_action_manifest;
8
9pub struct AckAction {
10    manifest: ActionPrimitiveManifest,
11}
12
13impl AckAction {
14    pub fn new() -> Self {
15        Self {
16            manifest: ack_action_manifest(),
17        }
18    }
19}
20
21impl Default for AckAction {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl ActionPrimitive for AckAction {
28    fn manifest(&self) -> &ActionPrimitiveManifest {
29        &self.manifest
30    }
31
32    fn execute(
33        &self,
34        inputs: &HashMap<String, ActionValue>,
35        parameters: &HashMap<String, ParameterValue>,
36    ) -> HashMap<String, ActionValue> {
37        let _event = inputs
38            .get("event")
39            .and_then(|v| v.as_event())
40            .expect("missing required event input 'event'");
41
42        let accept = parameters
43            .get("accept")
44            .and_then(|v| match v {
45                ParameterValue::Bool(b) => Some(*b),
46                _ => None,
47            })
48            .unwrap_or(true);
49
50        let outcome = if accept {
51            ActionOutcome::Completed
52        } else {
53            ActionOutcome::Rejected
54        };
55
56        HashMap::from([("outcome".to_string(), ActionValue::Event(outcome))])
57    }
58}