dis_rs/common/event_report/
model.rs1use crate::common::model::{
2 length_padded_to_num, EntityId, FixedDatum, PduBody, VariableDatum, BASE_VARIABLE_DATUM_LENGTH,
3 FIXED_DATUM_LENGTH,
4};
5use crate::common::{BodyInfo, Interaction};
6use crate::constants::EIGHT_OCTETS;
7use crate::enumerations::EventType;
8use crate::enumerations::PduType;
9use crate::event_report::builder::EventReportBuilder;
10#[cfg(feature = "serde")]
11use serde::{Deserialize, Serialize};
12
13pub const BASE_EVENT_REPORT_BODY_LENGTH: u16 = 28;
14
15#[derive(Clone, Debug, Default, PartialEq)]
21#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
22pub struct EventReport {
23 pub originating_id: EntityId,
24 pub receiving_id: EntityId,
25 pub event_type: EventType,
26 pub fixed_datum_records: Vec<FixedDatum>,
27 pub variable_datum_records: Vec<VariableDatum>,
28}
29
30impl EventReport {
31 #[must_use]
32 pub fn builder() -> EventReportBuilder {
33 EventReportBuilder::new()
34 }
35
36 #[must_use]
37 pub fn into_builder(self) -> EventReportBuilder {
38 EventReportBuilder::new_from_body(self)
39 }
40
41 #[must_use]
42 pub fn into_pdu_body(self) -> PduBody {
43 PduBody::EventReport(self)
44 }
45}
46
47impl BodyInfo for EventReport {
48 fn body_length(&self) -> u16 {
49 BASE_EVENT_REPORT_BODY_LENGTH
50 + (FIXED_DATUM_LENGTH * self.fixed_datum_records.len() as u16)
51 + (self
52 .variable_datum_records
53 .iter()
54 .map(|datum| {
55 let padded_record = length_padded_to_num(
56 BASE_VARIABLE_DATUM_LENGTH as usize + datum.datum_value.len(),
57 EIGHT_OCTETS,
58 );
59 padded_record.record_length as u16
60 })
61 .sum::<u16>())
62 }
63
64 fn body_type(&self) -> PduType {
65 PduType::EventReport
66 }
67}
68
69impl Interaction for EventReport {
70 fn originator(&self) -> Option<&EntityId> {
71 Some(&self.originating_id)
72 }
73
74 fn receiver(&self) -> Option<&EntityId> {
75 Some(&self.receiving_id)
76 }
77}