robotrt-middleware-core 0.1.0-beta.2

RobotRT modular robotics runtime and middleware components.
Documentation
use super::super::SimpleDiscovery;

impl SimpleDiscovery {
    /// Dump current discovery state using static-discovery directives.
    pub fn dump_static_payload(&self) -> String {
        let now = std::time::Instant::now();
        let mut lines = Vec::new();

        let mut topics: Vec<&String> = self
            .topics
            .iter()
            .filter(|topic| self.is_topic_active(topic, now))
            .collect();
        topics.sort();
        for topic in topics {
            if let Some(raw_labels) = self.labels.get(topic) {
                let mut labels = raw_labels.clone();
                labels.sort();
                if labels.is_empty() {
                    lines.push(format!("topic {}", topic));
                } else {
                    lines.push(format!("topic {} {}", topic, labels.join(",")));
                }
            } else {
                lines.push(format!("topic {}", topic));
            }
        }

        let mut services: Vec<&String> = self
            .services
            .iter()
            .filter(|service| self.is_service_active(service, now))
            .collect();
        services.sort();
        for service in services {
            if let Some(raw_labels) = self.labels.get(service) {
                let mut labels = raw_labels.clone();
                labels.sort();
                if labels.is_empty() {
                    lines.push(format!("service {}", service));
                } else {
                    lines.push(format!("service {} {}", service, labels.join(",")));
                }
            } else {
                lines.push(format!("service {}", service));
            }
        }

        let mut missions: Vec<&String> = self
            .missions
            .iter()
            .filter(|mission| self.is_mission_active(mission, now))
            .collect();
        missions.sort();
        for mission in missions {
            if let Some(raw_labels) = self.labels.get(mission) {
                let mut labels = raw_labels.clone();
                labels.sort();
                if labels.is_empty() {
                    lines.push(format!("mission {}", mission));
                } else {
                    lines.push(format!("mission {} {}", mission, labels.join(",")));
                }
            } else {
                lines.push(format!("mission {}", mission));
            }
        }

        let mut endpoint_names: Vec<&String> = self
            .endpoints
            .keys()
            .filter(|name| self.is_endpoint_active(name, now))
            .collect();
        endpoint_names.sort();
        for name in endpoint_names {
            let endpoint = self
                .endpoints
                .get(name)
                .expect("endpoint key must exist during dump");
            let mut labels = endpoint.labels.clone();
            labels.sort();
            if labels.is_empty() {
                lines.push(format!("endpoint {} {}", name, endpoint.address));
            } else {
                lines.push(format!(
                    "endpoint {} {} {}",
                    name,
                    endpoint.address,
                    labels.join(",")
                ));
            }
        }

        lines.join("\n")
    }
}