mecha10-dev 0.6.2

Mecha10 dev services — node discovery, topology analysis, and dev mode support
Documentation
//! Fallback source-file parsing for topic definitions and pub/sub call sites

use anyhow::{Context, Result};
use regex::Regex;
use std::collections::{HashMap, HashSet};
use std::path::Path;

use super::{TopicRef, TopologyService};

impl TopologyService {
    /// Parse node source file for topic definitions and usage
    pub(super) async fn parse_node_source(&self, source_path: &Path) -> Result<(Vec<TopicRef>, Vec<TopicRef>)> {
        let content = tokio::fs::read_to_string(source_path)
            .await
            .context(format!("Failed to read source file: {}", source_path.display()))?;

        // Extract topic constants and their types
        let topic_defs = self.extract_topic_definitions(&content);

        // Find publish calls
        let publishes = self.extract_publish_calls(&content, &topic_defs);

        // Find subscribe calls
        let subscribes = self.extract_subscribe_calls(&content, &topic_defs);

        Ok((publishes, subscribes))
    }

    /// Extract topic constant definitions from source
    ///
    /// Note: This method is public primarily for testing purposes.
    pub fn extract_topic_definitions(&self, content: &str) -> HashMap<String, TopicRef> {
        let mut topics = HashMap::new();

        // Match: pub const TOPIC_NAME: Topic<MessageType> = Topic::new("/path");
        let topic_pattern =
            Regex::new(r#"pub\s+const\s+([A-Z_]+):\s*Topic<([^>]+)>\s*=\s*Topic::new\("([^"]+)"\)"#).unwrap();

        for caps in topic_pattern.captures_iter(content) {
            let const_name = caps.get(1).map(|m| m.as_str()).unwrap_or("");
            let message_type = caps.get(2).map(|m| m.as_str().trim()).unwrap_or("");
            let topic_path = caps.get(3).map(|m| m.as_str()).unwrap_or("");

            topics.insert(
                const_name.to_string(),
                TopicRef {
                    path: topic_path.to_string(),
                    message_type: Some(message_type.to_string()),
                },
            );
        }

        topics
    }

    /// Extract publish_to calls
    ///
    /// Note: This method is public primarily for testing purposes.
    pub fn extract_publish_calls(&self, content: &str, topic_defs: &HashMap<String, TopicRef>) -> Vec<TopicRef> {
        // Match: ctx.publish_to(TOPIC_NAME, ...)
        // or: publish_to(TOPIC_NAME, ...)
        // or: ctx.publish_to(topics::TOPIC_NAME, ...)
        // Use a more flexible pattern that handles whitespace and optional module prefix
        let publish_pattern = Regex::new(r"publish_to\s*\(\s*(?:[a-z_]+::)?([A-Z_][A-Z0-9_]*)\s*,").unwrap();

        Self::extract_topic_refs(content, topic_defs, &publish_pattern)
    }

    /// Extract subscribe calls
    ///
    /// Note: This method is public primarily for testing purposes.
    pub fn extract_subscribe_calls(&self, content: &str, topic_defs: &HashMap<String, TopicRef>) -> Vec<TopicRef> {
        // Match: ctx.subscribe::<MessageType>(TOPIC_NAME)
        // or: subscribe(TOPIC_NAME)
        // or: ctx.subscribe::<MessageType>(topics::TOPIC_NAME)
        // Use a more flexible pattern that handles whitespace, generics, and optional module prefix
        let subscribe_pattern =
            Regex::new(r"subscribe\s*(?:::\s*<[^>]+>\s*)?\(\s*(?:[a-z_]+::)?([A-Z_][A-Z0-9_]*)\s*\)").unwrap();

        Self::extract_topic_refs(content, topic_defs, &subscribe_pattern)
    }

    /// Shared implementation for [`Self::extract_publish_calls`] and
    /// [`Self::extract_subscribe_calls`], which only differ in the regex used to find call
    /// sites referencing a topic constant.
    fn extract_topic_refs(content: &str, topic_defs: &HashMap<String, TopicRef>, pattern: &Regex) -> Vec<TopicRef> {
        let mut refs = Vec::new();
        let mut seen = HashSet::new();

        for caps in pattern.captures_iter(content) {
            if let Some(const_name) = caps.get(1) {
                let const_name = const_name.as_str();
                if let Some(topic) = topic_defs.get(const_name) {
                    if seen.insert(topic.path.clone()) {
                        refs.push(topic.clone());
                    }
                }
            }
        }

        refs
    }
}