dbc_rs/
byte_order.rs

1/// Byte order (endianness) for signal encoding in CAN messages.
2///
3/// In DBC files, byte order is specified as:
4/// - `0` = BigEndian (Motorola format)
5/// - `1` = LittleEndian (Intel format)
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum ByteOrder {
8    /// Little-endian byte order (Intel format, `1` in DBC files).
9    ///
10    /// Bytes are ordered from least significant to most significant.
11    LittleEndian = 1,
12    /// Big-endian byte order (Motorola format, `0` in DBC files).
13    ///
14    /// Bytes are ordered from most significant to least significant.
15    BigEndian = 0,
16}
17
18#[cfg(test)]
19mod tests {
20    use super::ByteOrder;
21    use core::hash::Hash;
22
23    // Tests that work in all configurations (no_std, std)
24    #[test]
25    fn test_byte_order_variants() {
26        assert_eq!(ByteOrder::LittleEndian as u8, 1);
27        assert_eq!(ByteOrder::BigEndian as u8, 0);
28    }
29
30    #[test]
31    fn test_byte_order_equality() {
32        assert_eq!(ByteOrder::LittleEndian, ByteOrder::LittleEndian);
33        assert_eq!(ByteOrder::BigEndian, ByteOrder::BigEndian);
34        assert_ne!(ByteOrder::LittleEndian, ByteOrder::BigEndian);
35    }
36
37    #[test]
38    fn test_byte_order_clone() {
39        let original = ByteOrder::LittleEndian;
40        let cloned = original;
41        assert_eq!(original, cloned);
42
43        let original2 = ByteOrder::BigEndian;
44        let cloned2 = original2;
45        assert_eq!(original2, cloned2);
46    }
47
48    #[test]
49    fn test_byte_order_copy() {
50        let order = ByteOrder::LittleEndian;
51        let copied = order; // Copy, not move
52        assert_eq!(order, copied); // Original still valid
53    }
54
55    #[test]
56    fn test_byte_order_hash_trait() {
57        // Test that Hash trait is implemented by checking it compiles
58        fn _assert_hash<T: Hash>() {}
59        _assert_hash::<ByteOrder>();
60    }
61
62    // Tests that require std (for DefaultHasher)
63    #[cfg(feature = "std")]
64    mod tests_std {
65        use super::*;
66        use core::hash::{Hash, Hasher};
67        use std::collections::hash_map::DefaultHasher;
68
69        #[test]
70        fn test_byte_order_debug() {
71            let little = format!("{:?}", ByteOrder::LittleEndian);
72            assert!(little.contains("LittleEndian"));
73
74            let big = format!("{:?}", ByteOrder::BigEndian);
75            assert!(big.contains("BigEndian"));
76        }
77
78        #[test]
79        fn test_byte_order_hash() {
80            let mut hasher1 = DefaultHasher::new();
81            let mut hasher2 = DefaultHasher::new();
82
83            ByteOrder::LittleEndian.hash(&mut hasher1);
84            ByteOrder::LittleEndian.hash(&mut hasher2);
85            assert_eq!(hasher1.finish(), hasher2.finish());
86
87            let mut hasher3 = DefaultHasher::new();
88            ByteOrder::BigEndian.hash(&mut hasher3);
89            assert_ne!(hasher1.finish(), hasher3.finish());
90        }
91    }
92}