logic_mesh/blocks/misc/
parse_bool.rs1use 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::{
12 encoding::zinc,
13 val::{kind::HaystackKind, Value},
14};
15
16use crate::{blocks::InputImpl, blocks::OutputImpl};
17
18#[block]
20#[derive(BlockProps, Debug)]
21#[category = "misc"]
22pub struct ParseBool {
23 #[input(name = "in", kind = "Str")]
24 pub input: InputImpl,
25 #[output(kind = "Bool")]
26 pub out: OutputImpl,
27}
28
29impl Block for ParseBool {
30 async fn execute(&mut self) {
31 self.read_inputs_until_ready().await;
32
33 if let Some(Value::Str(input)) = self.input.get_value() {
34 let parsed = input.value.parse::<bool>();
35 if let Ok(parsed) = parsed {
36 self.out.set(parsed.into());
37 } else if let Ok(Value::Bool(bool)) = zinc::decode::from_str(&input.value) {
38 self.out.set(bool.into());
39 }
40 }
41 }
42}
43
44#[cfg(test)]
45mod test {
46
47 use crate::{
48 base::block::test_utils::write_block_inputs, base::block::Block, blocks::misc::ParseBool,
49 };
50
51 #[tokio::test]
52 async fn parse_true() {
53 let mut block = ParseBool::new();
54
55 write_block_inputs(&mut [(&mut block.input, ("true").into())]).await;
56
57 block.execute().await;
58 assert_eq!(block.out.value, true.into());
59
60 write_block_inputs(&mut [(&mut block.input, ("T").into())]).await;
61
62 block.execute().await;
63 assert_eq!(block.out.value, true.into());
64 }
65
66 #[tokio::test]
67 async fn parse_false() {
68 let mut block = ParseBool::new();
69
70 write_block_inputs(&mut [(&mut block.input, ("false").into())]).await;
71
72 block.execute().await;
73 assert_eq!(block.out.value, false.into());
74
75 write_block_inputs(&mut [(&mut block.input, ("F").into())]).await;
76
77 block.execute().await;
78 assert_eq!(block.out.value, false.into());
79 }
80}