ros2_types/type_description.rs
1//! Type description trait
2
3use crate::{Result, calculate_type_hash};
4
5/// Information needed to construct a ROS2 message type name
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct MessageTypeName {
8 /// The message type category (typically "msg", "srv", or "action")
9 pub message_type: String,
10 /// The ROS2 package name (namespace) where the type is defined
11 pub package: String,
12 /// The ROS2 type name (without package prefix)
13 pub type_name: String,
14}
15
16impl MessageTypeName {
17 /// Create a new MessageTypeName
18 pub fn new(
19 message_type: impl Into<String>,
20 package: impl Into<String>,
21 type_name: impl Into<String>,
22 ) -> Self {
23 Self {
24 message_type: message_type.into(),
25 package: package.into(),
26 type_name: type_name.into(),
27 }
28 }
29
30 /// Get the full type name in the format "package/message_type/TypeName"
31 pub fn full_name(&self) -> String {
32 format!("{}/{}/{}", self.package, self.message_type, self.type_name)
33 }
34}
35
36/// Trait for types that can provide a ROS2 type description
37///
38/// This trait should be implemented by ROS2 message types to provide
39/// the information needed to calculate RIHS01 type hashes.
40pub trait TypeDescription {
41 /// Get the type description for this type
42 ///
43 /// Returns a complete type description including all referenced types
44 fn type_description() -> crate::types::TypeDescriptionMsg;
45
46 /// Get the message type name information
47 ///
48 /// Returns the prefix, package, and type name needed to construct
49 /// the full ROS2 message type name.
50 fn message_type_name() -> MessageTypeName;
51
52 /// Compute the RIHS01 type hash for this type
53 ///
54 /// This has a default implementation that uses `type_description()`
55 /// and calculates the SHA256 hash according to RIHS01 specification.
56 fn compute_hash() -> Result<String> {
57 let description = Self::type_description();
58 calculate_type_hash(&description)
59 }
60}
61
62/// Trait for ROS2 service types that can provide a type description for hash computation
63///
64/// This trait is separate from `ServiceMsg` (which is for runtime FFI) and only
65/// requires `TypeDescription` on Request/Response types.
66pub trait ServiceTypeDescription {
67 /// Get the type description for this service
68 ///
69 /// The service type description has fields:
70 /// - `request_message`: Reference to the Request message type
71 /// - `response_message`: Reference to the Response message type
72 /// - `event_message`: Reference to the Event message type
73 fn type_description() -> crate::types::TypeDescriptionMsg;
74
75 /// Get the service type name information
76 fn service_type_name() -> MessageTypeName;
77
78 /// Compute the RIHS01 type hash for this service
79 fn compute_hash() -> Result<String> {
80 let description = Self::type_description();
81 calculate_type_hash(&description)
82 }
83}
84
85/// Trait for ROS2 action types that can provide a type description for hash computation
86///
87/// This trait is separate from `ActionMsg` (which is for runtime FFI) and only
88/// requires `TypeDescription` on Goal/Result/Feedback types.
89pub trait ActionTypeDescription {
90 /// Get the type description for this action
91 ///
92 /// The action type description has fields:
93 /// - `goal`: Reference to the Goal message type
94 /// - `result`: Reference to the Result message type
95 /// - `feedback`: Reference to the Feedback message type
96 /// - `send_goal_service`: Reference to the SendGoal service
97 /// - `get_result_service`: Reference to the GetResult service
98 /// - `feedback_message`: Reference to the FeedbackMessage type
99 fn type_description() -> crate::types::TypeDescriptionMsg;
100
101 /// Get the action type name information
102 fn action_type_name() -> MessageTypeName;
103
104 /// Compute the RIHS01 type hash for this action
105 fn compute_hash() -> Result<String> {
106 let description = Self::type_description();
107 calculate_type_hash(&description)
108 }
109}