Skip to main content

motor_codec/
caps.rs

1//! Bus capability descriptor.
2
3/// Runtime feature set of a CAN transport.
4///
5/// Constructed via the [`BusCapabilities::classical`] / [`BusCapabilities::fd`]
6/// constructors, which enforce the invariant `supports_fd == (max_payload_len == 64)`.
7#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8pub struct BusCapabilities {
9    /// True if the bus can transmit and receive CAN-FD frames.
10    pub supports_fd: bool,
11    /// Maximum payload length: 8 for classical, 64 for FD.
12    pub max_payload_len: u8,
13}
14
15impl BusCapabilities {
16    /// Classical CAN: `supports_fd = false`, `max_payload_len = 8`.
17    pub const fn classical() -> Self {
18        Self {
19            supports_fd: false,
20            max_payload_len: 8,
21        }
22    }
23
24    /// CAN-FD: `supports_fd = true`, `max_payload_len = 64`.
25    pub const fn fd() -> Self {
26        Self {
27            supports_fd: true,
28            max_payload_len: 64,
29        }
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[test]
38    fn classical_constants() {
39        let c = BusCapabilities::classical();
40        assert!(!c.supports_fd);
41        assert_eq!(c.max_payload_len, 8);
42    }
43
44    #[test]
45    fn fd_constants() {
46        let c = BusCapabilities::fd();
47        assert!(c.supports_fd);
48        assert_eq!(c.max_payload_len, 64);
49    }
50}