1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! Derive macros for ROS2 types
//!
//! This crate provides derive macros and helper macros for ROS2 message types:
//!
//! - `TypeDescription`: Generates type descriptions for ROS2 type hash computation
//! - `Ros2Msg`: Generates ROS2 message bindings (FFI with `rcl` feature, pure Rust otherwise)
//! - `ros2_service!`: Generates service wrapper types
//! - `ros2_action!`: Generates action wrapper types
//!
//! # Features
//!
//! - `rcl`: Enable FFI code generation for ROS2 C libraries. When disabled, generates
//! pure Rust implementations (Clone, Default, PartialEq, Eq).
//!
//! # Container Attributes
//!
//! - `#[ros2(package = "pkg_name")]` - Specify the ROS2 package name
//! - `#[ros2(interface_type = "msg|srv|action")]` - Specify the interface type (default: "msg")
//!
//! # Field Attributes
//!
//! - `#[ros2(ros2_type = "byte")]` - Override field type (for byte, char, wstring)
//! - `#[ros2(capacity = 255)]` - Specify capacity for bounded strings/sequences
//! - `#[ros2(default = "0")]` - Specify default value
//!
//! # Message Example
//!
//! ```ignore
//! use ros2_types_derive::{TypeDescription, Ros2Msg};
//!
//! #[derive(TypeDescription, Ros2Msg)]
//! #[ros2(package = "std_msgs", interface_type = "msg")]
//! #[repr(C)]
//! pub struct Header {
//! pub stamp: Time,
//! pub frame_id: String,
//! }
//! ```
//!
//! # Service Example
//!
//! ```ignore
//! use ros2_types_derive::{Ros2Msg, ros2_service};
//!
//! #[derive(Ros2Msg)]
//! #[ros2(package = "example_interfaces", interface_type = "srv")]
//! #[repr(C)]
//! pub struct AddTwoInts_Request {
//! pub a: i64,
//! pub b: i64,
//! }
//!
//! #[derive(Ros2Msg)]
//! #[ros2(package = "example_interfaces", interface_type = "srv")]
//! #[repr(C)]
//! pub struct AddTwoInts_Response {
//! pub sum: i64,
//! }
//!
//! // Generate the service wrapper
//! ros2_service!(example_interfaces, AddTwoInts);
//! ```
//!
//! # Action Example
//!
//! ```ignore
//! use ros2_types_derive::{Ros2Msg, ros2_action};
//!
//! #[derive(Ros2Msg)]
//! #[ros2(package = "example_interfaces", interface_type = "action")]
//! #[repr(C)]
//! pub struct Fibonacci_Goal {
//! pub order: i32,
//! }
//!
//! #[derive(Ros2Msg)]
//! #[ros2(package = "example_interfaces", interface_type = "action")]
//! #[repr(C)]
//! pub struct Fibonacci_Result {
//! pub sequence: I32Seq<0>,
//! }
//!
//! #[derive(Ros2Msg)]
//! #[ros2(package = "example_interfaces", interface_type = "action")]
//! #[repr(C)]
//! pub struct Fibonacci_Feedback {
//! pub partial_sequence: I32Seq<0>,
//! }
//!
//! // Generate the action wrapper with all helper types
//! ros2_action!(example_interfaces, Fibonacci);
//! ```
use TokenStream;
use ;
/// Derive macro for the TypeDescription trait
///
/// This macro automatically implements the TypeDescription trait for structs,
/// generating the type description based on the struct's fields.
///
/// # Container Attributes
///
/// - `#[ros2(package = "pkg_name")]` - Specify the ROS2 package name
/// - `#[ros2(interface_type = "msg|srv|action")]` - Specify the interface type (default: "msg")
///
/// # Field Attributes
///
/// - `#[ros2(ros2_type = "byte")]` - Override field type (for byte, char, wstring)
/// - `#[ros2(capacity = 255)]` - Specify capacity for bounded strings/sequences
/// - `#[ros2(default = "0")]` - Specify default value
///
/// # Example
///
/// ```ignore
/// use ros2_types::TypeDescription;
///
/// #[derive(TypeDescription)]
/// #[ros2(package = "geometry_msgs", interface_type = "msg")]
/// struct Point {
/// x: f64,
/// y: f64,
/// z: f64,
/// }
/// ```
/// Derive macro for ROS2 message types
///
/// This macro generates the necessary implementations for ROS2 message types:
///
/// - **With `rcl` feature**: Generates FFI bindings to ROS2 C libraries including
/// TypeSupport, TryClone, PartialEq, Drop, and sequence wrapper types.
///
/// - **Without `rcl` feature**: Generates pure Rust implementations including
/// Default, Clone, PartialEq, and Eq.
///
/// # Container Attributes
///
/// - `#[ros2(package = "pkg_name")]` - Specify the ROS2 package name (required for FFI)
/// - `#[ros2(interface_type = "msg|srv|action")]` - Specify the interface type (default: "msg")
///
/// # Field Attributes
///
/// - `#[ros2(default = "value")]` - Specify default value for pure Rust Default impl
///
/// # Example
///
/// ```ignore
/// use ros2_types_derive::Ros2Msg;
///
/// #[derive(Ros2Msg)]
/// #[ros2(package = "std_msgs", interface_type = "msg")]
/// #[repr(C)]
/// pub struct Header {
/// pub stamp: Time,
/// pub frame_id: RosString<0>,
/// }
/// ```
///
/// With `rcl` feature enabled, this generates:
/// - extern "C" function declarations for init, fini, are_equal, copy
/// - TypeSupport implementation
/// - TryClone implementation
/// - HeaderSeq<N> sequence wrapper type
///
/// Without `rcl` feature, this generates:
/// - Default implementation
/// - Clone implementation
/// - PartialEq and Eq implementations
/// Macro to generate a ROS2 service wrapper type
///
/// This macro generates a service wrapper struct that implements `ServiceMsg` trait,
/// linking together the Request and Response types.
///
/// # Usage
///
/// ```ignore
/// // First, define the request and response types with #[derive(Ros2Msg)]
/// #[derive(Ros2Msg)]
/// #[ros2(package = "example_interfaces", interface_type = "srv")]
/// pub struct AddTwoInts_Request { pub a: i64, pub b: i64 }
///
/// #[derive(Ros2Msg)]
/// #[ros2(package = "example_interfaces", interface_type = "srv")]
/// pub struct AddTwoInts_Response { pub sum: i64 }
///
/// // Then generate the service wrapper
/// ros2_service!(example_interfaces, AddTwoInts);
/// ```
///
/// # Generated Code
///
/// With `rcl` feature enabled, this generates:
/// - `extern "C"` declaration for service type support
/// - Service wrapper struct implementing `ServiceMsg` trait
///
/// Without `rcl` feature, only the wrapper struct is generated (without trait impl)
/// Macro to generate a ROS2 action wrapper type
///
/// This macro generates all the necessary types for a ROS2 action:
/// - Action wrapper struct implementing `ActionMsg` trait
/// - `SendGoal` service types (Request/Response) with `ActionGoal` trait
/// - `GetResult` service types (Request/Response) with `ActionResult` trait
/// - `FeedbackMessage` type with `GetUUID` trait
///
/// # Requirements
///
/// Actions require the following dependencies:
/// - `unique_identifier_msgs` package for UUID types
/// - `builtin_interfaces` package for Time types
///
/// # Usage
///
/// ```ignore
/// // First, define the Goal, Result, and Feedback types
/// #[derive(Ros2Msg)]
/// #[ros2(package = "example_interfaces", interface_type = "action")]
/// pub struct Fibonacci_Goal { pub order: i32 }
///
/// #[derive(Ros2Msg)]
/// #[ros2(package = "example_interfaces", interface_type = "action")]
/// pub struct Fibonacci_Result { pub sequence: I32Seq<0> }
///
/// #[derive(Ros2Msg)]
/// #[ros2(package = "example_interfaces", interface_type = "action")]
/// pub struct Fibonacci_Feedback { pub partial_sequence: I32Seq<0> }
///
/// // Then generate the action wrapper with all helper types
/// ros2_action!(example_interfaces, Fibonacci);
///
/// // Or with a custom path prefix for unique_identifier_msgs:
/// ros2_action!(example_interfaces, Fibonacci, crate);
/// ```
///
/// # Generated Types
///
/// For `ros2_action!(pkg, Fibonacci)`, this generates:
/// - `Fibonacci` - Action wrapper implementing `ActionMsg`
/// - `Fibonacci_SendGoal` - SendGoal service implementing `ActionGoal`
/// - `Fibonacci_SendGoal_Request` - Goal request with UUID
/// - `Fibonacci_SendGoal_Response` - Acceptance response with timestamp
/// - `Fibonacci_GetResult` - GetResult service implementing `ActionResult`
/// - `Fibonacci_GetResult_Request` - Result request with UUID
/// - `Fibonacci_GetResult_Response` - Result response with status
/// - `Fibonacci_FeedbackMessage` - Feedback with UUID
/// Derive macro for ServiceTypeDescription trait
///
/// This macro generates the `ServiceTypeDescription` trait implementation for a marker struct.
/// It computes the service type hash using the Request and Response types' TypeDescription.
///
/// # Requirements
///
/// - The Request and Response types must already have `TypeDescription` implemented
/// - Types must follow naming convention: `{ServiceName}_Request` and `{ServiceName}_Response`
///
/// # Example
///
/// ```ignore
/// use ros2_types_derive::{TypeDescription, ServiceTypeDescription};
///
/// #[derive(TypeDescription)]
/// #[ros2(package = "example_interfaces", interface_type = "srv")]
/// pub struct AddTwoInts_Request { pub a: i64, pub b: i64 }
///
/// #[derive(TypeDescription)]
/// #[ros2(package = "example_interfaces", interface_type = "srv")]
/// pub struct AddTwoInts_Response { pub sum: i64 }
///
/// // Generate ServiceTypeDescription for the service
/// #[derive(ServiceTypeDescription)]
/// #[ros2(package = "example_interfaces")]
/// pub struct AddTwoInts;
/// ```
/// Derive macro for ActionTypeDescription trait
///
/// This macro generates the `ActionTypeDescription` trait implementation for a marker struct.
/// It computes the action type hash using the Goal, Result, and Feedback types' TypeDescription.
///
/// # Requirements
///
/// - Goal, Result, and Feedback types must already have `TypeDescription` implemented
/// - Types must follow naming convention: `{ActionName}_Goal`, `{ActionName}_Result`, `{ActionName}_Feedback`
///
/// # Example
///
/// ```ignore
/// use ros2_types_derive::{TypeDescription, ActionTypeDescription};
///
/// #[derive(TypeDescription)]
/// #[ros2(package = "example_interfaces", interface_type = "action")]
/// pub struct Fibonacci_Goal { pub order: i32 }
///
/// #[derive(TypeDescription)]
/// #[ros2(package = "example_interfaces", interface_type = "action")]
/// pub struct Fibonacci_Result { pub sequence: Vec<i32> }
///
/// #[derive(TypeDescription)]
/// #[ros2(package = "example_interfaces", interface_type = "action")]
/// pub struct Fibonacci_Feedback { pub partial_sequence: Vec<i32> }
///
/// // Generate ActionTypeDescription for the action
/// #[derive(ActionTypeDescription)]
/// #[ros2(package = "example_interfaces")]
/// pub struct Fibonacci;
/// ```