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
22    // Helper macro to run tests that work in all configurations
23    macro_rules! test_all_configs {
24        ($name:ident, $body:block) => {
25            #[test]
26            fn $name() $body
27        };
28    }
29
30    // Tests that work in all configurations (no_std, alloc, std, kernel)
31    test_all_configs!(test_byte_order_variants, {
32        assert_eq!(ByteOrder::LittleEndian as u8, 1);
33        assert_eq!(ByteOrder::BigEndian as u8, 0);
34    });
35
36    test_all_configs!(test_byte_order_equality, {
37        assert_eq!(ByteOrder::LittleEndian, ByteOrder::LittleEndian);
38        assert_eq!(ByteOrder::BigEndian, ByteOrder::BigEndian);
39        assert_ne!(ByteOrder::LittleEndian, ByteOrder::BigEndian);
40    });
41
42    test_all_configs!(test_byte_order_clone, {
43        let original = ByteOrder::LittleEndian;
44        let cloned = original;
45        assert_eq!(original, cloned);
46
47        let original2 = ByteOrder::BigEndian;
48        let cloned2 = original2;
49        assert_eq!(original2, cloned2);
50    });
51
52    test_all_configs!(test_byte_order_copy, {
53        let order = ByteOrder::LittleEndian;
54        let copied = order; // Copy, not move
55        assert_eq!(order, copied); // Original still valid
56    });
57
58    test_all_configs!(test_byte_order_hash_trait, {
59        // Test that Hash trait is implemented by checking it compiles
60        fn _assert_hash<T: core::hash::Hash>() {}
61        _assert_hash::<ByteOrder>();
62    });
63
64    // Tests that require alloc or kernel (for format! macro)
65    #[cfg(any(feature = "alloc", feature = "kernel"))]
66    mod tests_with_format {
67        use super::*;
68        #[cfg(any(feature = "alloc", feature = "kernel"))]
69        use alloc::format;
70
71        #[test]
72        fn test_byte_order_debug() {
73            let little = format!("{:?}", ByteOrder::LittleEndian);
74            assert!(little.contains("LittleEndian"));
75
76            let big = format!("{:?}", ByteOrder::BigEndian);
77            assert!(big.contains("BigEndian"));
78        }
79    }
80
81    // Tests that require std (for DefaultHasher)
82    #[cfg(feature = "std")]
83    mod tests_std {
84        use super::*;
85        use core::hash::{Hash, Hasher};
86        use std::collections::hash_map::DefaultHasher;
87
88        #[test]
89        fn test_byte_order_hash() {
90            let mut hasher1 = DefaultHasher::new();
91            let mut hasher2 = DefaultHasher::new();
92
93            ByteOrder::LittleEndian.hash(&mut hasher1);
94            ByteOrder::LittleEndian.hash(&mut hasher2);
95            assert_eq!(hasher1.finish(), hasher2.finish());
96
97            let mut hasher3 = DefaultHasher::new();
98            ByteOrder::BigEndian.hash(&mut hasher3);
99            assert_ne!(hasher1.finish(), hasher3.finish());
100        }
101    }
102}