Skip to main content

ergo_runtime/compute/implementations/subtract/
impl.rs

1use std::collections::HashMap;
2
3use crate::common::Value;
4use crate::compute::{ComputeError, ComputePrimitive, ComputePrimitiveManifest, PrimitiveState};
5
6use super::manifest::subtract_manifest;
7
8pub struct Subtract {
9    manifest: ComputePrimitiveManifest,
10}
11
12impl Subtract {
13    pub fn new() -> Self {
14        Self {
15            manifest: subtract_manifest(),
16        }
17    }
18}
19
20impl Default for Subtract {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26impl ComputePrimitive for Subtract {
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 a = inputs
38            .get("a")
39            .and_then(|v| v.as_number())
40            .expect("missing required numeric input 'a'");
41        let b = inputs
42            .get("b")
43            .and_then(|v| v.as_number())
44            .expect("missing required numeric input 'b'");
45
46        Ok(HashMap::from([(
47            "result".to_string(),
48            Value::Number(a - b),
49        )]))
50    }
51}