1#![cfg(feature = "yaml")]
2
3use std::vec;
4
5use agent_stream_kit::{
6 ASKit, AgentContext, AgentData, AgentError, AgentOutput, AgentSpec, AgentValue, AsAgent,
7 askit_agent, async_trait,
8};
9
10static CATEGORY: &str = "Std/Yaml";
11
12static PIN_DATA: &str = "data";
13static PIN_YAML: &str = "yaml";
14
15#[askit_agent(
17 title = "To YAML",
18 category = CATEGORY,
19 inputs = [PIN_DATA],
20 outputs = [PIN_YAML]
21)]
22struct ToYamlAgent {
23 data: AgentData,
24}
25
26#[async_trait]
27impl AsAgent for ToYamlAgent {
28 fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
29 Ok(Self {
30 data: AgentData::new(askit, id, spec),
31 })
32 }
33
34 async fn process(
35 &mut self,
36 ctx: AgentContext,
37 _pin: String,
38 value: AgentValue,
39 ) -> Result<(), AgentError> {
40 let yaml = serde_yaml_ng::to_string(&value)
41 .map_err(|e| AgentError::InvalidValue(e.to_string()))?;
42 self.try_output(ctx, PIN_YAML, AgentValue::string(yaml))?;
43 Ok(())
44 }
45}
46
47#[askit_agent(
49 title = "From YAML",
50 category = CATEGORY,
51 inputs = [PIN_YAML],
52 outputs = [PIN_DATA]
53)]
54struct FromYamlAgent {
55 data: AgentData,
56}
57
58#[async_trait]
59impl AsAgent for FromYamlAgent {
60 fn new(askit: ASKit, id: String, spec: AgentSpec) -> Result<Self, AgentError> {
61 Ok(Self {
62 data: AgentData::new(askit, id, spec),
63 })
64 }
65
66 async fn process(
67 &mut self,
68 ctx: AgentContext,
69 _pin: String,
70 value: AgentValue,
71 ) -> Result<(), AgentError> {
72 let s = value
73 .as_str()
74 .ok_or_else(|| AgentError::InvalidValue("not a string".to_string()))?;
75 let v: serde_json::Value =
76 serde_yaml_ng::from_str(s).map_err(|e| AgentError::InvalidValue(e.to_string()))?;
77 let value = AgentValue::from_json(v)?;
78 self.try_output(ctx, PIN_DATA, value)?;
79 Ok(())
80 }
81}