logic_mesh/blocks/math/
cos.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_reader::InputReader, Input, InputProps},
8    output::Output,
9};
10
11use libhaystack::val::{kind::HaystackKind, Number, Value};
12
13use crate::{blocks::InputImpl, blocks::OutputImpl};
14
15/// Outputs the cosine value of the input.
16#[block]
17#[derive(BlockProps, Debug)]
18#[category = "math"]
19pub struct Cos {
20    #[input(name = "in", kind = "Number")]
21    pub a: InputImpl,
22    #[output(kind = "Number")]
23    pub out: OutputImpl,
24}
25
26impl Block for Cos {
27    async fn execute(&mut self) {
28        self.read_inputs_until_ready().await;
29
30        if let Some(Value::Number(a)) = self.a.get_value() {
31            self.out.set(
32                Number {
33                    value: a.value.cos(),
34                    unit: a.unit,
35                }
36                .into(),
37            );
38        }
39    }
40}
41
42#[cfg(test)]
43mod test {
44
45    use std::assert_matches::assert_matches;
46
47    use libhaystack::val::{Number, Value};
48
49    use crate::{
50        base::block::test_utils::write_block_inputs, base::block::Block, blocks::math::Cos,
51    };
52
53    #[tokio::test]
54    async fn test_cos_block() {
55        let mut block = Cos::new();
56
57        write_block_inputs(&mut [(&mut block.a, 0.into())]).await;
58
59        block.execute().await;
60
61        assert_matches!(
62            block.out.value,
63            Value::Number(Number { value, .. }) if value.round() == 1.0
64        );
65    }
66}