open_dis_rust/common/
entity_id.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
7use super::simulation_address::SimulationAddress;
8use bytes::{Buf, BufMut, BytesMut};
9
10#[derive(Copy, Clone, Debug, Default, PartialEq)]
11/// Implemented according to IEEE 1278.1-2012 ยง6.2.28
12pub struct EntityId {
13    /// The simulation's designation associated with all object identifiers
14    pub simulation_address: SimulationAddress,
15    /// The unique identification number of the entity
16    pub entity_id: u16,
17}
18
19impl EntityId {
20    #[must_use]
21    pub fn new(site_id: u16, application_id: u16, entity_id: u16) -> Self {
22        EntityId {
23            simulation_address: SimulationAddress::new(site_id, application_id),
24            entity_id,
25        }
26    }
27
28    #[must_use]
29    pub fn default(entity_id: u16) -> Self {
30        EntityId {
31            simulation_address: SimulationAddress::default(),
32            entity_id,
33        }
34    }
35
36    pub fn serialize(&self, buf: &mut BytesMut) {
37        self.simulation_address.serialize(buf);
38        buf.put_u16(self.entity_id);
39    }
40
41    pub fn deserialize(buf: &mut BytesMut) -> EntityId {
42        EntityId {
43            simulation_address: EntityId::deserialize_simulation_address(buf),
44            entity_id: buf.get_u16(),
45        }
46    }
47
48    fn deserialize_simulation_address(buf: &mut BytesMut) -> SimulationAddress {
49        SimulationAddress {
50            site_id: buf.get_u16(),
51            application_id: buf.get_u16(),
52        }
53    }
54}