askit_std_agents/
utils.rs

1use std::vec;
2
3use agent_stream_kit::{
4    ASKit, AgentContext, AgentData, AgentError, AgentOutput, AgentSpec, AgentValue, AsAgent,
5    askit_agent, async_trait,
6};
7
8static CATEGORY: &str = "Std/Utils";
9
10static PIN_IN: &str = "in";
11static PIN_RESET: &str = "reset";
12static PIN_COUNT: &str = "count";
13
14static DISPLAY_COUNT: &str = "count";
15
16/// Counter
17#[askit_agent(
18    title = "Counter",
19    category = CATEGORY,
20    inputs = [PIN_IN, PIN_RESET],
21    outputs = [PIN_COUNT],
22    integer_display(name = DISPLAY_COUNT, hide_title)
23)]
24struct CounterAgent {
25    data: AgentData,
26    count: i64,
27}
28
29#[async_trait]
30impl AsAgent for CounterAgent {
31    fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
32        Ok(Self {
33            data: AgentData::new(askit, id, spec),
34            count: 0,
35        })
36    }
37
38    async fn start(&mut self) -> Result<(), AgentError> {
39        self.count = 0;
40        self.emit_display(DISPLAY_COUNT, AgentValue::integer(0));
41        Ok(())
42    }
43
44    async fn process(
45        &mut self,
46        ctx: AgentContext,
47        pin: String,
48        _value: AgentValue,
49    ) -> Result<(), AgentError> {
50        if pin == PIN_RESET {
51            self.count = 0;
52        } else if pin == PIN_IN {
53            self.count += 1;
54        }
55        self.try_output(ctx, PIN_COUNT, AgentValue::integer(self.count))?;
56        self.emit_display(DISPLAY_COUNT, AgentValue::integer(self.count));
57
58        Ok(())
59    }
60}