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) 2023 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: usize = 8192;
11
12/// PDU header size in bytes
13pub const PDU_HEADER_SIZE: usize = 12;
14
15/// Maximum number of articulation parameters
16pub const MAX_ARTICULATION_PARAMS: usize = 64;
17
18/// Maximum size of entity marking string
19pub const MAX_ENTITY_MARKING_LENGTH: usize = 32;
20
21/// Protocol version constants
22pub const PROTOCOL_VERSION_1995: u8 = 3;
23pub const PROTOCOL_VERSION_1998: u8 = 4;
24pub const PROTOCOL_VERSION_2012: u8 = 7;
25
26#[must_use]
27/// Compile-time PDU size validation
28pub const fn validate_pdu_size(size: usize) -> bool {
29    size <= MAX_PDU_SIZE
30}
31
32#[must_use]
33/// Compile-time calculation of PDU size including header
34pub const fn total_pdu_size(payload_size: usize) -> usize {
35    PDU_HEADER_SIZE + payload_size
36}
37
38#[must_use]
39/// Compile-time string length validation for entity marking
40pub const fn validate_marking_length(len: usize) -> bool {
41    len <= MAX_ENTITY_MARKING_LENGTH
42}
43
44#[must_use]
45/// Compile-time protocol version validation
46pub const fn is_valid_protocol_version(version: u8) -> bool {
47    matches!(
48        version,
49        PROTOCOL_VERSION_1995 | PROTOCOL_VERSION_1998 | PROTOCOL_VERSION_2012
50    )
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_pdu_size_validation() {
59        assert!(validate_pdu_size(1000));
60        assert!(validate_pdu_size(MAX_PDU_SIZE));
61        assert!(!validate_pdu_size(MAX_PDU_SIZE + 1));
62    }
63
64    #[test]
65    fn test_total_pdu_size() {
66        assert_eq!(total_pdu_size(100), PDU_HEADER_SIZE + 100);
67        assert_eq!(total_pdu_size(0), PDU_HEADER_SIZE);
68    }
69
70    #[test]
71    fn test_marking_length_validation() {
72        assert!(validate_marking_length(31));
73        assert!(validate_marking_length(MAX_ENTITY_MARKING_LENGTH));
74        assert!(!validate_marking_length(MAX_ENTITY_MARKING_LENGTH + 1));
75    }
76
77    #[test]
78    fn test_protocol_version_validation() {
79        assert!(is_valid_protocol_version(PROTOCOL_VERSION_1995));
80        assert!(is_valid_protocol_version(PROTOCOL_VERSION_1998));
81        assert!(is_valid_protocol_version(PROTOCOL_VERSION_2012));
82        assert!(!is_valid_protocol_version(0));
83        assert!(!is_valid_protocol_version(255));
84    }
85}