dis_rs/common/
mod.rs

1pub mod model;
2pub(crate) mod parser;
3
4pub mod acknowledge;
5pub mod acknowledge_r;
6pub mod action_request;
7pub mod action_request_r;
8pub mod action_response;
9pub mod action_response_r;
10pub mod aggregate_state;
11pub mod attribute;
12pub mod collision;
13pub mod collision_elastic;
14pub mod comment;
15pub mod comment_r;
16pub mod create_entity;
17pub mod create_entity_r;
18pub mod data;
19pub mod data_query;
20pub mod data_query_r;
21pub mod data_r;
22pub mod designator;
23pub mod detonation;
24pub mod electromagnetic_emission;
25pub mod entity_state;
26pub mod entity_state_update;
27pub mod event_report;
28pub mod event_report_r;
29pub mod fire;
30pub mod iff;
31pub mod is_group_of;
32pub mod is_part_of;
33pub mod other;
34pub mod receiver;
35pub mod record_query_r;
36pub mod record_r;
37pub mod remove_entity;
38pub mod remove_entity_r;
39pub mod repair_complete;
40pub mod repair_response;
41pub mod resupply_cancel;
42pub mod resupply_offer;
43pub mod resupply_received;
44pub mod sees;
45pub mod service_request;
46pub mod set_data;
47pub mod set_data_r;
48pub mod set_record_r;
49pub mod signal;
50pub mod start_resume;
51pub mod start_resume_r;
52pub mod stop_freeze;
53pub mod stop_freeze_r;
54pub mod transfer_ownership;
55pub mod transmitter;
56pub mod underwater_acoustic;
57
58pub mod errors;
59mod writer;
60
61use crate::common::errors::DisError;
62use crate::common::model::Pdu;
63use crate::common::parser::parse_multiple_pdu;
64use crate::enumerations::{PduType, ProtocolVersion};
65use bytes::BytesMut;
66
67#[allow(dead_code)]
68pub enum SupportedVersion {
69    V6,
70    V7,
71    Unsupported,
72}
73
74impl From<ProtocolVersion> for SupportedVersion {
75    fn from(version: ProtocolVersion) -> Self {
76        match version {
77            ProtocolVersion::IEEE1278_1A1998 => SupportedVersion::V6,
78            ProtocolVersion::IEEE1278_12012 => SupportedVersion::V7,
79            _ => SupportedVersion::Unsupported,
80        }
81    }
82}
83
84/// Returns a `Vec` of all `ProtocolVersion`s supported by the crate.
85#[must_use]
86pub fn supported_protocol_versions() -> Vec<ProtocolVersion> {
87    vec![
88        ProtocolVersion::IEEE1278_1A1998,
89        ProtocolVersion::IEEE1278_12012,
90    ]
91}
92
93/// Trait for PduBody-s to query basic information, typically used in the header
94pub trait BodyInfo {
95    fn body_length(&self) -> u16;
96    fn body_type(&self) -> PduType;
97}
98
99/// Trait for PDUs to implement whether an interaction between one or two
100/// entities happens. Used to generically query the originating ``EntityId`` and (optional) receiving ``EntityId`` of
101/// the interaction. When a PDU has no interaction, both the originator and receiver are ``None``.
102pub trait Interaction {
103    fn originator(&self) -> Option<&model::EntityId>;
104    fn receiver(&self) -> Option<&model::EntityId>;
105}
106
107/// Trait that implements writing a `PduBody` to a buffer
108/// based on the protocol version of the PDU.
109/// Returns the number of bytes written to the buffer.
110pub trait SerializePdu {
111    fn serialize_pdu(&self, version: SupportedVersion, buf: &mut BytesMut) -> u16;
112}
113
114/// Trait that implements writing data structures to a buffer.
115/// This serialize must be independent of protocol version differences for the data structure.
116/// Returns the number of bytes written to the buffer.
117pub trait Serialize {
118    fn serialize(&self, buf: &mut BytesMut) -> u16;
119}
120
121/// Parses the contents of the input, determining the DIS version by itself.
122/// This function tries to parse as many PDUs as there are in the buffer,
123/// assuming there are only complete PDUs present in the input.
124///
125/// Assumes there will only be a single DIS version of PDUs in a buffer (packet).
126///
127/// # Errors
128/// Returns a `DisError` when parsing fails
129pub fn parse(input: &[u8]) -> Result<Vec<Pdu>, DisError> {
130    parse_multiple_pdu(input)
131}
132
133/// Parses the contents of the input as DIS version 6.
134/// This function tries to parse as many PDUs as there are in the buffer,
135/// assuming there are only complete PDUs present in the input.
136///
137/// This function will filter out any non-v6 PDUs in a buffer (packet).
138///
139/// # Errors
140/// Returns a `DisError` when parsing fails
141pub fn parse_v6(input: &[u8]) -> Result<Vec<Pdu>, DisError> {
142    let pdus = parse_multiple_pdu(input)?
143        .into_iter()
144        .filter(|pdu| pdu.header.protocol_version == ProtocolVersion::IEEE1278_1A1998)
145        .collect();
146    Ok(pdus)
147}
148
149/// Parses the contents of the input as DIS version 7.
150/// This function tries to parse as many PDUs as there are in the buffer,
151/// assuming there are only complete PDUs present in the input.
152///
153/// This function will filter out any non-v7 PDUs in a buffer (packet).
154///
155/// # Errors
156/// Returns a `DisError` when parsing fails
157pub fn parse_v7(input: &[u8]) -> Result<Vec<Pdu>, DisError> {
158    let pdus = parse_multiple_pdu(input)?
159        .into_iter()
160        .filter(|pdu| pdu.header.protocol_version == ProtocolVersion::IEEE1278_12012)
161        .collect();
162    Ok(pdus)
163}