rustorb_dds_ros2/lib.rs
1//! The conventions of ROS 2 on DDS, for rustorb-dds: what a DDS program
2//! needs to be seen by ROS 2 (Jazzy) as ROS 2 nodes, and to talk with
3//! them with types known only at run time.
4//!
5//! - [`names`]: ROS 2 names (topics, services, nodes, namespaces) checked
6//! and expanded, and their DDS names: `rt/`, `rq/...Request`,
7//! `rr/...Reply`, `pkg::msg::dds_::Name_`.
8//! - [`msg`]: the `.msg` and `.srv` files parsed.
9//! - [`TypeLibrary`]: interface types found on a search path (and
10//! `AMENT_PREFIX_PATH`), as [`MessageType`] and [`ServiceType`]: the
11//! fields, the DDS type as a [`DynamicType`](rustorb_xtypes::dynamic::DynamicType),
12//! the description and the RIHS01 type hash of REP-2011
13//! ([`type_description`]).
14//! - [`Value`]: messages as trees of named fields, to and from
15//! DynamicData, checked against their types.
16//! - [`QosProfile`]: the ROS 2 QoS profiles by name.
17//! - [`UserData`]: the `key=value;` entries ROS 2 puts in USER_DATA
18//! (`typehash=RIHS01_...;`).
19//! - [`RosParticipant`] and [`Node`]: nodes announced on
20//! `ros_discovery_info`, with publishers and subscriptions whose
21//! USER_DATA has the type hash; and the writer of a received sample
22//! ([`RosParticipant::writer_of`]).
23//!
24//! ROS 2 is used as a black box: what it puts on DDS, seen with its tools
25//! and in captures, and its documented conventions (REP-2011, the
26//! design articles). No ROS 2 code is used or needed; the `.msg` files
27//! are read where they are installed.
28//!
29//! ```no_run
30//! use rustorb_dds_ros2::{QosProfile, RosParticipant, TypeLibrary, UserData, Value};
31//!
32//! let types = TypeLibrary::with_dirs(["msgs"]);
33//! let string = types.message("std_msgs/msg/String")?;
34//! let ros = RosParticipant::new(0)?;
35//! let node = ros.create_node("/", "talker")?;
36//! let publisher =
37//! node.create_publisher("chatter", &string, &QosProfile::DEFAULT, &UserData::new())?;
38//! publisher.publish(&Value::Struct(vec![(
39//! "data".into(),
40//! Value::String("Hello World".into()),
41//! )]))?;
42//! # Ok::<(), rustorb_dds_ros2::Error>(())
43//! ```
44
45pub mod library;
46pub mod msg;
47pub mod names;
48pub mod node;
49pub mod qos;
50pub mod type_description;
51pub mod user_data;
52pub mod value;
53
54use std::fmt;
55
56pub use library::{MessageType, ServiceType, TypeLibrary};
57pub use node::{Node, RosParticipant, RosPublisher, RosSubscription};
58pub use qos::QosProfile;
59pub use type_description::TypeHash;
60pub use user_data::UserData;
61pub use value::Value;
62
63/// Errors of this crate.
64#[derive(Debug)]
65#[non_exhaustive]
66pub enum Error {
67 /// A ROS 2 name that breaks the rules, with the reason.
68 Name(String),
69 /// A `.msg` or `.srv` file, or a value in it, that cannot be parsed.
70 Parse(String),
71 /// No definition of a type.
72 NotFound(String),
73 /// A file that could not be read.
74 Io(String),
75 /// A value that does not fit its type.
76 Value(String),
77 /// DDS failed.
78 Dds(rustorb_dds::Error),
79}
80
81impl fmt::Display for Error {
82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83 match self {
84 Error::Name(e) => write!(f, "ROS 2 name: {e}"),
85 Error::Parse(e) => write!(f, "interface definition: {e}"),
86 Error::NotFound(e) => write!(f, "type not found: {e}"),
87 Error::Io(e) => write!(f, "{e}"),
88 Error::Value(e) => write!(f, "value: {e}"),
89 Error::Dds(e) => write!(f, "DDS: {e}"),
90 }
91 }
92}
93
94impl std::error::Error for Error {
95 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
96 match self {
97 Error::Dds(e) => Some(e),
98 _ => None,
99 }
100 }
101}
102
103impl From<rustorb_dds::Error> for Error {
104 fn from(e: rustorb_dds::Error) -> Error {
105 Error::Dds(e)
106 }
107}
108
109/// The result type of this crate.
110pub type Result<T> = std::result::Result<T, Error>;