use std::time::Duration;
use crate::{Error, Result};
pub fn topic_type(node: &r2r::Node, topic: &str) -> Result<Option<String>> {
let namespace = node.namespace()?;
let node_name = node.fully_qualified_name()?;
let topic = resolve_topic(&namespace, &node_name, topic);
let topics = node.get_topic_names_and_types()?;
match topics.get(&topic).map(Vec::as_slice) {
None | Some([]) => Ok(None),
Some([ros_type]) => Ok(Some(ros_type.clone())),
Some(types) => Err(Error::TopicTypes {
topic,
types: types.to_vec(),
}),
}
}
pub async fn wait_for_topic_type(node: &r2r::Node, topic: &str) -> Result<String> {
let mut logged = false;
loop {
if let Some(ros_type) = topic_type(node, topic)? {
return Ok(ros_type);
}
if !logged {
tracing::info!(topic, "waiting for the topic to appear to learn its type");
logged = true;
}
tokio::time::sleep(Duration::from_millis(500)).await;
}
}
fn resolve_topic(namespace: &str, node_name: &str, topic: &str) -> String {
let join = |base: &str, rest: &str| format!("{}/{}", base.trim_end_matches('/'), rest.trim_start_matches('/'));
if topic.starts_with('/') {
topic.to_string()
} else if let Some(rest) = topic.strip_prefix('~') {
join(node_name, rest)
} else {
join(namespace, topic)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolves_topic_names() {
for (namespace, topic, resolved) in [
("/", "/imu", "/imu"),
("/", "imu", "/imu"),
("/robot", "imu", "/robot/imu"),
("/robot", "/imu", "/imu"),
("/robot", "~/state", "/robot/moqros_pub/state"),
] {
let node = format!("{}/moqros_pub", namespace.trim_end_matches('/'));
assert_eq!(resolve_topic(namespace, &node, topic), resolved, "{namespace} {topic}");
}
}
}