tether_utils/tether_topics/
agent_tree.rs1use std::fmt;
2
3use super::{parse_agent_id, parse_agent_role, parse_plug_name};
4
5pub struct AgentTree {
7 pub role: String,
8 pub ids: Vec<String>,
9 pub output_plugs: Vec<String>,
10}
11
12impl AgentTree {
13 pub fn new(role: &str, topics: &[String]) -> AgentTree {
14 let topics_this_agent = topics.iter().filter_map(|topic| {
15 if let Some(role_part) = parse_agent_role(topic.as_str()) {
16 if role_part == role {
17 Some(String::from(topic))
18 } else {
19 None
20 }
21 } else {
22 None
23 }
24 });
25
26 let ids = topics_this_agent
27 .clone()
28 .fold(Vec::new(), |mut acc, topic| {
29 if let Some(id) = parse_agent_id(&topic) {
30 if !acc.iter().any(|x| x == id) {
31 acc.push(String::from(id))
32 }
33 }
34 acc
35 });
36
37 let output_plugs = topics_this_agent
38 .clone()
39 .fold(Vec::new(), |mut acc, topic| {
40 if let Some(p) = parse_plug_name(&topic) {
41 acc.push(String::from(p));
42 }
43 acc
44 });
45
46 AgentTree {
47 role: role.into(),
48 ids: ids.to_vec(),
49 output_plugs: output_plugs.to_vec(),
50 }
51 }
52}
53
54impl fmt::Display for AgentTree {
55 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56 let Self {
57 role,
58 ids,
59 output_plugs,
60 } = self;
61 let ids_list = ids
62 .iter()
63 .fold(String::from(""), |acc, x| format!("{}\n - {}", acc, x));
64 let output_plugs_list = output_plugs.iter().fold(String::from(""), |acc, x| {
65 format!("{}\n - {}", acc, x)
66 });
67 write!(f, "\n{}\n {}\n {}\n", role, ids_list, output_plugs_list)
68 }
69}