someip_parse/
return_code.rs

1///Return code contained in a SOME/IP header.
2#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
3pub enum ReturnCode {
4    Ok,                    // 0x00
5    NotOk,                 // 0x01
6    UnknownService,        // 0x02
7    UnknownMethod,         // 0x03
8    NotReady,              // 0x04
9    NotReachable,          // 0x05
10    Timeout,               // 0x06
11    WrongProtocolVersion,  // 0x07
12    WrongInterfaceVersion, // 0x08
13    MalformedMessage,      // 0x09
14    WrongMessageType,      // 0x0a
15    Generic(u8),
16    InterfaceError(u8),
17}
18
19impl From<ReturnCode> for u8 {
20    fn from(r: ReturnCode) -> u8 {
21        use ReturnCode::*;
22        match r {
23            Ok => 0x00,
24            NotOk => 0x01,
25            UnknownService => 0x02,
26            UnknownMethod => 0x03,
27            NotReady => 0x04,
28            NotReachable => 0x05,
29            Timeout => 0x06,
30            WrongProtocolVersion => 0x07,
31            WrongInterfaceVersion => 0x08,
32            MalformedMessage => 0x09,
33            WrongMessageType => 0x0a,
34            Generic(value) => value,
35            InterfaceError(value) => value,
36        }
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use proptest::prelude::*;
44
45    #[test]
46    fn debug_clone_eq() {
47        let return_code = ReturnCode::Ok;
48        let _ = format!("{:?}", return_code);
49        assert_eq!(return_code, return_code.clone());
50        assert_eq!(return_code.cmp(&return_code), core::cmp::Ordering::Equal);
51        assert_eq!(
52            return_code.partial_cmp(&return_code),
53            Some(core::cmp::Ordering::Equal)
54        );
55
56        use core::hash::{Hash, Hasher};
57        use std::collections::hash_map::DefaultHasher;
58        let h1 = {
59            let mut h = DefaultHasher::new();
60            return_code.hash(&mut h);
61            h.finish()
62        };
63        let h2 = {
64            let mut h = DefaultHasher::new();
65            return_code.clone().hash(&mut h);
66            h.finish()
67        };
68        assert_eq!(h1, h2);
69    }
70
71    proptest! {
72        #[test]
73        fn into_u8(generic_error in 0x0bu8..0x20,
74                   interface_error in 0x20u8..0x5F)
75        {
76            use crate::ReturnCode::*;
77            let values = [
78                (Ok, 0x00),
79                (NotOk, 0x01),
80                (UnknownService, 0x02),
81                (UnknownMethod, 0x03),
82                (NotReady, 0x04),
83                (NotReachable, 0x05),
84                (Timeout, 0x06),
85                (WrongProtocolVersion, 0x07),
86                (WrongInterfaceVersion, 0x08),
87                (MalformedMessage, 0x09),
88                (WrongMessageType, 0x0a),
89                (Generic(generic_error), generic_error),
90                (InterfaceError(interface_error), interface_error),
91            ];
92            for (ref input, ref expected) in values.iter() {
93                let result: u8 = (*input).into();
94                assert_eq!(*expected, result);
95            }
96        }
97    }
98}