dis_rs/common/comment_r/
model.rs

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