dis_rs/common/set_record_r/
model.rs

1use crate::common::model::EntityId;
2use crate::common::{BodyInfo, Interaction};
3use crate::constants::EIGHT_OCTETS;
4use crate::enumerations::{PduType, RequiredReliabilityService};
5use crate::model::{
6    length_padded_to_num, PduBody, RecordSpecification, BASE_RECORD_SPEC_RECORD_LENGTH,
7};
8use crate::set_record_r::builder::SetRecordRBuilder;
9#[cfg(feature = "serde")]
10use serde::{Deserialize, Serialize};
11
12pub const BASE_RECORD_R_BODY_LENGTH: u16 = 28;
13
14/// 5.12.4.15 Set Record-R PDU
15///
16/// 7.11.15 Set Record-R PDU
17#[derive(Clone, Debug, Default, PartialEq)]
18#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
19pub struct SetRecordR {
20    pub originating_id: EntityId,
21    pub receiving_id: EntityId,
22    pub request_id: u32,
23    pub required_reliability_service: RequiredReliabilityService,
24    pub record_specification: RecordSpecification,
25}
26
27impl SetRecordR {
28    #[must_use]
29    pub fn builder() -> SetRecordRBuilder {
30        SetRecordRBuilder::new()
31    }
32
33    #[must_use]
34    pub fn into_builder(self) -> SetRecordRBuilder {
35        SetRecordRBuilder::new_from_body(self)
36    }
37
38    #[must_use]
39    pub fn into_pdu_body(self) -> PduBody {
40        PduBody::SetRecordR(self)
41    }
42}
43
44impl BodyInfo for SetRecordR {
45    fn body_length(&self) -> u16 {
46        BASE_RECORD_R_BODY_LENGTH
47            + (self
48                .record_specification
49                .record_sets
50                .iter()
51                .map(|record| {
52                    let data_length_bytes = record
53                        .records
54                        .iter()
55                        .map(|rec| rec.len() as u16)
56                        .sum::<u16>();
57                    let padded_record =
58                        length_padded_to_num(data_length_bytes.into(), EIGHT_OCTETS);
59                    BASE_RECORD_SPEC_RECORD_LENGTH + padded_record.record_length as u16
60                })
61                .sum::<u16>())
62    }
63
64    fn body_type(&self) -> PduType {
65        PduType::SetRecordR
66    }
67}
68
69impl Interaction for SetRecordR {
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}