hiroz 0.2.0

Native Rust ROS 2 implementation using Zenoh
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use std::marker::PhantomData;

use hiroz_cdr::{CdrBuffer, CdrDeserialize, CdrReader, CdrSerialize, CdrSerializedSize, CdrWriter};
use serde::{Deserialize, Serialize};

use super::{GoalId, GoalInfo, GoalStatus, ZAction};

// Standard ROS 2 cancel messages

/// Request to cancel one or more goals.
///
/// Used to request cancellation of specific goals or all goals.
/// A zero UUID in `goal_info.goal_id` cancels all goals.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CancelGoalRequest {
    /// Information about the goal(s) to cancel.
    pub goal_info: GoalInfo,
}

/// Response to a cancel goal request.
///
/// Contains the result code and list of goals that are being canceled.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CancelGoalResponse {
    /// Return code indicating success or failure.
    pub return_code: i8,
    /// List of goals that are being canceled.
    pub goals_canceling: Vec<GoalInfo>,
}

// Internal service/topic message types

/// Request to send a goal to an action server.
///
/// Contains the goal ID and the actual goal data.
#[derive(Debug, Clone)]
pub struct GoalRequest<A: ZAction> {
    /// Unique identifier for this goal.
    pub goal_id: GoalId,
    /// The goal data to be executed.
    pub goal: A::Goal,
}

impl<A: ZAction> serde::Serialize for GoalRequest<A>
where
    A: 'static,
    A::Goal: serde::Serialize + 'static,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeStruct;
        let mut state = serializer.serialize_struct("GoalRequest", 2)?;
        state.serialize_field("goal_id", &self.goal_id)?;
        state.serialize_field("goal", &self.goal)?;
        state.end()
    }
}

impl<'de, A: ZAction> serde::Deserialize<'de> for GoalRequest<A>
where
    A: 'static,
    A::Goal: serde::Deserialize<'de> + 'static,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(serde::Deserialize)]
        struct GoalRequestHelper<B> {
            goal_id: GoalId,
            goal: B,
        }
        let helper = GoalRequestHelper::<A::Goal>::deserialize(deserializer)?;
        Ok(GoalRequest {
            goal_id: helper.goal_id,
            goal: helper.goal,
        })
    }
}

/// Response to a goal request.
///
/// Indicates whether the goal was accepted and includes a timestamp.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GoalResponse {
    /// Whether the goal was accepted by the server.
    pub accepted: bool,
    /// Timestamp seconds (corresponds to builtin_interfaces/Time.sec)
    pub stamp_sec: i32,
    /// Timestamp nanoseconds (corresponds to builtin_interfaces/Time.nanosec)
    pub stamp_nanosec: u32,
}

/// Request to get the result of a completed goal.
///
/// Contains the goal ID for which to retrieve the result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResultRequest {
    /// The ID of the goal whose result is requested.
    pub goal_id: GoalId,
}

/// Response containing the result of a completed goal.
///
/// Includes the final status and the result data.
#[derive(Debug, Clone)]
pub struct ResultResponse<A: ZAction> {
    /// The final status of the goal.
    pub status: GoalStatus,
    /// The result data returned by the action server.
    pub result: A::Result,
}

impl<A: ZAction> serde::Serialize for ResultResponse<A>
where
    A: 'static,
    A::Result: serde::Serialize + 'static,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeStruct;
        let mut state = serializer.serialize_struct("ResultResponse", 2)?;
        state.serialize_field("status", &self.status)?;
        state.serialize_field("result", &self.result)?;
        state.end()
    }
}

impl<'de, A: ZAction> serde::Deserialize<'de> for ResultResponse<A>
where
    A: 'static,
    A::Result: serde::Deserialize<'de> + 'static,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(serde::Deserialize)]
        struct ResultResponseHelper<B> {
            status: GoalStatus,
            result: B,
        }
        let helper = ResultResponseHelper::<A::Result>::deserialize(deserializer)?;
        Ok(ResultResponse {
            status: helper.status,
            result: helper.result,
        })
    }
}

/// Message containing feedback from an executing goal.
///
/// Feedback messages are published periodically during goal execution
/// to provide progress updates to clients.
///
/// Note: This type does NOT implement WithTypeInfo because the type hash
/// is action-specific and must be provided via A::feedback_type_info()
#[derive(Debug, Clone)]
pub struct FeedbackMessage<A: ZAction> {
    /// The ID of the goal providing feedback.
    pub goal_id: GoalId,
    /// The feedback data from the executing goal.
    pub feedback: A::Feedback,
}

impl<A: ZAction> serde::Serialize for FeedbackMessage<A>
where
    A: 'static,
    A::Feedback: serde::Serialize + 'static,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeStruct;
        let mut state = serializer.serialize_struct("FeedbackMessage", 2)?;
        state.serialize_field("goal_id", &self.goal_id)?;
        state.serialize_field("feedback", &self.feedback)?;
        state.end()
    }
}

impl<'de, A: ZAction> serde::Deserialize<'de> for FeedbackMessage<A>
where
    A: 'static,
    A::Feedback: serde::Deserialize<'de> + 'static,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(serde::Deserialize)]
        struct FeedbackMessageHelper<B> {
            goal_id: GoalId,
            feedback: B,
        }
        let helper = FeedbackMessageHelper::<A::Feedback>::deserialize(deserializer)?;
        Ok(FeedbackMessage {
            goal_id: helper.goal_id,
            feedback: helper.feedback,
        })
    }
}

/// Message containing status updates for multiple goals.
///
/// Published periodically to inform clients about the current status
/// of all goals known to the action server.
///
/// Note: This type does NOT implement WithTypeInfo because the type hash
/// is action-specific and must be provided via A::status_type_info()
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatusMessage {
    /// List of status information for all goals.
    pub status_list: Vec<GoalStatusInfo>,
}

/// Status information for a single goal.
///
/// Contains the goal info and current status.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GoalStatusInfo {
    /// Information about the goal (ID and timestamp).
    pub goal_info: GoalInfo,
    /// Current status of the goal.
    pub status: GoalStatus,
}

// Internal service type wrappers
/// SendGoal service request message matching ROS2 IDL
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SendGoalRequest<A: ZAction> {
    pub goal_id: GoalId,
    pub goal: A::Goal,
}

/// SendGoal service response message matching ROS2 IDL
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SendGoalResponse {
    pub accepted: bool,
    pub stamp_sec: i32,
    pub stamp_nanosec: u32,
}

pub struct GoalService<A: ZAction>(PhantomData<A>);
impl<A: ZAction> crate::msg::ZService for GoalService<A> {
    type Request = SendGoalRequest<A>;
    type Response = SendGoalResponse;
}

impl<A: ZAction> crate::ServiceTypeInfo for GoalService<A> {
    fn service_type_info() -> crate::entity::TypeInfo {
        // Delegate to the action's send_goal_type_info method for proper interop
        A::send_goal_type_info()
    }
}

/// GetResult service request message matching ROS2 IDL
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetResultRequest {
    pub goal_id: GoalId,
}

/// GetResult service response message matching ROS2 IDL
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetResultResponse<A: ZAction> {
    pub status: i8,
    pub result: A::Result,
}

pub struct ResultService<A: ZAction>(PhantomData<A>);
impl<A: ZAction> crate::msg::ZService for ResultService<A> {
    type Request = GetResultRequest;
    type Response = GetResultResponse<A>;
}

impl<A: ZAction> crate::ServiceTypeInfo for ResultService<A> {
    fn service_type_info() -> crate::entity::TypeInfo {
        // Delegate to the action's get_result_type_info method for proper interop
        A::get_result_type_info()
    }
}

/// CancelGoal service request message matching ROS2 IDL
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CancelGoalServiceRequest {
    pub goal_info: GoalInfo,
}

/// CancelGoal service response message matching ROS2 IDL
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CancelGoalServiceResponse {
    pub return_code: i8,
    pub goals_canceling: Vec<GoalInfo>,
}

pub struct CancelService<A: ZAction>(PhantomData<A>);
impl<A: ZAction> crate::msg::ZService for CancelService<A> {
    type Request = CancelGoalServiceRequest;
    type Response = CancelGoalServiceResponse;
}

impl<A: ZAction> crate::ServiceTypeInfo for CancelService<A> {
    fn service_type_info() -> crate::entity::TypeInfo {
        // Delegate to the action's cancel_goal_type_info method for proper interop
        A::cancel_goal_type_info()
    }
}

// ── CDR serialization impls ───────────────────────────────────────────────────
// Concrete (non-generic) action message types now implement CdrSerialize +
// CdrDeserialize + CdrSerializedSize directly, so the blanket
// `impl<T: CdrSerialize + ...> ZMessage for T` covers them automatically.
//
// Generic types (GoalRequest<A>, etc.) still use the serde path via explicit
// ZMessage impls below until ZAction's associated type bounds are updated.

impl CdrSerialize for GoalStatusInfo {
    fn cdr_serialize<BO: byteorder::ByteOrder, B: CdrBuffer>(&self, w: &mut CdrWriter<'_, BO, B>) {
        self.goal_info.cdr_serialize(w);
        self.status.cdr_serialize(w);
    }
}
impl CdrDeserialize for GoalStatusInfo {
    fn cdr_deserialize<'de, BO: byteorder::ByteOrder>(
        r: &mut CdrReader<'de, BO>,
    ) -> hiroz_cdr::Result<Self> {
        Ok(GoalStatusInfo {
            goal_info: GoalInfo::cdr_deserialize(r)?,
            status: GoalStatus::cdr_deserialize(r)?,
        })
    }
}
impl CdrSerializedSize for GoalStatusInfo {
    fn cdr_serialized_size(&self, pos: usize) -> usize {
        let p = self.goal_info.cdr_serialized_size(pos);
        self.status.cdr_serialized_size(p)
    }
}

impl CdrSerialize for CancelGoalRequest {
    fn cdr_serialize<BO: byteorder::ByteOrder, B: CdrBuffer>(&self, w: &mut CdrWriter<'_, BO, B>) {
        self.goal_info.cdr_serialize(w);
    }
}
impl CdrDeserialize for CancelGoalRequest {
    fn cdr_deserialize<'de, BO: byteorder::ByteOrder>(
        r: &mut CdrReader<'de, BO>,
    ) -> hiroz_cdr::Result<Self> {
        Ok(CancelGoalRequest {
            goal_info: GoalInfo::cdr_deserialize(r)?,
        })
    }
}
impl CdrSerializedSize for CancelGoalRequest {
    fn cdr_serialized_size(&self, pos: usize) -> usize {
        self.goal_info.cdr_serialized_size(pos)
    }
}

impl CdrSerialize for CancelGoalResponse {
    fn cdr_serialize<BO: byteorder::ByteOrder, B: CdrBuffer>(&self, w: &mut CdrWriter<'_, BO, B>) {
        self.return_code.cdr_serialize(w);
        self.goals_canceling.cdr_serialize(w);
    }
}
impl CdrDeserialize for CancelGoalResponse {
    fn cdr_deserialize<'de, BO: byteorder::ByteOrder>(
        r: &mut CdrReader<'de, BO>,
    ) -> hiroz_cdr::Result<Self> {
        Ok(CancelGoalResponse {
            return_code: i8::cdr_deserialize(r)?,
            goals_canceling: Vec::<GoalInfo>::cdr_deserialize(r)?,
        })
    }
}
impl CdrSerializedSize for CancelGoalResponse {
    fn cdr_serialized_size(&self, pos: usize) -> usize {
        let p = self.return_code.cdr_serialized_size(pos);
        self.goals_canceling.cdr_serialized_size(p)
    }
}

impl CdrSerialize for GoalResponse {
    fn cdr_serialize<BO: byteorder::ByteOrder, B: CdrBuffer>(&self, w: &mut CdrWriter<'_, BO, B>) {
        self.accepted.cdr_serialize(w);
        self.stamp_sec.cdr_serialize(w);
        self.stamp_nanosec.cdr_serialize(w);
    }
}
impl CdrDeserialize for GoalResponse {
    fn cdr_deserialize<'de, BO: byteorder::ByteOrder>(
        r: &mut CdrReader<'de, BO>,
    ) -> hiroz_cdr::Result<Self> {
        Ok(GoalResponse {
            accepted: bool::cdr_deserialize(r)?,
            stamp_sec: i32::cdr_deserialize(r)?,
            stamp_nanosec: u32::cdr_deserialize(r)?,
        })
    }
}
impl CdrSerializedSize for GoalResponse {
    fn cdr_serialized_size(&self, pos: usize) -> usize {
        let p = self.accepted.cdr_serialized_size(pos);
        let p = self.stamp_sec.cdr_serialized_size(p);
        self.stamp_nanosec.cdr_serialized_size(p)
    }
}

impl CdrSerialize for ResultRequest {
    fn cdr_serialize<BO: byteorder::ByteOrder, B: CdrBuffer>(&self, w: &mut CdrWriter<'_, BO, B>) {
        self.goal_id.cdr_serialize(w);
    }
}
impl CdrDeserialize for ResultRequest {
    fn cdr_deserialize<'de, BO: byteorder::ByteOrder>(
        r: &mut CdrReader<'de, BO>,
    ) -> hiroz_cdr::Result<Self> {
        Ok(ResultRequest {
            goal_id: GoalId::cdr_deserialize(r)?,
        })
    }
}
impl CdrSerializedSize for ResultRequest {
    fn cdr_serialized_size(&self, pos: usize) -> usize {
        self.goal_id.cdr_serialized_size(pos)
    }
}

impl CdrSerialize for StatusMessage {
    fn cdr_serialize<BO: byteorder::ByteOrder, B: CdrBuffer>(&self, w: &mut CdrWriter<'_, BO, B>) {
        self.status_list.cdr_serialize(w);
    }
}
impl CdrDeserialize for StatusMessage {
    fn cdr_deserialize<'de, BO: byteorder::ByteOrder>(
        r: &mut CdrReader<'de, BO>,
    ) -> hiroz_cdr::Result<Self> {
        Ok(StatusMessage {
            status_list: Vec::<GoalStatusInfo>::cdr_deserialize(r)?,
        })
    }
}
impl CdrSerializedSize for StatusMessage {
    fn cdr_serialized_size(&self, pos: usize) -> usize {
        self.status_list.cdr_serialized_size(pos)
    }
}

impl CdrSerialize for SendGoalResponse {
    fn cdr_serialize<BO: byteorder::ByteOrder, B: CdrBuffer>(&self, w: &mut CdrWriter<'_, BO, B>) {
        self.accepted.cdr_serialize(w);
        self.stamp_sec.cdr_serialize(w);
        self.stamp_nanosec.cdr_serialize(w);
    }
}
impl CdrDeserialize for SendGoalResponse {
    fn cdr_deserialize<'de, BO: byteorder::ByteOrder>(
        r: &mut CdrReader<'de, BO>,
    ) -> hiroz_cdr::Result<Self> {
        Ok(SendGoalResponse {
            accepted: bool::cdr_deserialize(r)?,
            stamp_sec: i32::cdr_deserialize(r)?,
            stamp_nanosec: u32::cdr_deserialize(r)?,
        })
    }
}
impl CdrSerializedSize for SendGoalResponse {
    fn cdr_serialized_size(&self, pos: usize) -> usize {
        let p = self.accepted.cdr_serialized_size(pos);
        let p = self.stamp_sec.cdr_serialized_size(p);
        self.stamp_nanosec.cdr_serialized_size(p)
    }
}

impl CdrSerialize for GetResultRequest {
    fn cdr_serialize<BO: byteorder::ByteOrder, B: CdrBuffer>(&self, w: &mut CdrWriter<'_, BO, B>) {
        self.goal_id.cdr_serialize(w);
    }
}
impl CdrDeserialize for GetResultRequest {
    fn cdr_deserialize<'de, BO: byteorder::ByteOrder>(
        r: &mut CdrReader<'de, BO>,
    ) -> hiroz_cdr::Result<Self> {
        Ok(GetResultRequest {
            goal_id: GoalId::cdr_deserialize(r)?,
        })
    }
}
impl CdrSerializedSize for GetResultRequest {
    fn cdr_serialized_size(&self, pos: usize) -> usize {
        self.goal_id.cdr_serialized_size(pos)
    }
}

impl CdrSerialize for CancelGoalServiceRequest {
    fn cdr_serialize<BO: byteorder::ByteOrder, B: CdrBuffer>(&self, w: &mut CdrWriter<'_, BO, B>) {
        self.goal_info.cdr_serialize(w);
    }
}
impl CdrDeserialize for CancelGoalServiceRequest {
    fn cdr_deserialize<'de, BO: byteorder::ByteOrder>(
        r: &mut CdrReader<'de, BO>,
    ) -> hiroz_cdr::Result<Self> {
        Ok(CancelGoalServiceRequest {
            goal_info: GoalInfo::cdr_deserialize(r)?,
        })
    }
}
impl CdrSerializedSize for CancelGoalServiceRequest {
    fn cdr_serialized_size(&self, pos: usize) -> usize {
        self.goal_info.cdr_serialized_size(pos)
    }
}

impl CdrSerialize for CancelGoalServiceResponse {
    fn cdr_serialize<BO: byteorder::ByteOrder, B: CdrBuffer>(&self, w: &mut CdrWriter<'_, BO, B>) {
        self.return_code.cdr_serialize(w);
        self.goals_canceling.cdr_serialize(w);
    }
}
impl CdrDeserialize for CancelGoalServiceResponse {
    fn cdr_deserialize<'de, BO: byteorder::ByteOrder>(
        r: &mut CdrReader<'de, BO>,
    ) -> hiroz_cdr::Result<Self> {
        Ok(CancelGoalServiceResponse {
            return_code: i8::cdr_deserialize(r)?,
            goals_canceling: Vec::<GoalInfo>::cdr_deserialize(r)?,
        })
    }
}
impl CdrSerializedSize for CancelGoalServiceResponse {
    fn cdr_serialized_size(&self, pos: usize) -> usize {
        let p = self.return_code.cdr_serialized_size(pos);
        self.goals_canceling.cdr_serialized_size(p)
    }
}

// ── Generic types: still use serde path until ZAction gains CDR bounds ────────

impl<A: ZAction + 'static> crate::msg::ZMessage for GoalRequest<A>
where
    A::Goal: Send + Sync + serde::Serialize + for<'de> serde::Deserialize<'de> + 'static,
{
    type Serdes = crate::msg::SerdeCdrSerdes<GoalRequest<A>>;
}

impl<A: ZAction + 'static> crate::msg::ZMessage for ResultResponse<A>
where
    A::Result: Send + Sync + serde::Serialize + for<'de> serde::Deserialize<'de> + 'static,
{
    type Serdes = crate::msg::SerdeCdrSerdes<ResultResponse<A>>;
}

impl<A: ZAction + 'static> crate::msg::ZMessage for FeedbackMessage<A>
where
    A::Feedback: Send + Sync + serde::Serialize + for<'de> serde::Deserialize<'de> + 'static,
{
    type Serdes = crate::msg::SerdeCdrSerdes<FeedbackMessage<A>>;
}

impl<A: ZAction + 'static> crate::msg::ZMessage for SendGoalRequest<A>
where
    A::Goal: Send + Sync + serde::Serialize + for<'de> serde::Deserialize<'de> + 'static,
{
    type Serdes = crate::msg::SerdeCdrSerdes<SendGoalRequest<A>>;
}

impl<A: ZAction + 'static> crate::msg::ZMessage for GetResultResponse<A>
where
    A::Result: Send + Sync + serde::Serialize + for<'de> serde::Deserialize<'de> + 'static,
{
    type Serdes = crate::msg::SerdeCdrSerdes<GetResultResponse<A>>;
}