open_dis_rust/common/
constants.rs

1//     open-dis-rust - Rust implementation of the IEEE 1278.1-2012 Distributed Interactive
2//                     Simulation (DIS) application protocol
3//     Copyright (C) 2025 Cameron Howell
4//
5//     Licensed under the BSD 2-Clause License
6
7//! Constants and compile-time computations for DIS protocol
8
9/// Maximum PDU size in bytes as defined by the DIS standard
10pub const MAX_PDU_SIZE_OCTETS: usize = 8192;
11pub const MAX_PDU_SIZE_BITS: usize = 65_536;
12pub const BITS_PER_BYTE: u16 = 8;
13
14/// PDU header size in bytes
15pub const PDU_HEADER_SIZE: usize = 12;
16
17/// Maximum number of articulation parameters
18pub const MAX_ARTICULATION_PARAMS: usize = 64;
19
20/// Maximum size of entity marking string
21pub const MAX_ENTITY_MARKING_LENGTH: usize = 32;
22
23#[must_use]
24/// Compile-time PDU size validation
25pub const fn validate_pdu_size(size: usize) -> bool {
26    size <= MAX_PDU_SIZE_OCTETS
27}
28
29#[must_use]
30/// Compile-time calculation of PDU size including header
31pub const fn total_pdu_size(payload_size: usize) -> usize {
32    PDU_HEADER_SIZE + payload_size
33}
34
35#[must_use]
36/// Compile-time string length validation for entity marking
37pub const fn validate_marking_length(len: usize) -> bool {
38    len <= MAX_ENTITY_MARKING_LENGTH
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_pdu_size_validation() {
47        assert!(validate_pdu_size(1000));
48        assert!(validate_pdu_size(MAX_PDU_SIZE_OCTETS));
49        assert!(!validate_pdu_size(MAX_PDU_SIZE_OCTETS + 1));
50    }
51
52    #[test]
53    fn test_total_pdu_size() {
54        assert_eq!(total_pdu_size(100), PDU_HEADER_SIZE + 100);
55        assert_eq!(total_pdu_size(0), PDU_HEADER_SIZE);
56    }
57
58    #[test]
59    fn test_marking_length_validation() {
60        assert!(validate_marking_length(31));
61        assert!(validate_marking_length(MAX_ENTITY_MARKING_LENGTH));
62        assert!(!validate_marking_length(MAX_ENTITY_MARKING_LENGTH + 1));
63    }
64}