logic_mesh/blocks/string/
len.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::val::{kind::HaystackKind, Number, Value};
12
13use crate::{blocks::InputImpl, blocks::OutputImpl};
14
15#[block]
17#[derive(BlockProps, Debug)]
18#[dis = "StringLen"]
19#[category = "string"]
20pub struct StrLen {
21 #[input(name = "in", kind = "Str")]
22 pub input: InputImpl,
23 #[output(kind = "Number")]
24 pub out: OutputImpl,
25}
26
27impl Block for StrLen {
28 async fn execute(&mut self) {
29 self.read_inputs_until_ready().await;
30
31 if let Some(Value::Str(a)) = self.input.get_value() {
32 self.out.set(
33 Number {
34 value: a.value.len() as f64,
35 unit: None,
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::string::StrLen,
52 };
53
54 #[tokio::test]
55 async fn test_sub() {
56 let mut block = StrLen::new();
57
58 write_block_inputs(&mut [(&mut block.input, "ana are mere".into())]).await;
59
60 block.execute().await;
61
62 assert_matches!(
63 block.out.value,
64 Value::Number(Number { value, .. }) if value == 12.0
65 );
66 }
67}