mecha10_dev/services/topology/
source_scan.rs1use anyhow::{Context, Result};
4use regex::Regex;
5use std::collections::{HashMap, HashSet};
6use std::path::Path;
7
8use super::{TopicRef, TopologyService};
9
10impl TopologyService {
11 pub(super) async fn parse_node_source(&self, source_path: &Path) -> Result<(Vec<TopicRef>, Vec<TopicRef>)> {
13 let content = tokio::fs::read_to_string(source_path)
14 .await
15 .context(format!("Failed to read source file: {}", source_path.display()))?;
16
17 let topic_defs = self.extract_topic_definitions(&content);
19
20 let publishes = self.extract_publish_calls(&content, &topic_defs);
22
23 let subscribes = self.extract_subscribe_calls(&content, &topic_defs);
25
26 Ok((publishes, subscribes))
27 }
28
29 pub fn extract_topic_definitions(&self, content: &str) -> HashMap<String, TopicRef> {
33 let mut topics = HashMap::new();
34
35 let topic_pattern =
37 Regex::new(r#"pub\s+const\s+([A-Z_]+):\s*Topic<([^>]+)>\s*=\s*Topic::new\("([^"]+)"\)"#).unwrap();
38
39 for caps in topic_pattern.captures_iter(content) {
40 let const_name = caps.get(1).map(|m| m.as_str()).unwrap_or("");
41 let message_type = caps.get(2).map(|m| m.as_str().trim()).unwrap_or("");
42 let topic_path = caps.get(3).map(|m| m.as_str()).unwrap_or("");
43
44 topics.insert(
45 const_name.to_string(),
46 TopicRef {
47 path: topic_path.to_string(),
48 message_type: Some(message_type.to_string()),
49 },
50 );
51 }
52
53 topics
54 }
55
56 pub fn extract_publish_calls(&self, content: &str, topic_defs: &HashMap<String, TopicRef>) -> Vec<TopicRef> {
60 let publish_pattern = Regex::new(r"publish_to\s*\(\s*(?:[a-z_]+::)?([A-Z_][A-Z0-9_]*)\s*,").unwrap();
65
66 Self::extract_topic_refs(content, topic_defs, &publish_pattern)
67 }
68
69 pub fn extract_subscribe_calls(&self, content: &str, topic_defs: &HashMap<String, TopicRef>) -> Vec<TopicRef> {
73 let subscribe_pattern =
78 Regex::new(r"subscribe\s*(?:::\s*<[^>]+>\s*)?\(\s*(?:[a-z_]+::)?([A-Z_][A-Z0-9_]*)\s*\)").unwrap();
79
80 Self::extract_topic_refs(content, topic_defs, &subscribe_pattern)
81 }
82
83 fn extract_topic_refs(content: &str, topic_defs: &HashMap<String, TopicRef>, pattern: &Regex) -> Vec<TopicRef> {
87 let mut refs = Vec::new();
88 let mut seen = HashSet::new();
89
90 for caps in pattern.captures_iter(content) {
91 if let Some(const_name) = caps.get(1) {
92 let const_name = const_name.as_str();
93 if let Some(topic) = topic_defs.get(const_name) {
94 if seen.insert(topic.path.clone()) {
95 refs.push(topic.clone());
96 }
97 }
98 }
99 }
100
101 refs
102 }
103}