Skip to main content

ergo_runtime/compute/implementations/select_bool/
impl.rs

1use std::collections::HashMap;
2
3use crate::common::Value;
4use crate::compute::{ComputeError, ComputePrimitive, ComputePrimitiveManifest, PrimitiveState};
5
6use super::manifest::select_bool_manifest;
7
8pub struct SelectBool {
9    manifest: ComputePrimitiveManifest,
10}
11
12impl SelectBool {
13    pub fn new() -> Self {
14        Self {
15            manifest: select_bool_manifest(),
16        }
17    }
18}
19
20impl Default for SelectBool {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26impl ComputePrimitive for SelectBool {
27    fn manifest(&self) -> &ComputePrimitiveManifest {
28        &self.manifest
29    }
30
31    fn compute(
32        &self,
33        inputs: &HashMap<String, Value>,
34        _parameters: &HashMap<String, Value>,
35        _state: Option<&mut PrimitiveState>,
36    ) -> Result<HashMap<String, Value>, ComputeError> {
37        let cond = inputs
38            .get("cond")
39            .and_then(|v| v.as_bool())
40            .expect("missing required bool input 'cond'");
41        let when_true = inputs
42            .get("when_true")
43            .and_then(|v| v.as_bool())
44            .expect("missing required bool input 'when_true'");
45        let when_false = inputs
46            .get("when_false")
47            .and_then(|v| v.as_bool())
48            .expect("missing required bool input 'when_false'");
49
50        let result = if cond { when_true } else { when_false };
51
52        Ok(HashMap::from([("result".to_string(), Value::Bool(result))]))
53    }
54}