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