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