cu_ros_payloads/lib.rs
1pub mod builtin;
2pub mod sensor_msgs;
3pub mod std_msgs;
4
5use serde::Serialize;
6use std::convert::From;
7
8// By default use Rust type as ROS type
9#[macro_export]
10macro_rules! ros_type_name {
11 ($t:ty) => {{
12 std::any::type_name::<$t>().rsplit("::").next().unwrap()
13 }};
14}
15
16/// ROS adaptation trait to convert payload data to ROS compatible message.
17/// The output type must match the structure of the related "msg" file.
18/// The namespace relates to the ROS namespace (such as "std_msgs")
19/// and the type name is the same as the message filename.
20pub trait RosMsgAdapter<'a>: Sized {
21 type Output: Serialize + for<'b> From<&'b Self>;
22
23 /// The namespace of the ROS message, such as "std_msgs" or "sensor_msgs".
24 fn namespace() -> &'a str;
25
26 /// The type name of the ROS message, such as "Int8" or "PointCloud2".
27 fn type_name() -> &'a str {
28 ros_type_name!(Self::Output)
29 }
30
31 /// This hash is generated from an SHA256 from the IDL.
32 /// It is obscure.
33 /// For example Int8 is "RIHS01_26525065a403d972cb672f0777e333f0c799ad444ae5fcd79e43d1e73bd0f440"
34 /// This will only be used in ROS 2 Iron and later versions.
35 fn type_hash() -> &'static str;
36
37 /// Converts the current Copper type into the corresponding ROS message type.
38 ///
39 /// # Returns
40 /// A tuple containing:
41 /// - The converted ROS message type (`Self::Output`).
42 /// - The type hash as a string, which is used to identify the message type in ROS.
43 #[cfg(not(feature = "humble"))]
44 fn convert(&self) -> (Self::Output, &'static str) {
45 (self.into(), Self::type_hash())
46 }
47
48 #[cfg(feature = "humble")]
49 fn convert(&self) -> (Self::Output, &str) {
50 (self.into(), "TypeHashNotSupported")
51 }
52}