1use crate::types::RosMessage;
35use core::fmt;
36use nros_serdes::{CdrReader, CdrWriter, DeserError, Deserialize, SerError, Serialize};
37
38pub trait RosAction: Sized {
54 type Goal: RosMessage;
56
57 type Result: RosMessage + Default;
59
60 type Feedback: RosMessage;
62
63 type SendGoalRequest: RosMessage;
65
66 type SendGoalResponse: RosMessage;
68
69 type GetResultRequest: RosMessage;
71
72 type GetResultResponse: RosMessage;
74
75 type FeedbackMessage: RosMessage;
77
78 const ACTION_NAME: &'static str;
80
81 const ACTION_HASH: &'static str;
83
84 const SEND_GOAL_SERVICE_HASH: &'static str = Self::ACTION_HASH;
93
94 const GET_RESULT_SERVICE_HASH: &'static str = Self::ACTION_HASH;
97
98 const CANCEL_GOAL_SERVICE_HASH: &'static str = Self::ACTION_HASH;
104
105 const STATUS_MESSAGE_HASH: &'static str = Self::ACTION_HASH;
108
109 #[allow(clippy::result_unit_err)]
125 fn register_protocol_types() -> Result<(), ()> {
126 Ok(())
127 }
128}
129
130#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
138#[repr(i8)]
139pub enum GoalStatusCode {
140 #[default]
142 Unknown = 0,
143 Accepted = 1,
145 Executing = 2,
147 Canceling = 3,
149 Succeeded = 4,
151 Canceled = 5,
153 Aborted = 6,
155}
156
157impl GoalStatusCode {
158 pub fn is_terminal(&self) -> bool {
160 matches!(
161 self,
162 GoalStatusCode::Succeeded | GoalStatusCode::Canceled | GoalStatusCode::Aborted
163 )
164 }
165
166 pub fn is_active(&self) -> bool {
168 matches!(
169 self,
170 GoalStatusCode::Accepted | GoalStatusCode::Executing | GoalStatusCode::Canceling
171 )
172 }
173
174 pub fn from_i8(value: i8) -> Option<Self> {
176 match value {
177 0 => Some(GoalStatusCode::Unknown),
178 1 => Some(GoalStatusCode::Accepted),
179 2 => Some(GoalStatusCode::Executing),
180 3 => Some(GoalStatusCode::Canceling),
181 4 => Some(GoalStatusCode::Succeeded),
182 5 => Some(GoalStatusCode::Canceled),
183 6 => Some(GoalStatusCode::Aborted),
184 _ => None,
185 }
186 }
187}
188
189impl fmt::Display for GoalStatusCode {
190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191 match self {
192 GoalStatusCode::Unknown => write!(f, "UNKNOWN"),
193 GoalStatusCode::Accepted => write!(f, "ACCEPTED"),
194 GoalStatusCode::Executing => write!(f, "EXECUTING"),
195 GoalStatusCode::Canceling => write!(f, "CANCELING"),
196 GoalStatusCode::Succeeded => write!(f, "SUCCEEDED"),
197 GoalStatusCode::Canceled => write!(f, "CANCELED"),
198 GoalStatusCode::Aborted => write!(f, "ABORTED"),
199 }
200 }
201}
202
203impl Serialize for GoalStatusCode {
204 fn serialize(&self, writer: &mut CdrWriter) -> Result<(), SerError> {
205 writer.write_i8(*self as i8)
206 }
207}
208
209impl Deserialize for GoalStatusCode {
210 fn deserialize(reader: &mut CdrReader) -> Result<Self, DeserError> {
211 let value = reader.read_i8()?;
212 GoalStatusCode::from_i8(value).ok_or(DeserError::InvalidData)
213 }
214}
215
216#[derive(Clone, Copy, PartialEq, Eq, Hash)]
221pub struct GoalUuid {
222 pub uuid: [u8; 16],
224}
225
226impl GoalUuid {
227 pub const UUID_LEN: usize = 16;
229
230 pub const SEQ_PREFIX_LEN: usize = 0;
241
242 pub const fn new(uuid: [u8; 16]) -> Self {
244 Self { uuid }
245 }
246
247 pub const fn zero() -> Self {
249 Self { uuid: [0; 16] }
250 }
251
252 pub fn is_zero(&self) -> bool {
254 self.uuid == [0; 16]
255 }
256
257 pub fn from_counter(counter: u64) -> Self {
262 let mut uuid = [0u8; 16];
263 uuid[8..16].copy_from_slice(&counter.to_be_bytes());
265 uuid[6] = (uuid[6] & 0x0f) | 0x40; uuid[8] = (uuid[8] & 0x3f) | 0x80; Self { uuid }
269 }
270}
271
272impl Default for GoalUuid {
273 fn default() -> Self {
274 Self::zero()
275 }
276}
277
278impl Serialize for GoalUuid {
279 fn serialize(&self, writer: &mut CdrWriter) -> Result<(), SerError> {
280 for byte in &self.uuid {
281 writer.write_u8(*byte)?;
282 }
283 Ok(())
284 }
285}
286
287impl Deserialize for GoalUuid {
288 fn deserialize(reader: &mut CdrReader) -> Result<Self, DeserError> {
289 let mut uuid = [0u8; 16];
290 for byte in &mut uuid {
291 *byte = reader.read_u8()?;
292 }
293 Ok(Self { uuid })
294 }
295}
296
297impl fmt::Debug for GoalUuid {
298 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
299 write!(
301 f,
302 "GoalUuid({:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x})",
303 self.uuid[0],
304 self.uuid[1],
305 self.uuid[2],
306 self.uuid[3],
307 self.uuid[4],
308 self.uuid[5],
309 self.uuid[6],
310 self.uuid[7],
311 self.uuid[8],
312 self.uuid[9],
313 self.uuid[10],
314 self.uuid[11],
315 self.uuid[12],
316 self.uuid[13],
317 self.uuid[14],
318 self.uuid[15]
319 )
320 }
321}
322
323impl fmt::Display for GoalUuid {
324 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
325 write!(
326 f,
327 "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
328 self.uuid[0],
329 self.uuid[1],
330 self.uuid[2],
331 self.uuid[3],
332 self.uuid[4],
333 self.uuid[5],
334 self.uuid[6],
335 self.uuid[7],
336 self.uuid[8],
337 self.uuid[9],
338 self.uuid[10],
339 self.uuid[11],
340 self.uuid[12],
341 self.uuid[13],
342 self.uuid[14],
343 self.uuid[15]
344 )
345 }
346}
347
348#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
353pub struct GoalInfo {
354 pub goal_id: GoalUuid,
356 pub stamp_sec: i32,
358 pub stamp_nanosec: u32,
360}
361
362impl GoalInfo {
363 pub const fn new(goal_id: GoalUuid, stamp_sec: i32, stamp_nanosec: u32) -> Self {
365 Self {
366 goal_id,
367 stamp_sec,
368 stamp_nanosec,
369 }
370 }
371
372 pub const fn with_id(goal_id: GoalUuid) -> Self {
374 Self {
375 goal_id,
376 stamp_sec: 0,
377 stamp_nanosec: 0,
378 }
379 }
380}
381
382impl Serialize for GoalInfo {
383 fn serialize(&self, writer: &mut CdrWriter) -> Result<(), SerError> {
384 self.goal_id.serialize(writer)?;
385 writer.write_i32(self.stamp_sec)?;
386 writer.write_u32(self.stamp_nanosec)?;
387 Ok(())
388 }
389}
390
391impl Deserialize for GoalInfo {
392 fn deserialize(reader: &mut CdrReader) -> Result<Self, DeserError> {
393 Ok(Self {
394 goal_id: GoalUuid::deserialize(reader)?,
395 stamp_sec: reader.read_i32()?,
396 stamp_nanosec: reader.read_u32()?,
397 })
398 }
399}
400
401#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
405pub struct GoalStatusStamped {
406 pub goal_info: GoalInfo,
408 pub status: GoalStatusCode,
410}
411
412impl GoalStatusStamped {
413 pub const fn new(goal_info: GoalInfo, status: GoalStatusCode) -> Self {
415 Self { goal_info, status }
416 }
417}
418
419impl Serialize for GoalStatusStamped {
420 fn serialize(&self, writer: &mut CdrWriter) -> Result<(), SerError> {
421 self.goal_info.serialize(writer)?;
422 writer.write_i8(self.status as i8)?;
423 Ok(())
424 }
425}
426
427impl Deserialize for GoalStatusStamped {
428 fn deserialize(reader: &mut CdrReader) -> Result<Self, DeserError> {
429 let goal_info = GoalInfo::deserialize(reader)?;
430 let status_val = reader.read_i8()?;
431 let status = GoalStatusCode::from_i8(status_val).unwrap_or(GoalStatusCode::Unknown);
432 Ok(Self { goal_info, status })
433 }
434}
435
436#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
455#[repr(i8)]
456pub enum CancelResponseCode {
457 #[default]
459 Ok = 0,
460 Rejected = 1,
462 UnknownGoal = 2,
464 GoalTerminated = 3,
466}
467
468impl CancelResponseCode {
469 pub fn from_i8(value: i8) -> Option<Self> {
471 match value {
472 0 => Some(CancelResponseCode::Ok),
473 1 => Some(CancelResponseCode::Rejected),
474 2 => Some(CancelResponseCode::UnknownGoal),
475 3 => Some(CancelResponseCode::GoalTerminated),
476 _ => None,
477 }
478 }
479}
480
481impl Serialize for CancelResponseCode {
482 fn serialize(&self, writer: &mut CdrWriter) -> Result<(), SerError> {
483 writer.write_i8(*self as i8)
484 }
485}
486
487impl Deserialize for CancelResponseCode {
488 fn deserialize(reader: &mut CdrReader) -> Result<Self, DeserError> {
489 let value = reader.read_i8()?;
490 CancelResponseCode::from_i8(value).ok_or(DeserError::InvalidData)
491 }
492}
493
494#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
506#[repr(i8)]
507pub enum CancelResponse {
508 #[default]
510 Reject = 0,
511 Accept = 1,
513}
514
515impl CancelResponse {
516 pub fn from_i8(value: i8) -> Option<Self> {
518 match value {
519 0 => Some(CancelResponse::Reject),
520 1 => Some(CancelResponse::Accept),
521 _ => None,
522 }
523 }
524}
525
526#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
528#[repr(i8)]
529pub enum GoalResponse {
530 #[default]
532 Reject = 0,
533 AcceptAndExecute = 1,
535 AcceptAndDefer = 2,
537}
538
539impl GoalResponse {
540 pub fn from_i8(value: i8) -> Option<Self> {
542 match value {
543 0 => Some(GoalResponse::Reject),
544 1 => Some(GoalResponse::AcceptAndExecute),
545 2 => Some(GoalResponse::AcceptAndDefer),
546 _ => None,
547 }
548 }
549
550 pub fn is_accepted(&self) -> bool {
552 matches!(
553 self,
554 GoalResponse::AcceptAndExecute | GoalResponse::AcceptAndDefer
555 )
556 }
557}
558
559#[deprecated(
573 since = "0.6.0",
574 note = "renamed to `GoalUuid`, rclrs's name; use `GoalUuid`"
575)]
576pub type GoalId = GoalUuid;
577
578#[deprecated(
581 since = "0.6.0",
582 note = "renamed to `GoalStatusCode`, rclrs's name; use `GoalStatusCode`"
583)]
584pub type GoalStatus = GoalStatusCode;
585
586#[deprecated(
589 since = "0.6.0",
590 note = "renamed to `CancelResponseCode`, rclrs's name; use `CancelResponseCode`"
591)]
592pub type CancelReturnCode = CancelResponseCode;
593
594#[cfg(test)]
595mod tests {
596 extern crate std;
597
598 use super::*;
599 use std::format;
600
601 #[test]
602 fn test_goal_status_is_terminal() {
603 assert!(!GoalStatusCode::Unknown.is_terminal());
604 assert!(!GoalStatusCode::Accepted.is_terminal());
605 assert!(!GoalStatusCode::Executing.is_terminal());
606 assert!(!GoalStatusCode::Canceling.is_terminal());
607 assert!(GoalStatusCode::Succeeded.is_terminal());
608 assert!(GoalStatusCode::Canceled.is_terminal());
609 assert!(GoalStatusCode::Aborted.is_terminal());
610 }
611
612 #[test]
613 fn test_goal_status_is_active() {
614 assert!(!GoalStatusCode::Unknown.is_active());
615 assert!(GoalStatusCode::Accepted.is_active());
616 assert!(GoalStatusCode::Executing.is_active());
617 assert!(GoalStatusCode::Canceling.is_active());
618 assert!(!GoalStatusCode::Succeeded.is_active());
619 assert!(!GoalStatusCode::Canceled.is_active());
620 assert!(!GoalStatusCode::Aborted.is_active());
621 }
622
623 #[test]
624 fn test_goal_status_from_i8() {
625 assert_eq!(GoalStatusCode::from_i8(0), Some(GoalStatusCode::Unknown));
626 assert_eq!(GoalStatusCode::from_i8(1), Some(GoalStatusCode::Accepted));
627 assert_eq!(GoalStatusCode::from_i8(2), Some(GoalStatusCode::Executing));
628 assert_eq!(GoalStatusCode::from_i8(3), Some(GoalStatusCode::Canceling));
629 assert_eq!(GoalStatusCode::from_i8(4), Some(GoalStatusCode::Succeeded));
630 assert_eq!(GoalStatusCode::from_i8(5), Some(GoalStatusCode::Canceled));
631 assert_eq!(GoalStatusCode::from_i8(6), Some(GoalStatusCode::Aborted));
632 assert_eq!(GoalStatusCode::from_i8(7), None);
633 assert_eq!(GoalStatusCode::from_i8(-1), None);
634 }
635
636 #[test]
637 fn test_goal_id_zero() {
638 let id = GoalUuid::zero();
639 assert!(id.is_zero());
640 assert_eq!(id.uuid, [0; 16]);
641 }
642
643 #[test]
644 fn test_goal_id_from_counter() {
645 let id1 = GoalUuid::from_counter(1);
646 let id2 = GoalUuid::from_counter(2);
647
648 assert!(!id1.is_zero());
649 assert!(!id2.is_zero());
650 assert_ne!(id1, id2);
651
652 let id1_again = GoalUuid::from_counter(1);
654 assert_eq!(id1, id1_again);
655 }
656
657 #[test]
658 fn test_goal_id_display() {
659 let id = GoalUuid::new([
660 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
661 0x77, 0x88,
662 ]);
663 let s = format!("{}", id);
664 assert_eq!(s, "12345678-9abc-def0-1122-334455667788");
665 }
666
667 #[test]
668 fn test_cancel_return_code_from_i8() {
669 assert_eq!(CancelResponseCode::from_i8(0), Some(CancelResponseCode::Ok));
670 assert_eq!(
671 CancelResponseCode::from_i8(1),
672 Some(CancelResponseCode::Rejected)
673 );
674 assert_eq!(
675 CancelResponseCode::from_i8(2),
676 Some(CancelResponseCode::UnknownGoal)
677 );
678 assert_eq!(
679 CancelResponseCode::from_i8(3),
680 Some(CancelResponseCode::GoalTerminated)
681 );
682 assert_eq!(CancelResponseCode::from_i8(4), None);
683 }
684
685 #[test]
691 fn test_cancel_response_is_not_a_return_code() {
692 assert_eq!(CancelResponse::from_i8(0), Some(CancelResponse::Reject));
693 assert_eq!(CancelResponse::from_i8(1), Some(CancelResponse::Accept));
694 assert_eq!(CancelResponse::from_i8(2), None);
695 assert_eq!(CancelResponse::default(), CancelResponse::Reject);
696
697 assert_eq!(CancelResponse::Reject as i8, CancelResponseCode::Ok as i8);
699 assert_eq!(
700 CancelResponse::Accept as i8,
701 CancelResponseCode::Rejected as i8
702 );
703 }
704
705 #[test]
706 fn test_goal_response_is_accepted() {
707 assert!(!GoalResponse::Reject.is_accepted());
708 assert!(GoalResponse::AcceptAndExecute.is_accepted());
709 assert!(GoalResponse::AcceptAndDefer.is_accepted());
710 }
711
712 #[test]
713 fn test_goal_info_new() {
714 let goal_id = GoalUuid::from_counter(42);
715 let info = GoalInfo::new(goal_id, 123, 456);
716 assert_eq!(info.goal_id, goal_id);
717 assert_eq!(info.stamp_sec, 123);
718 assert_eq!(info.stamp_nanosec, 456);
719 }
720
721 #[test]
722 fn test_goal_info_with_id() {
723 let goal_id = GoalUuid::from_counter(42);
724 let info = GoalInfo::with_id(goal_id);
725 assert_eq!(info.goal_id, goal_id);
726 assert_eq!(info.stamp_sec, 0);
727 assert_eq!(info.stamp_nanosec, 0);
728 }
729
730 #[test]
731 fn test_goal_status_stamped() {
732 let goal_id = GoalUuid::from_counter(1);
733 let info = GoalInfo::with_id(goal_id);
734 let stamped = GoalStatusStamped::new(info, GoalStatusCode::Executing);
735 assert_eq!(stamped.goal_info.goal_id, goal_id);
736 assert_eq!(stamped.status, GoalStatusCode::Executing);
737 }
738
739 #[test]
740 fn test_goal_id_serialization() {
741 use nros_serdes::{CdrReader, CdrWriter};
742
743 let original = GoalUuid::new([
744 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
745 0x0f, 0x10,
746 ]);
747
748 let mut buf = [0u8; 32];
750 let mut writer = CdrWriter::new(&mut buf);
751 original.serialize(&mut writer).unwrap();
752 let len = writer.position();
753 assert_eq!(len, 16); let mut reader = CdrReader::new(&buf[..len]);
757 let deserialized = GoalUuid::deserialize(&mut reader).unwrap();
758
759 assert_eq!(original, deserialized);
760 }
761
762 #[test]
763 fn test_goal_status_serialization() {
764 use nros_serdes::{CdrReader, CdrWriter};
765
766 for status in [
767 GoalStatusCode::Unknown,
768 GoalStatusCode::Accepted,
769 GoalStatusCode::Executing,
770 GoalStatusCode::Canceling,
771 GoalStatusCode::Succeeded,
772 GoalStatusCode::Canceled,
773 GoalStatusCode::Aborted,
774 ] {
775 let mut buf = [0u8; 8];
776 let len = {
777 let mut writer = CdrWriter::new(&mut buf);
778 status.serialize(&mut writer).unwrap();
779 writer.position()
780 };
781
782 let mut reader = CdrReader::new(&buf[..len]);
783 let deserialized = GoalStatusCode::deserialize(&mut reader).unwrap();
784
785 assert_eq!(status, deserialized);
786 }
787 }
788
789 #[test]
790 fn test_goal_info_serialization() {
791 use nros_serdes::{CdrReader, CdrWriter};
792
793 let goal_id = GoalUuid::from_counter(42);
794 let original = GoalInfo::new(goal_id, 1234567890, 123456789);
795
796 let mut buf = [0u8; 64];
798 let mut writer = CdrWriter::new(&mut buf);
799 original.serialize(&mut writer).unwrap();
800 let len = writer.position();
801 assert_eq!(len, 24);
803
804 let mut reader = CdrReader::new(&buf[..len]);
806 let deserialized = GoalInfo::deserialize(&mut reader).unwrap();
807
808 assert_eq!(original.goal_id, deserialized.goal_id);
809 assert_eq!(original.stamp_sec, deserialized.stamp_sec);
810 assert_eq!(original.stamp_nanosec, deserialized.stamp_nanosec);
811 }
812
813 #[test]
814 fn test_goal_status_stamped_serialization() {
815 use nros_serdes::{CdrReader, CdrWriter};
816
817 let goal_id = GoalUuid::from_counter(99);
818 let goal_info = GoalInfo::new(goal_id, 987654321, 111222333);
819 let original = GoalStatusStamped::new(goal_info, GoalStatusCode::Succeeded);
820
821 let mut buf = [0u8; 64];
823 let mut writer = CdrWriter::new(&mut buf);
824 original.serialize(&mut writer).unwrap();
825 let len = writer.position();
826 assert_eq!(len, 25);
828
829 let mut reader = CdrReader::new(&buf[..len]);
831 let deserialized = GoalStatusStamped::deserialize(&mut reader).unwrap();
832
833 assert_eq!(original.goal_info.goal_id, deserialized.goal_info.goal_id);
834 assert_eq!(
835 original.goal_info.stamp_sec,
836 deserialized.goal_info.stamp_sec
837 );
838 assert_eq!(
839 original.goal_info.stamp_nanosec,
840 deserialized.goal_info.stamp_nanosec
841 );
842 assert_eq!(original.status, deserialized.status);
843 }
844}
845
846#[cfg(kani)]
851mod verification {
852 use super::*;
853
854 #[kani::proof]
857 fn goal_status_from_i8_valid_range() {
858 let val: i8 = kani::any();
859 let status = GoalStatusCode::from_i8(val);
860 if (0..=6).contains(&val) {
861 assert!(status.is_some());
862 } else {
863 assert!(status.is_none());
864 }
865 }
866
867 #[kani::proof]
868 fn goal_status_terminal_active_exclusive() {
869 let val: i8 = kani::any();
870 kani::assume((0..=6).contains(&val));
871 let status = GoalStatusCode::from_i8(val).unwrap();
872 assert!(!(status.is_terminal() && status.is_active()));
874 if val == 0 {
876 assert!(!status.is_terminal());
877 assert!(!status.is_active());
878 }
879 if (1..=3).contains(&val) {
881 assert!(status.is_active());
882 assert!(!status.is_terminal());
883 }
884 if (4..=6).contains(&val) {
886 assert!(status.is_terminal());
887 assert!(!status.is_active());
888 }
889 }
890
891 #[kani::proof]
892 #[kani::unwind(5)]
893 fn goal_status_serialize_roundtrip() {
894 let val: i8 = kani::any();
895 kani::assume((0..=6).contains(&val));
896 let status = GoalStatusCode::from_i8(val).unwrap();
897
898 let mut buf = [0u8; 8];
899 let len = {
900 let mut writer = CdrWriter::new(&mut buf);
901 status.serialize(&mut writer).unwrap();
902 writer.position()
903 };
904
905 let mut reader = CdrReader::new(&buf[..len]);
906 let deserialized = GoalStatusCode::deserialize(&mut reader).unwrap();
907 assert_eq!(status, deserialized);
908 }
909
910 #[kani::proof]
913 fn goal_response_from_i8_valid_range() {
914 let val: i8 = kani::any();
915 let resp = GoalResponse::from_i8(val);
916 if (0..=2).contains(&val) {
917 assert!(resp.is_some());
918 } else {
919 assert!(resp.is_none());
920 }
921 }
922
923 #[kani::proof]
924 fn goal_response_is_accepted_consistent() {
925 let val: i8 = kani::any();
926 kani::assume((0..=2).contains(&val));
927 let resp = GoalResponse::from_i8(val).unwrap();
928 assert_eq!(resp.is_accepted(), val >= 1);
930 }
931
932 #[kani::proof]
935 fn cancel_return_code_from_i8_valid_range() {
936 let val: i8 = kani::any();
937 let resp = CancelResponseCode::from_i8(val);
938 if (0..=3).contains(&val) {
939 assert!(resp.is_some());
940 } else {
941 assert!(resp.is_none());
942 }
943 }
944
945 #[kani::proof]
948 fn cancel_response_from_i8_valid_range() {
949 let val: i8 = kani::any();
950 let resp = CancelResponse::from_i8(val);
951 if (0..=1).contains(&val) {
952 assert!(resp.is_some());
953 } else {
954 assert!(resp.is_none());
955 }
956 }
957
958 #[kani::proof]
961 fn goal_id_zero_is_zero() {
962 let id = GoalUuid::zero();
963 assert!(id.is_zero());
964 }
965
966 #[kani::proof]
967 fn goal_id_from_counter_deterministic() {
968 let counter: u64 = kani::any();
969 kani::assume(counter <= 1_000_000);
971 let id1 = GoalUuid::from_counter(counter);
972 let id2 = GoalUuid::from_counter(counter);
973 assert_eq!(id1, id2);
974 }
975
976 #[kani::proof]
977 fn goal_id_from_counter_not_zero() {
978 let counter: u64 = kani::any();
979 kani::assume(counter > 0 && counter <= 1_000_000);
980 let id = GoalUuid::from_counter(counter);
981 assert!(!id.is_zero());
982 }
983
984 #[kani::proof]
985 #[kani::unwind(20)]
986 fn goal_id_serialize_roundtrip() {
987 let mut uuid = [0u8; 16];
988 uuid[0] = kani::any();
990 uuid[7] = kani::any();
991 uuid[15] = kani::any();
992 let id = GoalUuid::new(uuid);
993
994 let mut buf = [0u8; 32];
995 let len = {
996 let mut writer = CdrWriter::new(&mut buf);
997 id.serialize(&mut writer).unwrap();
998 writer.position()
999 };
1000
1001 let mut reader = CdrReader::new(&buf[..len]);
1002 let deserialized = GoalUuid::deserialize(&mut reader).unwrap();
1003 assert_eq!(id, deserialized);
1004 }
1005}