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