Skip to main content

moqros_cli/
lib.rs

1//! Shared plumbing for the moqros bridge binaries.
2
3use std::time::Duration;
4
5use clap::Args;
6use moqros::ros::{QosProfile, r2r};
7
8/// Relay connection flags.
9#[derive(Debug, Clone, Args)]
10pub struct RelayArgs {
11	/// Relay URL; its path scopes broadcast names.
12	#[arg(long, env = "MOQROS_URL", default_value = "http://localhost:4443/anon")]
13	pub url: moqros::Url,
14
15	/// Disable TLS certificate verification (development only).
16	#[arg(long, env = "MOQROS_INSECURE")]
17	pub insecure: bool,
18}
19
20impl RelayArgs {
21	pub fn config(&self) -> moqros::ClientConfig {
22		moqros::ClientConfig {
23			url: self.url.clone(),
24			insecure: self.insecure,
25		}
26	}
27}
28
29/// ROS QoS flags.
30#[derive(Debug, Clone, Args)]
31pub struct QosArgs {
32	/// Use reliable QoS instead of the best-effort sensor-data profile.
33	#[arg(long)]
34	pub reliable: bool,
35}
36
37impl QosArgs {
38	pub fn profile(&self) -> QosProfile {
39		if self.reliable {
40			QosProfile::default().keep_last(5)
41		} else {
42			QosProfile::sensor_data()
43		}
44	}
45}
46
47pub fn init_logging() {
48	tracing_subscriber::fmt()
49		.with_env_filter(
50			tracing_subscriber::EnvFilter::try_from_default_env()
51				.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
52		)
53		.init();
54}
55
56/// Spin `node` forever on a blocking thread.
57pub fn spawn_spinner(mut node: r2r::Node) -> tokio::task::JoinHandle<()> {
58	tokio::task::spawn_blocking(move || {
59		loop {
60			node.spin_once(Duration::from_millis(50));
61		}
62	})
63}