logic_mesh/blocks/logic/
eq.rs

1// Copyright (c) 2022-2023, Radu Racariu.
2
3use uuid::Uuid;
4
5use crate::base::{
6    block::{Block, BlockDesc, BlockProps, BlockState},
7    input::{Input, InputProps},
8    output::Output,
9};
10
11use libhaystack::val::kind::HaystackKind;
12
13use crate::{blocks::InputImpl, blocks::OutputImpl};
14
15use super::util::execute_impl;
16
17/// Outputs true if value of the inputs are equal.
18#[block]
19#[derive(BlockProps, Debug)]
20#[category = "logic"]
21pub struct Equal {
22    #[input(name = "in1", kind = "Null")]
23    pub input1: InputImpl,
24    #[input(name = "in2", kind = "Null")]
25    pub input2: InputImpl,
26    #[output(kind = "Bool")]
27    pub out: OutputImpl,
28}
29
30impl Block for Equal {
31    async fn execute(&mut self) {
32        execute_impl(self, |in1, in2| in1 == in2).await;
33    }
34}
35
36#[cfg(test)]
37mod test {
38
39    use crate::{
40        base::block::test_utils::write_block_inputs,
41        base::{block::Block, input::input_reader::InputReader},
42        blocks::logic::Equal,
43    };
44
45    #[tokio::test]
46    async fn test_eq_block() {
47        let mut block = Equal::new();
48
49        for _ in write_block_inputs(&mut [
50            (&mut block.input1, ("true").into()),
51            (&mut block.input2, ("true").into()),
52        ])
53        .await
54        {
55            block.read_inputs().await;
56        }
57
58        block.execute().await;
59        assert_eq!(block.out.value, true.into());
60    }
61}