Skip to main content

nros_node/
node_options.rs

1//! Node construction options — rclrs's `NodeOptions` / `IntoNodeOptions`.
2//!
3//! Lives here rather than in `nros` so the executor's own
4//! [`create_node`](crate::Executor::create_node) can take it; `nros`
5//! re-exports both names unchanged.
6
7/// Runtime-neutral node construction options.
8///
9/// ADOPT-BOUNDED against rclrs's `NodeOptions` / `IntoNodeOptions` builder: three
10/// public fields (`name`, `namespace`, `domain_id`) and nothing else. There are no
11/// node ARGUMENTS and no rosout toggle — both are fixed at build time here,
12/// so an rclrs builder call for either is a compile error.
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub struct NodeOptions<'a> {
15    /// Source node name. Launch planning may remap/namespace later.
16    pub name: &'a str,
17    /// Source namespace. Defaults to `/`.
18    pub namespace: &'a str,
19    /// ROS domain ID hint. Defaults to `0`.
20    pub domain_id: u32,
21}
22
23impl<'a> NodeOptions<'a> {
24    /// Create node options with default namespace and domain.
25    pub const fn new(name: &'a str) -> Self {
26        Self {
27            name,
28            namespace: "/",
29            domain_id: 0,
30        }
31    }
32
33    /// Set source namespace.
34    pub const fn namespace(mut self, namespace: &'a str) -> Self {
35        self.namespace = namespace;
36        self
37    }
38
39    /// Set ROS domain ID hint.
40    pub const fn domain_id(mut self, domain_id: u32) -> Self {
41        self.domain_id = domain_id;
42        self
43    }
44}
45
46/// rclrs's `IntoNodeOptions`: anything a node can be created from — a bare
47/// name (`"talker"`) or a full [`NodeOptions`] — so
48/// `executor.create_node("talker")` and
49/// `executor.create_node("talker".namespace("/robot"))` read as they do in
50/// rclrs.
51///
52/// ADOPT-BOUNDED: rclrs's other builder steps (`arguments`,
53/// `use_global_arguments`, `enable_rosout`, `start_parameter_services`,
54/// `clock_type`, `clock_qos`) configure what is fixed at build time here,
55/// so they are absent and a call to one is a compile error.
56pub trait IntoNodeOptions<'a>: Sized {
57    /// Convert to the full options value.
58    fn into_node_options(self) -> NodeOptions<'a>;
59
60    /// Set the namespace — rclrs's `"name".namespace("/ns")`.
61    fn namespace(self, namespace: &'a str) -> NodeOptions<'a> {
62        self.into_node_options().namespace(namespace)
63    }
64}
65
66impl<'a> IntoNodeOptions<'a> for &'a str {
67    fn into_node_options(self) -> NodeOptions<'a> {
68        NodeOptions::new(self)
69    }
70}
71
72impl<'a> IntoNodeOptions<'a> for NodeOptions<'a> {
73    fn into_node_options(self) -> NodeOptions<'a> {
74        self
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn a_bare_name_takes_the_defaults() {
84        assert_eq!("talker".into_node_options(), NodeOptions::new("talker"));
85    }
86
87    #[test]
88    fn rclrs_namespace_step_on_a_bare_name() {
89        let o = "talker".namespace("/robot");
90        assert_eq!(o.name, "talker");
91        assert_eq!(o.namespace, "/robot");
92        assert_eq!(o.domain_id, 0);
93    }
94
95    #[test]
96    fn full_options_pass_through() {
97        let o = NodeOptions::new("talker").domain_id(7);
98        assert_eq!(o.into_node_options(), o);
99    }
100}