dis_rust/entity_information/
entity_marking_record.rs

1//     dis-rust - A rust implementation of the DIS simulation protocol.
2//     Copyright (C) 2022 Thomas Mann
3// 
4//     This software is dual-licensed. It is available under the conditions of
5//     the GNU Affero General Public License (see the LICENSE file included) 
6//     or under a commercial license (email contact@coffeebreakdevs.com for
7//     details).
8
9use bytes::{BytesMut, BufMut, Buf};
10
11#[derive(Clone, Debug,)]
12/// Entity Marking Record as defined in IEEE 1278.1 standard. Used to communicate any markings on an entity during the simulation.
13pub struct EntityMarkingRecord {
14    pub entity_marking_character_set_field: EntityMarkingCharacterSet,
15    pub entity_marking_string_record: String,
16}
17
18impl EntityMarkingRecord {
19    /// Provides a function to create a new EntityMarkingRecord.
20    pub fn new(
21        entity_marking_character_set_field: EntityMarkingCharacterSet,
22        entity_marking_string_record: String,) -> Self {
23        EntityMarkingRecord {
24            entity_marking_character_set_field,
25            entity_marking_string_record
26        }
27    }
28
29    /// Provides a function to create a default EntityMarkingRecord.
30    /// Provides an Entity Marking Record with a string and ASCII encoding.
31    /// 
32    /// # Examples
33    /// 
34    /// Creating a default EntityMarkingRecord with TEST as the marking:
35    /// 
36    /// ```
37    /// let entity_marking_record = EntityMarkingRecord::default("TEST".to_string());
38    /// ```
39    /// 
40    pub fn default(marking: String) -> Self {
41        EntityMarkingRecord {
42            entity_marking_character_set_field: EntityMarkingCharacterSet::ASCII,
43            entity_marking_string_record: marking
44        }
45    }
46
47    /// Fills a BytesMut struct with a EntityMarkingRecord serialised into binary. This buffer is then ready to be sent via
48    /// UDP to the simluation network.
49    pub fn serialize(&self, buf: &mut BytesMut) {
50        buf.put_u8(self.entity_marking_character_set_field as u8);
51        let marking = self.entity_marking_string_record.clone();
52        buf.put_slice(&marking.into_bytes()[..]);
53    }
54
55    pub fn decode(buf: &mut BytesMut) -> EntityMarkingRecord {
56        EntityMarkingRecord {
57            entity_marking_character_set_field: EntityMarkingCharacterSet::from_u8(buf.get_u8()),
58            entity_marking_string_record: "".to_string(),
59        }
60    }
61}
62
63#[derive(Debug, Clone, Copy)]
64/// Enum to represent the character set used by the marking.
65pub enum EntityMarkingCharacterSet {
66    Unused = 0,
67    ASCII = 1,
68    ArmyMarking = 2,
69    DigitChevron = 3,
70}
71
72impl EntityMarkingCharacterSet {
73    pub fn from_u8(bit: u8) -> EntityMarkingCharacterSet {
74        match bit {
75            0 => EntityMarkingCharacterSet::Unused,
76            1 => EntityMarkingCharacterSet::ASCII,
77            2 => EntityMarkingCharacterSet::ArmyMarking,
78            3 => EntityMarkingCharacterSet::DigitChevron,  
79            4_u8..=u8::MAX => EntityMarkingCharacterSet::Unused          
80        }
81    }
82}