dis_rs/common/event_report_r/
model.rs

1use 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_r::builder::EventReportRBuilder;
10#[cfg(feature = "serde")]
11use serde::{Deserialize, Serialize};
12
13pub const BASE_EVENT_REPORT_R_BODY_LENGTH: u16 = 28;
14
15/// 5.12.4.12 Event Report-R PDU
16///
17/// 7.11.12 Event Report-R PDU
18#[derive(Clone, Debug, Default, PartialEq)]
19#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
20pub struct EventReportR {
21    pub originating_id: EntityId,
22    pub receiving_id: EntityId,
23    pub event_type: EventType,
24    pub fixed_datum_records: Vec<FixedDatum>,
25    pub variable_datum_records: Vec<VariableDatum>,
26}
27
28impl EventReportR {
29    #[must_use]
30    pub fn builder() -> EventReportRBuilder {
31        EventReportRBuilder::new()
32    }
33
34    #[must_use]
35    pub fn into_builder(self) -> EventReportRBuilder {
36        EventReportRBuilder::new_from_body(self)
37    }
38
39    #[must_use]
40    pub fn into_pdu_body(self) -> PduBody {
41        PduBody::EventReportR(self)
42    }
43}
44
45impl BodyInfo for EventReportR {
46    fn body_length(&self) -> u16 {
47        BASE_EVENT_REPORT_R_BODY_LENGTH
48            + (FIXED_DATUM_LENGTH * self.fixed_datum_records.len() as u16)
49            + (self
50                .variable_datum_records
51                .iter()
52                .map(|datum| {
53                    let padded_record = length_padded_to_num(
54                        BASE_VARIABLE_DATUM_LENGTH as usize + datum.datum_value.len(),
55                        EIGHT_OCTETS,
56                    );
57                    padded_record.record_length as u16
58                })
59                .sum::<u16>())
60    }
61
62    fn body_type(&self) -> PduType {
63        PduType::EventReportR
64    }
65}
66
67impl Interaction for EventReportR {
68    fn originator(&self) -> Option<&EntityId> {
69        Some(&self.originating_id)
70    }
71
72    fn receiver(&self) -> Option<&EntityId> {
73        Some(&self.receiving_id)
74    }
75}